EJB3 - 处理非标准链接表

时间:2009-04-29 13:58:37

标签: java jpa ejb-3.0 java-ee-5

我遇到的情况是我正在使用EJB3和遗留数据库。我有一种情况,即两个表A和B之间存在多对多关系,通过第三个(链接)表L定义。

复杂的是,除了表A和B的PK之外,链接表中还包含其他字段。列是标准时间戳和用户列,用于记录生成链接的人员。这两个额外的列阻止我使用连接表注释定义多对多关系,因为它们不是可填充的,因此必须填充。

有没有人知道解决这个限制的方法?我可以定义从链接表到关系中每个其他表的一对多关系,但这不是很优雅。

谢谢,

1 个答案:

答案 0 :(得分:1)

是的,但是你需要让它变得优雅。以下超类可用于将任意多对多关系定义为实体:

@MappedSuperclass
public abstract class ModelBaseRelationship {

    @Embeddable
    public static class Id implements Serializable {

        public Long entityId1;
        public Long entityId2;

        @Column(name = "ENTITY1_ID")
        public Long getEntityId1() {
            return entityId1;
        }

        @Column(name = "ENTITY2_ID")
        public Long getEntityId2() {
            return entityId2;
        }

        public Id() {
        }

        public Id(Long entityId1, Long entityId2) {
            this.entityId1 = entityId1;
            this.entityId2 = entityId2;
        }

        @Override
        public boolean equals(Object other) {
            if (other == null)
                return false;
            if (this == other)
                return true;
            if (!(other instanceof Id))
                return false;
            final Id that = (Id) other;
            return new EqualsBuilder().append(this.entityId1, that.getEntityId1()).append(this.entityId1, that.getEntityId2()).isEquals();
        }

        @Override
        public int hashCode() {
            return new HashCodeBuilder(11, 111).append(this.entityId1).append(this.entityId2).toHashCode();
        }

        protected void setEntityId1(Long theEntityId1) {
            entityId1 = theEntityId1;
        }

        protected void setEntityId2(Long theEntityId2) {
            entityId2 = theEntityId2;
        }
    }

    protected Id id = new Id();

    public ModelBaseRelationship() {
        super();
    }

    public ModelBaseRelationship(ModelBaseEntity entity1, ModelBaseEntity entity2) {
        this();
        this.id.entityId1 = entity1.getId();
        this.id.entityId2 = entity2.getId();
        setVersion(0);
    }

    @EmbeddedId
    public Id getId() {
        return id;
    }

    protected void setId(Id theId) {
        id = theId;
    }

}

基于此超类(片段)的实体示例:

@Entity(name = "myRealEntity")
@Table(name = "REAL_TABLE_NAME", uniqueConstraints = { @UniqueConstraint(columnNames = {
 "FIRST_FK_ID", "SECOND_FK_ID" }) })
@AttributeOverrides( {
@AttributeOverride(name = "entityId1", column = @Column(name = "FIRST_FK_ID")),
@AttributeOverride(name = "entityId2", column = @Column(name = "SECOND_FK_ID"))    
})
public class ModelBaseRelationshipReferenceImpl extends ModelBaseRelationship {

  private Entity1OfManyToManyRelationship entity1;
  private Entity2OfManyToManyRelationship entity2;
  ...
  @ManyToOne
  @JoinColumn(name = "FIRST_FK_ID", insertable = false, updatable = false)
  public Entity1OfManyToManyRelationship getEntity1OfManyToManyRelationship() {
    return entity1;
  }

  @ManyToOne
  @JoinColumn(name = "SECOND_FK_ID", insertable = false, updatable = false)
  public Entity2OfManyToManyRelationship getEntity2OfManyToManyRelationship () {
    return entity2;
  }
...
}