How to generate Persistence units?
Hello,
I'm having a hibernate project setup issue. During project setup, I didn't select JEE persietence, but instead just told Intellij I was using Hibernate.
-------------------------------
Here's what I did:
(1) I created a regular Java Web project where I selected to include Hiberante and MySQL jars.
(2) I created a db connection to the database and confirmed it was "query-able" from the Intelilj project.
The Hibernate Config generated has the DB connection information, with user and password.
(3) I generated a Persistence mapping from the database in the Persistence tab of Intelij.
The entity class for zipcodes looks correctly generated.
(4) Then wrote a quick DAO:
public static void main(String[] args)
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Zipcodes");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
// test get the zip code list...
List zipCodes = (ArrayList) em.createQuery("SELECT from ZIPCODES");
tx.commit();
em.close();
// code compiles.
Yet, when I try to run this, I get this error: "Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager"
Didn't Intellij already generate this persistence info in step 3 above?
I looked into this error and it says I have to have a persietence.xml in the WEB-INF folder. However, Intellij didn't create either a persistence.xml or a WEB-INF folder in this project.
Thanks in advance for any help or suggestions.
-m
请先登录再写评论。
It seems to me that you are running this program from the command line and not from the web container.
You should read the hibernate docs about configuring hibernate in stand alone jse applications.
Appreciate your reply.
No, as I described above, I created a Web app when creating the project.
I'm using the same approach (using the main method for testing) as I did when I picked JEE Persistence in another app that worked.
Both applications are run the same way: from within IDEA. The Hibernate only method does not work, and I can't get it to work, but the JEE Persietence project does work.
Furthermore in the Hibernate-only project, even though I have a hibernate.cfg.xml, I am unable to add a Persistence mapping.
It's confusing that you can add Hiberante two ways to a project:
1. Using Hibernate Only
2. Using JEE Persistence
Thanks,
- m
The problem seems to be caused by the fact that by default META-INF/persistence.xml
is not in source root and thus not copied on make to the output folder.
So the quick DAO when built and run can't find it in classpath.
The fix: move META-INF/ to a source root.
Thanks Gregory. That could very well be it.
In any case, since I understand IDEA will soon have the option to generate DAOs when reverse engineering entity objects, I'm assuming IDEA would handle that detail of having the persistence.xml in the right folder so I don't need to juggle all the configuration permutations.
Appreciate your reply.
Thanks again,
- m