我有以下類別:Hibernate的JPA創建每類表InheritanceType.JOINED
AbstractEntity,這是超一流的我所有的實體並存儲所有實體使用的公共領域,如ID:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
...
}
AbstractUser,這是超類的所有用戶實體(管理,標準等),並存儲字段通用於所有的用戶帳戶,如登錄名和密碼等:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AbstractUser extends AbstractEntity implements Serializable
實施例非抽象用戶類的,管理用戶:
@Entity
public class AdminUser extends AbstractUser implements Serializable {
我想什麼有是,我有一個表用於AbstractUser類,它包含在AbstractUser類僅列,然後爲每個包含所有類特定值的子類ONE表。我認爲AbstractUser中的@Inheritance(strategy = InheritanceType.JOINED)註釋應該這樣做。然而,Hibernate爲每個類(AbstractUser和擴展它的所有類)提供一個表,而非抽象類的表包含抽象類的所有列。
當我堅持擴展AbstractUser的類之一的對象時,所有值都寫入更具體的表,即AbstractUser表保持爲空。
澄清:
說我有一個AbstractUser類和延伸AbstractUser ADMINUSER和StandardUser類。然後我仍存在與ID 1和名字「A」和一個StandardUser ID爲2一個ADMINUSER對象,名稱「B」和賬戶號碼「001」,我將結束了這樣的:
AbstractUser:
| ID | NAME |
empty table
ADMINUSER:
| ID | NAME |
| 1 | A |
StandardUser:
| ID | NAME | AccountNum |
| 2 | B | 001 |
我想獲得的是:
AbstractUser:
| ID | NAME |
| 1 | A |
| 2 | B |
ADMINUSER:
| ForeignKey |
| 1 |
StandardUser:
| ForeignKey | AccountNum |
| 2 | 001 |
我使用的註釋有什麼問題?我怎樣才能做到這一點?
爲了以防萬一,我的堅持。XML:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="cfadbPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/cfadbDS</jta-data-source>
<properties>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/cfadb"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="*****"/>
<property name="hibernate.connection.password" value="*****"/>
<property name="hibernate.default_schema" value="public"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
UPDATE:
我不得不通過服務器日誌一看,看到這些消息:
WARN [org.hibernate.cfg.AnnotationBinder] (ServerService Thread Pool -- 187) HHH000138: Mixing inheritance strategy in a entity hierarchy is not allowed, ignoring sub strategy in: entities.users.AbstractUser
WARN [org.hibernate.cfg.AnnotationBinder] (ServerService Thread Pool -- 187) HHH000137: Root entity should not hold an PrimaryKeyJoinColum(s), will be ignored
WARN [org.hibernate.cfg.AnnotationBinder] (ServerService Thread Pool -- 187) HHH000137: Root entity should not hold an PrimaryKeyJoinColum(s), will be ignored
是錯誤混合繼承策略那裏,因爲我第一次對AbstractEntity有TABLE_PER_CLASS,然後對AbstractUser進行JOINED?如果這是錯誤的原因,爲什麼不允許混合繼承類型?我不明白這可能會導致什麼問題?
這是一個混合繼承,因此是供應商特定的。你是否與其他持久性提供者進行交叉檢查? – wypieprz 2015-03-24 22:25:04