我相信我有這個工作,我必須做一些事情。希望有人能馬上看到問題。如何映射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>
什麼我需要做的,有休眠包括在插入的enrg ENTP列/價值?在這種情況下,entp值應該是'ws'。
解決方案
我所有的映射/配置保持,如上。我已經升級到Hibernate 4.3.6,並且在TomEE 1.7下現在可以工作。
Hibernate: insert into enrg (nm, ns, tmcr, entp) values (?, ?, ?, 'ws')
Hibernate: insert into ws (exdr, enid) values (?, ?)
如果Hibernate不支持JOINED策略上的鑑別器,那麼我相信還有很多其他的JPA提供者可以這樣做。 – 2014-09-01 07:00:18