Vy Do
Vy Do

Reputation: 52516

IntelliJ IDEA 2016: Please choose persistence unit

I try to atomatic generate entities by IntelliJ IDEA but not success.

error: Please choose presistence unit.

enter image description here

Help me generate entity and mapping files.

Upvotes: 11

Views: 25202

Answers (3)

Dylan K.
Dylan K.

Reputation: 11

The top voted answer does a decent job at getting you most of the way there. However, I was having some trouble on step 3. In summary:

Follow steps one and two in that post. Once you've done that and you have a persistence.xml file under the following path: src/main/resources/META-INF/persistence.xml , you will need to map your entity.

  1. Find the "Persistence" toolbar within your IntelliJ IDE. Attached in the below screenshot is where mine was located. image 1

  2. Right click on the root of your persistence. For me this was "Well-Being-App" (this is a personal project). Select: Generate Persistence Mapping > By Database Schema. This will open the window that is shown in step 3 of the top post. image 2

  3. In this window you will need to do a few things. First, connect to your DB instance. I have MySQL installed locally so I was able to click the ellipsis next to "Choose Data Source", hit the "+" icon in the top left of the new window, select MySQL (in my case), supply your username and password in the input fields, give it a URL (likely to be jdbc:mysql://localhost:3306) and hit "Test Connection" at the bottom. If you get a success window, you've done everything right.

image 3

  1. With the same window still open, select "Schemas" and make sure you have selected your DB of choice. WELLNESS_APP in my case. Now hit Ok at the bottom.

image 4

  1. Back in the "Import Database Schema" window choose your package from the dropdown. Then, scroll through the "Database Schema Mapping" section until you find your table(s) of choice. Select them and verify the mapping is correct. Then, select the "Add to Persistence Unit" checkbox at the bottom, hit the "+" icon to the right of it, and give it a name. Verify the rest of your selections at the bottom match mine & then you're good to go. The result should be an addition to your persistence.xml that somewhat resembles what I've screenshotted.

image 5 image 5

Now, I'm quite new to all this but from my understanding, this is a process that will need to be undergone every time you decide to create, update, or delete your DB entities. I hope that is not the case, so if anyone uncovers a setting or preference that handles this for you, please let me know. Best of luck!

Upvotes: 1

Yuechuan Xiao
Yuechuan Xiao

Reputation: 86

you need add "hibernate.cfg.xml" first

Project Structure -> Modules -> Hibernate add hibernate.cfg.xml

Upvotes: 4

Robyn Liu
Robyn Liu

Reputation: 241

I've got the similar problem.Here is my solution.

  1. add "JPA" facet.
    open the "Project Structure" window -> click the "Facets" -> click the "add" button -> select "JPA"

  2. add JPA persistence.xml.
    add persistence click "persistence" -> right click your root item in the "persistence" toolbar -> "Generate Persistence Mapping" -> "By Database Schema".

  3. generate entities.
    Step 3

Upvotes: 24

Related Questions