gpt4 book ai didi

java - 如何跳过父类(super class)表的模式生成? Java/JPA/Hibernate/注释/Maven/hbm2ddl

转载 作者:行者123 更新时间:2023-12-01 05:42:46 24 4
gpt4 key购买 nike

我们的应用程序需要与另一个本地运行的应用程序共享现有的数据库表,因此,我希望引用该表,但跳过为其生成 DDL。我还希望将应用程序架构中的表定义为别名或 MySQL 合并表,但我可以将其添加到将在生成的架构脚本之前运行的手动脚本中。

如何指定从 myapp-schema-ddl.sql 输出中省略共享模式表的创建和更改表 DDL?

来自 commons/src/main/java/com/company/shared/SharedSuperEntity.java:

@Entity
@Table(name="shared_entity", catalog = "shared_schema")
@Inheritance(strategy = InheritanceType.JOINED)
public class SharedSuperEntity {
// fields, etc...
}

来自 myapp/src/main/java/com/company/shared/SharedSuperEntity.java:

@Entity
@Table(name="local_entity", catalog = "local_schema")
public class LocalEntity extends SharedSuperEntity {
// fields, etc...
}

来自 myapp/src/main/resources/META_INF/persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="MyAppPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/LocalDS</jta-data-source>
<class>com.company.shared.SharedSuperEntity</class>
<class>com.company.myapp.LocalEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>AUTO</validation-mode>
<properties>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.bytecode.use_reflection_optimizer" value="false" />
<property name="hibernate.default_batch_fetch_size" value="4" />
<property name="hibernate.default_entity_mode" value="pojo" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.jdbc.batch_versioned_data" value="true" />
<property name="hibernate.jdbc.fetch_size" value="0" />
<property name="hibernate.jdbc.use_get_generated_keys" value="false" />
<property name="hibernate.jdbc.use_scrollable_resultset" value="false" />
<property name="hibernate.jdbc.use_streams_for_binary" value="false" />
<property name="hibernate.hibernate.max_fetch_depth" value="3" />
<property name="hibernate.order_updates" value="true" />
<property name="hibernate.query.substitutions" value="true 1, false 0, yes 'Y', no 'N'" />
<property name="hibernate.use_identifer_rollback" value="true" />
<property name="hibernate.use_outer_join" value="false" />
<property name="hibernate.use_sql_comments" value="false" />
<property name="hibernate.id.new_generator_mappings" value="true" />
</properties>
</persistence-unit>

从 myapp/pom.xml 中:

        <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>jpaconfiguration</implementation>
</component>
</components>
<componentProperties>
<persistenceunit>MyAppPU</persistenceunit>
<outputfilename>myapp-schema-ddl.sql</outputfilename>
<drop>false</drop>
<create>true</create>
<export>false</export>
<format>true</format>
<jdk5>true</jdk5>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>

最佳答案

我认为 Hibernate 不直接支持这一点。您可以创建两个配置文件:一个列出运行时的所有映射类,另一个忽略用于模式生成的违规类。否则,您可能会考虑某种后处理(例如 sed 脚本),以在事后删除 DDL。

关于java - 如何跳过父类(super class)表的模式生成? Java/JPA/Hibernate/注释/Maven/hbm2ddl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720530/

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