使用orm.xml(Hibernate)替代仅JPA的实体表生成器

时间:2018-11-01 07:10:31

标签: java xml hibernate jpa persistence

我有一个JPA实体,如下所示:

public class Host {
    @Id
    @TableGenerator(name = "Host.id", schema = "Schema", table = "SEQUENCE", pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", pkColumnValue = "Host.id")
    @GeneratedValue(generator = "Host.id", strategy = GenerationType.TABLE)
    protected Long id;

    @NotNull
    @Column(length = 64, nullable = false)
    protected String name;

    @Size(max = 64)
    @Column(length = 64)
    protected String hardwareType;

    @Size(max = 65535)
    @Column(length = 65535)
    protected String resourceDescription;
}

如何覆盖表生成器(不使用特定于Hibernate的@GenericGenerator注释)?

当前,我有一个解决方案,可以在<hibernate-mapping>文件中为实体定义orm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.x.y.datamodel.config.equipment.Host" schema="Schema">
        <id name="id" column="id">
            <generator class="com.x.y.customtablegenerator.CustomTableGenerator"/>
        </id>
        <property name="name" column="name"></property>
        <property name="hardwareType" column="hardwareType"></property>
        ...
    </class>
</hibernate-mapping>

但是,这要求我为实体中定义的每个属性添加一个<property>

是否有一种方法可以覆盖 only 生成器,而不必两次指定实体的所有属性?

0 个答案:

没有答案