本文介绍了Ebean EmbeddedId映射列到ManyToOne关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

尝试创建组合键(EmbeddedId)时,我遇到了Ebean的问题.

I'm facing a problem with Ebean when trying to create Composed Keys (EmbeddedId).

这是我所拥有的草案:

@Entity public class EntityA extends Model{
    @Id
    private String ID;

    @OneToMany
    private List<EntityB> listEntitesB;

    public EntityA(){
        ID = UUID.randomUUID();
    }
}

@Entity public class EntityB extends Model{

    @EmbeddedId
    private EntityB_PK ID;

    @ManyToOne
    @JoinColumn(name="entityA_fk", referencedColumnName="listEntitiesB")
    private EntityA entityA;

    public EntityB(String entityB_ID){
         ID = new EntityB_PK(UUID.randomUUID(), entityB_ID);
    }
}

@Embeddable public class EntityB_PK{
    private String entityB_ID;
    private String entityA_FK_ID;

    public EntityB_PK(String entB_ID, String entA_FK_ID){
       entityB_ID = entB_ID;
       entityA_FK_ID = entA_FK_ID;
    }
}

注意:我正在将Ebean 4.1.4与Java 1.6结合使用.

Note: I'm using Ebean 4.1.4 with Java 1.6.

因此,此代码运行良好,但存在一个我正在尝试解决的问题-数据库中的结果表如下所示:

So, this code works well, but there is a perk, which I'm trying to solve - the resultant table in the database looks like this:

  • entityB_ID [主要]
  • entityA_FK_ID [主要]
  • entityA_fk

如您所见,考虑到"entityA_FK_ID"列,最后一列是多余的.

As you can see that last column is redundant considering the "entityA_FK_ID" column.

我想要什么?

  • 我希望能够告诉Ebean为关联@ManyToOne使用列"entityA_FK_ID",而不是创建它自己的列.

推荐答案

要解决此问题,我们必须:

To solve this problem we have to:

  1. 将EntityB.entityA映射到与EntityB.ID.entityA_FK_ID相同的列
  2. 设置EntityB.entityA @JoinColumn批注的可插入"和可更新"属性
  3. 重写EntityB.entityA的设置器

这是代码:

EntityA.java:

EntityA.java:

@Entity
public class EntityA extends Model {
    @Id
    private String ID;

    @OneToMany(mappedBy="entityA")
    public List<EntityB> listEntitesB;

    public static Finder<String,EntityA> find = new Finder<String,EntityA>(
        String.class, EntityA.class
    );

    public EntityA() {
        ID = UUID.randomUUID().toString();
    }

    public String getID() {
        return ID;
    }
}

EntityB.java:

EntityB.java:

@Entity
public class EntityB extends Model {

    @EmbeddedId
    private EntityB_PK ID;

    @ManyToOne
    @JoinColumn(name = "entityA_fk_id", insertable = false, updatable = false)
    private EntityA entityA;

    public EntityA getEntityA() {
        return entityA;
    }

    public void setEntityA(EntityA aEntityA) {
        entityA = aEntityA;
        ID.entityA_FK_ID = aEntityA.getID();
    }

    public EntityB(String entityB_ID){
         ID = new EntityB_PK(UUID.randomUUID().toString(), entityB_ID);
    }

    public String getID() {
        return ID.entityB_ID;
    }
}

EntityB_PK.java:

EntityB_PK.java:

@Embeddable
public class EntityB_PK implements Serializable {

    public String entityB_ID;

    @Column(name="entityA_fk_id")
    public String entityA_FK_ID;

    public EntityB_PK(String entB_ID, String entA_FK_ID){
       entityB_ID = entB_ID;
       entityA_FK_ID = entA_FK_ID;
    }

    @Override
    public int hashCode() {
        return entityB_ID.length() + entityA_FK_ID.length();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        EntityB_PK b = (EntityB_PK)obj;
        if(b==null)
            return false;
        if (b.entityB_ID.equals(entityB_ID) && b.entityA_FK_ID.equals(entityA_FK_ID)) {
            return true;
        }
        return false;
    }
}

这篇关于Ebean EmbeddedId映射列到ManyToOne关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 19:25