【问题标题】:Is this correct (better) object relational mapping?这是正确的(更好的)对象关系映射吗?
【发布时间】:2012-10-02 16:58:07
【问题描述】:

在我的项目中使用 Hibernate 真的很困难。我在这里描述的一些问题:Hibernate: getting too many rows 我开始怀疑我的代码是否正确。我正在做一个巨大的项目,我不得不自己定义带有注释的映射类。但是当问题开始出现时,我决定重新创建与项目分开的部分数据库并尝试在 IDE 中生成实体。

我有两张桌子:MyOptionMy 具有主键:列 qwertypropertyProperty 是来自 Option 表的外键。当然Optionproperty 作为主键。

在我的解决方案中,我创建了具有两个属性的 @Embeddable MyPK 类:String qwertyString property。在我的My 实体中,我当然有@EmbeddedId MyPKproperty(与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


    【解决方案1】:

    摆脱属性private String lang;/private String property; 它已经被多线程定义了。您也不需要 MyEntity 的主键的类。 MyEntity 可以是它自己的 id 类,其中两个属性 qwertyoption 作为它的 key-property,key-manytoone。

    【讨论】:

    • 我必须在这个灵魂中使用@Id注解?
    • 我对注释不是很流利,但我认为您需要在每个 keyproperty/reference 上加上 @Id
    【解决方案2】:

    查看使用派生 ID 的 JPA 2.0 示例: http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers 这似乎是你所追求的。

    在 JPA 中,您的 id 不需要 embeddedId,但如果使用复合 PK,您确实需要一个类来保存组成 pk 的多个值。此类的实例被传递给 em.find 方法,它可以是 EmbeddedId 或 PKClass。我更喜欢自己使用 PKClass,但这取决于您 - 使用嵌入式只是将字段放置在可嵌入类中,因此您可以使用嵌入式对象来设置映射并访问值。如果使用 pkClass,则不需要在其中注释的字段/属性,因为它们可以直接在实体中访问和映射。

    【讨论】:

    • 但我也在使用 Hibernate。所以这不是同一个问题,是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2011-04-24
    • 2016-03-04
    • 2013-05-27
    • 1970-01-01
    相关资源
    最近更新 更多