2015-05-12 73 views
0

- Facts ....
在我的項目中,爲了提供多站點部署,每個類/表都應該有一對(站點,結構)。使用Hibernate映射組合鍵在Oracle中產生一個原始字段

我提供了一個安裝類

@Entity 
@Table(name="COM_INSTALLATION") 
@IdClass(InstallationPK.class) 
public class Installation implements Serializable{ 

private static final long serialVersionUID = 3601006283578715263L; 

@Id 
@Column(name = "site") 
private String site; 
@Id 
@Column(name = "structure") 
private String structure; 

/* ... constructor, getters, setters ... */ 
} 

及其PK,作爲

public class InstallationPK implements Serializable { 

private static final long serialVersionUID = 1L; 
private String site; 
private String structure; 
/* .. constructor, getters, setters ... */ 
} 

然後我有一個名爲BaseEntity MappedSuperclass

@MappedSuperclass 
public class BaseEntity 
{ 
private Installation installation; 

@OneToOne 
@JoinColumns({  
    @JoinColumn(name = "site", referencedColumnName = "site"), 
    @JoinColumn(name = "structure", referencedColumnName = "structure") 
}) 
public Installation getInstallation() { 
    return installation; 
} 
public void setInstallation(Installation installation) { 
    this.installation = installation; 
} 
} 

很容易那麼遠,每@Entity和@Table註釋類擴展BaseEntity包含一個SITE列,一個STRUCTURE列和一個相關的fo統治關鍵。 enter image description here

- 的問題 ....
碰巧有一個類實現org.springframework.security.core.userdetails.UserDetails和擴展BaseEntity,Hibernate不創建兩個列,但在安裝與RAW類型的列。這就是類

@Entity 
@Table(name="USR_USER", uniqueConstraints =  
@UniqueConstraint(columnNames = { "username" })) 
@SequenceGenerator(name = "userId_seq_generator", sequenceName = "S_USER") 
public class User extends BaseEntity implements UserDetails{ 

private static final long serialVersionUID = 8698408700004341649L; 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO, generator = "userId_seq_generator") 
private Long userId; 
/* ... getter, setters, ... */ 
} 

,這就是結果 enter image description here

任何想法?在此先感謝

+1

你的領域和干將混合註釋。不要這樣做。始終如一。 OneToOne可能被忽略,因爲它位於getter上,而@Id註釋位於字段上。 –

+0

謝謝,我糾正了這個課程,但這不是重點......即使是因爲它與其他案例一起工作 – davidetrapani

+0

ops ......這確實是重點! – davidetrapani

回答

0

我沒有一點信譽發表評論,所以我會把我在這裏的評論:

爲什麼instalationPK字段沒有instalation.class? 您應該將@EmbeddedId放在Installation.class的字段instalationPK中。

我不知道如果讓區別,而是把joinCollums場上的BaseEntity

@OneToOne 
@JoinColumns({  
    @JoinColumn(name = "site", referencedColumnName = "site"), 
    @JoinColumn(name = "structure", referencedColumnName = "structure") 
}) 
private Installation installation; 
+0

我沒有使用@EmbeddedId,因爲安裝類具有@IdClass(InstallationPK.class) – davidetrapani

+0

無論如何...它在擴展BaseEntity的所有其他類中工作...我相信它與UserDetails接口有關(或者實現接口...) – davidetrapani

+0

我還沒有看到@idClass ...把​​'UserDetails'接口。其他實體,他們使用其他接口?這是什麼? –