本文介绍了在EBean中使用复合键读取注释时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

遵循此链接

我想使用OneToMany而不是ManyToMany批注,使用Ebean在其中包含带有复合键的中产阶级.我有这个错误:

I would like to use OneToMany instead ManyToMany annotation, having middle class with composite key in it using Ebean. I have this error:

这是我的SoftwareTagPk类:

This is my SoftwareTagPk class:

@Embeddable
public class SoftwareTagPk  implements Serializable {

    @ManyToOne
    private Tag tag;

    @ManyToOne
    private Software software;

    ...
}

和SoftwareTag类:

And SoftwareTag class:

@Entity
public class SoftwareTag extends Model {

    @EmbeddedId
    private SoftwareTagPk pk = new SoftwareTagPk();

    @Transient
    public Tag getTag() {
        return pk.getTag();
    }

    public void setTag(Tag aTag) {
        pk.setTag(aTag);
    }

    @Transient
    public Software getSoftware() {
        return pk.getSoftware();
    }

    public void setSoftware(Software aSoftware) {
        pk.setSoftware(aSoftware);
    }
}

也在日志中:

如何解决?

推荐答案

要使此代码正常工作,您必须执行以下操作:

To make this code work you have to do:

  1. 在您的SoftwareTagPk类中,仅添加标签和软件的ID
  2. @ManyToOne关系移动到SoftwareTag
  3. 添加属性updatableinsertable设置为false的@JoinColumn注释.
  4. 覆盖SoftwareTag类中的设置器setTagsetSoftware.在这些设置器中,您将把ID重写为复合键.
  1. In your SoftwareTagPk class put only id's of Tag and Software
  2. Move @ManyToOne relations to SoftwareTag class
  3. Add @JoinColumn annotations with attributes updatable and insertable set to false.
  4. Override setters setTag and setSoftware in SoftwareTag class. In these setters you will rewrite id's to composite key.

此解决方案的主要思想是SoftwareTag具有复合键和@ManyToOne关系,它们被映射到相同的列.

Main idea of this solution is that SoftwareTag has composite key and @ManyToOne relations and they are mapped to the same collumns.

这是代码:

Tag.java

@Entity
public class Tag extends Model {
    @Id
    private Integer id;

    @OneToMany(mappedBy="tag")
    public List<SoftwareTag> softwareTags;

    public Integer getId() {
        return id;
    }

    public void setId(Integer aId) {
        id=aId;
    }

    public static Finder<Integer,Tag> find = new Finder<Integer,Tag>(
        Integer.class, Tag.class
    );
}

Software.java

Software.java

@Entity
public class Software  extends Model {
    @Id
    private Integer id;

    @OneToMany(mappedBy="software")
    public List<SoftwareTag> softwareTags;

    public Integer getId() {
        return id;
    }

    public void setId(Integer aId) {
        id=aId;
    }
}

SoftwareTag.java

SoftwareTag.java

@Entity
public class SoftwareTag extends Model {

    SoftwareTag() {
        pk = new SoftwareTagPk();
    }

    @EmbeddedId
    private SoftwareTagPk pk = new SoftwareTagPk();

    @ManyToOne
    @JoinColumn(name = "tag_id", insertable = false, updatable = false)
    private Tag tag;

    @ManyToOne
    @JoinColumn(name = "software_id", insertable = false, updatable = false)
    private Software software;

    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag aTag) {
        tag = aTag;
        pk.tag_id = aTag.getId();
    }

    public Software getSoftware() {
        return software;
    }

    public void setSoftware(Software aSoftware) {
        software = aSoftware;
        pk.software_id = aSoftware.getId();
    }
}

SoftwareTagPk.java

SoftwareTagPk.java

@Embeddable
public class SoftwareTagPk implements Serializable {

    public Integer tag_id;

    public Integer software_id;

    @Override
    public int hashCode() {
        return tag_id + software_id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        SoftwareTagPk pk = (SoftwareTagPk)obj;
        if(pk == null)
            return false;
        if (pk.tag_id.equals(tag_id) && pk.software_id.equals(software_id)) {
            return true;
        }
        return false;
    }
}

这篇关于在EBean中使用复合键读取注释时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 19:25