【问题标题】:How to skip the schema generation of a superclass table? Java/JPA/Hibernate/Annotations/Maven/hbm2ddl如何跳过超类表的模式生成? Java/JPA/休眠/注释/Maven/hbm2ddl
【发布时间】:2011-10-06 22:11:51
【问题描述】:

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

如何指定从 myapp-schema-ddl.sql 输出中省略 shared_schema 表的 create 和 alter table 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>

【问题讨论】:

  • 您已正确指定&lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;,但您是否尝试删除&lt;class&gt;com.company.shared.SharedSuperEntity&lt;/class&gt;

标签: java hibernate jpa maven hbm2ddl


【解决方案1】:

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

【讨论】:

  • Sed 在我们开发使用的几十个 Windows 机器上不可用;我宁愿在 hibernate3:hbm2ddl 执行的 Maven 阶段处理这个问题。我听说有一个使用数据库对象的 XML 映射唯一的 hacky 解决方法,但我们都准备使用注释。我还听说过一个名为 maven-replacer-plugin 的 maven 插件,我希望它可以帮助我解决眼前的问题。
  • @Tom:替换插件看起来很有前途,因为它支持正则表达式。在数据库对象方面,XML 并不是唯一的方法,在任何情况下您都可以将它与注解映射一起使用。我写了一个blog post 详细说明了如何在休眠中使用辅助数据库对象,如果你想看看那条路线。
  • 谢谢瑞恩。允许通过 Hibernate 定义视图对我来说似乎很有用,恕我直言,应该将其视为功能增强。
猜你喜欢
  • 2012-08-05
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 2011-03-24
相关资源
最近更新 更多