本文介绍了这是正确的(更好的)对象关系映射吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的被困在我的项目中使用Hibernate。我在这里描述了一些问题:我开始想知道我的代码是否正确。我正在研究一个巨大的项目,我必须自己定义带有注释的映射类。但是当问题开始出现时,我决定重新创建与项目分开的数据库的一部分,并尝试在IDE中生成实体。



我有两个表:我和选项。 我的有主键:列 qwerty 和属性。 属性是 Option 表中的外键。当然 Option 有属性作为主键。



<在我的解决方案中,我创建了 @Embeddable MyPK 类,它有两个属性: String qwerty 和字符串属性。在我的 My 实体中,我当然有 @EmbeddedId MyPK 和属性(与 MyPK 中的列名相同),但是这是 Option 对象,不是字符串,如 MyPK 。

  @ManyToOne 
@JoinColumn(name =property,nullable = false,insertable = false,updatable = false)
保护选项选项;

这是由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 {
私人字符串qwerty;

@ javax.persistence.Column(name =QWERTY)
@Id
public String getQwerty(){
return qwerty;
}

public void setQwerty(String qwerty){
this.qwerty = qwerty;
}

私人字符串文本;

@ javax.persistence.Column(name =TEXT)
@Basic
public String getText(){
return text;
}

public void setText(String text){
this.text = text;
}

私人字符串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(){
返回optionByProperty;
}

public void setOptionByProperty(OptionEntity optionByProperty){
this.optionByProperty = optionByProperty;


$ / code $ / pre
$ b

这是 MyEntityPK code>生成的类:

  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;
}

私有字符串属性;

@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> 而不是集合< 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;
}

私有字符串属性;

@ 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(){
返回myByProperty;
}

public void setMyByProperty(Collection< MyEntity> myByProperty){
this.myByProperty = myByProperty;
}
}

什么样的选择是最合适的问题?我已经描述过的或粘贴的那个?

解决方案

查看使用派生ID的JPA 2.0示例:

这似乎是你以后的样子。



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

I have really got stuck with using Hibernate in my project. Some issues I have described here: Hibernate: getting too many rows I have started wondering if my code is correct. I am working on a huge project and I have had to define mapping classes with annotations on my own. But when the problems have began to occur I have decided to recreate part of database separate to the project and try to generate entities in IDE.

I have two tables: My and Option. My has primary key: column qwerty and property. Propertyis the foreign key from Option table. And of course Option has property as a primary key.

In my solution I have created @Embeddable MyPK class with two properties: String qwerty and String property. In my My entity I have of course @EmbeddedId MyPK and also property (the same column name as in the MyPK) but is this Option object, not String as in the MyPK.

@ManyToOne
@JoinColumn(name = "property", nullable = false, insertable = false, updatable = false)
protected Option option;

This is entity generated by Hibernate Tools in Intellij Idea. There isn't EmbeddedId, but there is @IdClass. I have thought that @IdClass is only for basic types. But I have a object as a part of my primary key. However there is also OptionEntity object here. Is this correct to keep basic type and object type for one column?

@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;
    }
}

This is MyEntityPK generated class:

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 below. No special points aren't in this entity. I would like only @Version annotation on version property and also List<MyEntity> instead of 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;
    }
}

What option is the most proper and less problematic? The one that I have described or the one that pasted?

解决方案

Check out JPA 2.0 examples using derived Ids:http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifierswhich seem to be what you are after.

In JPA, you do not need an embeddedId for your ids, but if using a composite PK, you do need a class to hold the multiple values that make up the pk. Instances of this class are passed to the em.find method, and it can be either an EmbeddedId or a PKClass. I prefer using PKClass myself, but its up to you - using an embedded just places the fields within the embeddedable class, so you use the embedded object to set the mappings and access the values. If using a pkClass, you do not need the fields/properties annotated within it since they are accessed and mapped within the entity directly.

这篇关于这是正确的(更好的)对象关系映射吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:41