从数据库模式生成 Java 类,不带持久性单元



我正在使用Spring Boot,我想从IntelliJ中的数据库模式生成Java注释类。

我去Persistence -> Generate Persistence Mapping -> By Database Schema但我无法生成类,因为我没有持久性.xml文件,所以我得到错误:

JPA 注释映射至少需要一个持久性单元

我在春天靴子所以...我该怎么做?

如果您使用的是 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

绒球.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.复仇.xml

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

db_dvdrental.休眠属性

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 实体在 target/generation-sources/hibernate3 生成,生成的包需要复制到 src/main/java。

同样,http://tech.asimio.net/2016/08/04/Integration-Testing-using-Spring-Boot-Postgres-and-Docker.html 提供了更好的说明和演示源代码,以完成从现有模式生成 JPA 实体。

最新更新