简体   繁体   English

从没有持久性单元的数据库模式生成 Java 类

[英]Generate Java classes from Database Schema without persistence unit

I'm using Spring Boot and I want to generate Java annotated classes from a database schema in IntelliJ.我正在使用 Spring Boot,我想从 IntelliJ 中的数据库模式生成 Java 注释类。

I go to Persistence -> Generate Persistence Mapping -> By Database Schema but I can't generate classes because I haven't persistence.xml file, so I get the error:我转到Persistence -> Generate Persistence Mapping -> By Database Schema但我无法生成类,因为我没有 persistence.xml 文件,所以我收到错误:

JPA annotation mappings require at least one Persistence Unit JPA 注释映射至少需要一个 Persistence Unit

I'm in Spring Boot so... How can I do it?我在 Spring Boot 所以......我该怎么做?

If you are using Maven, it could be accomplished using hibernate3-maven-plugin along with .reveng.xml file and Hibernate connection properties.如果您使用 Maven,则可以使用hibernate3-maven-plugin以及.reveng.xml文件和Hibernate连接属性来完成。

Here is an example taken from a section of a blog I published a few months ago at: http://tech.asimio.net/2016/08/04/Integration-Testing-using-Spring-Boot-Postgres-and-Docker.html#generating-jpa-entities-from-database-schema这是我几个月前在博客中发布的一个部分的示例: http : //tech.asimio.net/2016/08/04/Integration-Testing-using-Spring-Boot-Postgres-and-Docker .html#generating-jpa-entities-from-database-schema

pom.xml 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 db_dvdrental.reveng.xml

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

db_dvdrental.hibernate.properties db_dvdrental.hibernate.properties

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

Generate entities生成实体

mvn hibernate3:hbm2java

JPA entities are generated at target/generated-sources/hibernate3 and the resulting package needs to be copied to src/main/java. JPA 实体在 target/generated-sources/hibernate3 生成,生成的包需要复制到 src/main/java。

Again, http://tech.asimio.net/2016/08/04/Integration-Testing-using-Spring-Boot-Postgres-and-Docker.html provides better instructions and demo source code to accomplish generating JPA entities from existing schema.同样, http://tech.asimio.net/2016/08/04/Integration-Testing-using-Spring-Boot-Postgres-and-Docker.html提供了更好的说明和演示源代码来完成从现有模式生成 JPA 实体.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM