IDEA 配置了 JPA 后无法生成 Entity 无报错

【坑】配置了之后用persistence生成无效,也不报错

最后发现是我直接在moudle里面添加 ,结果添加的JPA在工程(webci)之外,一开始没找到原因,后来去gooogle上搜发现是这个问题,这是原文,请按这个走(给不能外网的同孩拷出来了)

原网址:https://stackoverflow.com/questions/46892039/intellij-idea-persistence-support-for-spring-boot-project

IntelliJ IDEA persistence support for Spring Boot project

Ask Question

up vote4down votefavorite

2

I try to create a Spring Boot 2 project with JPA to access a MySQL database using Hibernate. I have created a Gradle project on http://start.spring.io/ with JPA and MySQL dependency and imported into IntelliJ IDEA 2017.1 Ultimate

By default the persistence tool window is not present. I have searched for solution and found many different answer and know I'm a bit confused how should I configure my project. What is the correct way to do it?

  1. What framework support should I add to my project?

    • Spring Data JPA
    • Hibernate
    • JPA
  2. How should I add them?

    • Right click on my module and "Add Framework Support" (I can select Spring Data JPA and Hibernate here)
    • Project Structure -> Facets -> Add (I can select Hibernate and JPA here)
  3. If my project name is db and I have the db_main and db_test modules then to which one should I attach the Facet?

    • db
    • db_main

At the moment I added JPA Facet with Hibernate provider and added the persistence.xml. So I have the Persistence window.

However if I try to generate entities I select the datasource, the tables, the package but nothing happens. I mean no error message just the window closes as if it would be succeeded but there is no changes in the xml and no entity was generated.

java spring hibernate jpa intellij-idea

shareimprove this question

edited Oct 23 '17 at 15:14

asked Oct 23 '17 at 14:35

Vmxes

172114

add a comment

1 Answer

activeoldestvotes

up vote5down vote

After trying a lot of solutions found on the web here is what worked for me. I hope this helps someone.

I use IntelliJ IDEA 2017.1 Ultimate

Step 1.

I have created a Spring Boot (2.0.0 M5) project using Gradle with the built in wizard and select WebJPA and MySQL dependencies.enter image description here

This gives you a very simple project structure but the Persistence tool is not available: enter image description here

Step 2.

Add JPA to Facets in Project Structure window to the main module and set Default JPA Providerto Hibernate.

Note that no descriptor was added.

enter image description here

As result the Persistence tool window is now available and it automatically contains Entities as Persistence Unit.

enter image description here

Step 3.

Add new DataSource to your project and also set datasource properties in the application.properties file.

enter image description here

Step 4.

In the Persistence tool window right click on Entities and select Generate Persistence Mappingand By Database Schema

enter image description here

Now you can select for which tables you want to generate an entity:

enter image description here

IDEA will generate the entity classes for you.

enter image description here

Step 5.

At this point if you open the JPA console and write your HQL or JPL query it will fail. You need to rebuild your project before using the console.

enter image description here

shareimprove this answer

猜你喜欢

转载自blog.csdn.net/wanxiaoderen/article/details/82285834