2017-01-09 60 views
0

我使用Spring的引導,我想在的IntelliJ數據庫模式生成Java註釋類。生成數據庫架構的Java類,而不會持久單元

我去Persistence -> Generate Persistence Mapping -> By Database Schema但我不能產生類,因爲我還沒有persistence.xml文件,所以我得到的錯誤:

JPA annotation mappings require at least one Persistence Unit

我在春天引導很......怎麼能我這麼做?

回答

1

如果您使用的是Maven,可以使用hibernate3-maven-plugin以及.reveng.xml文件和Hibernate連接屬性來完成。

這是從博客的部分採取了一個例子,我在幾個月前發表在:http://tech.asimio.net/2016/08/04/Integration-Testing-using-Spring-Boot-Postgres-and-Docker.html#generating-jpa-entities-from-database-schema

的pom.xml

... 
<properties> 
... 
    <postgresql.version>9.4-1206-jdbc42</postgresql.version> 
... 
</properties> 
... 
<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>hibernate3-maven-plugin</artifactId> 
    <version>2.2</version> 
    <configuration> 
     <components> 
     <component> 
      <name>hbm2java</name> 
      <implementation>jdbcconfiguration</implementation> 
      <outputDirectory>target/generated-sources/hibernate3</outputDirectory> 
     </component> 
     </components> 
     <componentProperties> 
     <revengfile>src/main/resources/reveng/db_dvdrental.reveng.xml</revengfile> 
     <propertyfile>src/main/resources/reveng/db_dvdrental.hibernate.properties</propertyfile> 
     <packagename>com.asimio.dvdrental.model</packagename> 
     <jdk5>true</jdk5> 
     <ejb3>true</ejb3> 
     </componentProperties> 
    </configuration> 
    <dependencies> 
    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib-nodep</artifactId> 
     <version>2.2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>${postgresql.version}</version> 
    </dependency>    
    </dependencies> 
</plugin> 
... 

db_dvdrental.reveng.xml

... 
<hibernate-reverse-engineering> 
    <schema-selection match-schema="public" /> 
</hibernate-reverse-engineering> 

db_dvdrental.hibernate.prop ERTIES

hibernate.connection.driver_class=org.postgresql.Driver 
hibernate.connection.url=jdbc:postgresql://localhost:5432/db_dvdrental 
hibernate.connection.username=user_dvdrental 
hibernate.connection.password=changeit 

生成實體

mvn hibernate3:hbm2java 

JPA實體在靶/生成來源/ Hibernate3中產生的並需要被複制到的src /主/ JAVA所得到的封裝。

再次,http://tech.asimio.net/2016/08/04/Integration-Testing-using-Spring-Boot-Postgres-and-Docker.html提供了更好的說明和演示源代碼來完成從現有模式生成JPA實體。