如何映射JPA鉴别器值?

时间:2014-09-01 03:33:38

标签: hibernate jpa

我相信我有这个工作,我一定做了些什么。希望有人可以立即看到问题。

继承关系中的两个类。两张桌子在玩。两个表都接收记录。手头的问题:没有存储鉴别器值。

父类

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="entp", discriminatorType=DiscriminatorType.STRING)
@Table(name="enrg")
public abstract class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)   
    @Column(name="enid")
    private Long entityId;

    // some string property mapped to column 'nm;
    // some string property mapped to column 'ns;
    // some string property mapped to column 'tmcr;

儿童班

@Entity
@DiscriminatorValue("ws")
@Table(name="ws")
public class Website extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @Column(name="exdr", length=100)
    private String exportDirectory;

服务电话

getEntityManager().persist(website);

SQL

Hibernate: insert into enrg (nm, ns, tmcr) values (?, ?, ?)
Hibernate: insert into ws (exdr, enid) values (?, ?)

休眠

<persistence-unit name="prod" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>prodDataSource</jta-data-source>

    <class>webadmin.domain.core.Website</class>

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name = "hibernate.show_sql" value = "true" />
        <property name = "hibernate.discriminator.ignore_explicit_for_joined" value = "false" />
    </properties>

</persistence-unit>

我需要做什么,让Hibernate在enrg的插入中包含entp列/值?在这种情况下,entp值应为&#39; ws&#39;

解决方案

我的所有映射/配置仍然如上所述。我已升级到Hibernate 4.3.6,在TomEE 1.7下,现在可以使用了。

Hibernate: insert into enrg (nm, ns, tmcr, entp) values (?, ?, ?, 'ws')
Hibernate: insert into ws (exdr, enid) values (?, ?)

1 个答案:

答案 0 :(得分:1)

thisthis可以为您提供帮助。

最后,只有SINGLE_TABLE策略需要discriminator列,JOINED可以不使用JOINED。 Hibernate当前的问题是,当使用@DiscriminatorColumn映射的JPA继承中的子实体持久化时会导致数据不一致,即使JOINED规范建议在使用鉴别器时保留鉴别器值与{{1}}。