我真的被困在我的項目中使用Hibernate。我在這裏描述的一些問題:Hibernate: getting too many rows我開始想知道我的代碼是否正確。我正在研究一個巨大的項目,我必須自己定義帶有註釋的映射類。但是當問題開始出現時,我決定重新創建與該項目分開的部分數據庫,並嘗試在IDE中生成實體。這是否正確(更好)的對象關係映射?
我有兩個表:My
和Option
。 My
有主鍵:列qwerty
和property
。 Property
是來自Option
表的外鍵。當然Option
有property
作爲主鍵。
在我的解決方案中,我創建了@Embeddable
MyPK
類,具有兩個屬性:String qwerty
和String property
。在我的My
實體當然我有@EmbeddedId
MyPK
和property
(與MyPK
相同的列名稱),但是這是Option
對象,而不是在MyPK
中的字符串。
@ManyToOne
@JoinColumn(name = "property", nullable = false, insertable = false, updatable = false)
protected Option option;
這是由Intellij Idea中的Hibernate Tools生成的實體。沒有EmbeddedId
,但有@IdClass
。我以爲@IdClass
只適用於基本類型。但我有一個對象作爲我的主鍵的一部分。但是這裏也有OptionEntity
對象。這是正確的保持一列的基本類型和對象類型?
@javax.persistence.IdClass(test.go.MyEntityPK.class)
@javax.persistence.Table(name = "MY", schema = "PUBLIC", catalog = "PUBLIC")
@Entity
public class MyEntity {
private String qwerty;
@javax.persistence.Column(name = "QWERTY")
@Id
public String getQwerty() {
return qwerty;
}
public void setQwerty(String qwerty) {
this.qwerty = qwerty;
}
private String text;
@javax.persistence.Column(name = "TEXT")
@Basic
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
private String lang;
@javax.persistence.Column(name = "PROPERTY")
@Id
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property= property;
}
@Override
public boolean equals(Object o) {
//equals
}
@Override
public int hashCode() {
//hashCode
}
private OptionEntity optionByProperty;
@ManyToOne
@javax.persistence.JoinColumn(name = "PROPERTY", referencedColumnName = "PROPERTY", nullable = false)
public OptionEntity getOptionByProperty() {
return optionByProperty;
}
public void setOptionByProperty(OptionEntity optionByProperty) {
this.optionByProperty = optionByProperty;
}
}
這是MyEntityPK
生成的類:
public class MyEntityPK implements Serializable {
private String qwerty;
@Id
@Column(name = "qwerty")
public String getQwerty() {
return qwerty;
}
public void setQwerty(String qwerty) {
this.qwerty = qwerty;
}
private String property;
@Id
@Column(name = "PROPERTY")
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
@Override
public boolean equals(Object o) {
//equals
}
@Override
public int hashCode() {
//hashCode
}
}
下面OptionEntity
。這個實體沒有特別的要點。我只想@Version
註釋version
財產和List<MyEntity>
而不是Collection<MyEntity>
。
@javax.persistence.Table(name = "OPTION", schema = "PUBLIC", catalog = "PUBLIC")
@Entity
public class OptionEntity {
private Long version;
@javax.persistence.Column(name = "VERSION")
@Basic
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
private String property;
@javax.persistence.Column(name = "PROPERTY")
@Id
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
@Override
public boolean equals(Object o) {
//equals
}
@Override
public int hashCode() {
//hashcode
}
private Collection<MyEntity> myByProperty;
@OneToMany(mappedBy = "optionByProperty")
public Collection<MyEntity> getMyByProperty() {
return myByProperty;
}
public void setMyByProperty(Collection<MyEntity> myByProperty) {
this.myByProperty = myByProperty;
}
}
什麼樣的選擇是最合適的,問題更少?我描述過的還是粘貼的那個?
我必須在這個靈魂'@ Id'註釋中使用嗎? – woyaru
我不是那麼流利的註釋,但我認爲你需要在每個keyproperty/reference上加上@ @ Id – Firo