【发布时间】:2012-10-02 16:58:07
【问题描述】:
在我的项目中使用 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 工具生成的实体。没有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 属性和 List<MyEntity> 上注解 @Version 而不是 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;
}
}
哪个选项最合适且问题较少?是我描述的还是贴的?
【问题讨论】:
标签: java hibernate jpa persistence