gpt4 book ai didi

java - 从数据库模式生成 Java 类,无需持久化单元

转载 作者:行者123 更新时间:2023-11-30 02:41:28 25 4
gpt4 key购买 nike

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

我转到持久性 -> 生成持久性映射 -> 通过数据库架构,但无法生成类,因为我没有 persistence.xml 文件,因此出现错误:

JPA annotation mappings require at least one Persistence Unit

我使用的是 Spring Boot,所以...我该怎么做?

最佳答案

如果您使用 Maven,则可以使用 hibernate3-maven-plugin 以及 .reveng.xml 文件和 Hibernate 连接来完成属性。

以下示例取 self 几个月前发布的博客的一部分: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.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

生成实体

mvn hibernate3:hbm2java

JPA 实体在 target/generated-sources/hibernate3 生成,并且需要将生成的包复制到 src/main/java。

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

关于java - 从数据库模式生成 Java 类,无需持久化单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41550670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com