EJB3 - 处理非标准链接表

发布于 2024-07-18 22:18:35 字数 259 浏览 6 评论 0原文

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

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

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

谢谢,

I have a situation where I am working with EJB3 and a legacy database. I have a situation where there is a many-to-many relationship between two tables A and B, defined through a third (link) table L.

The complication is that the link table has other fields in it other than the PK's of tables A and B. The columns are standard timestamp and user columns to record who generated the link. These two additional columns are preventing me from defining the many-many relationship using a join table annotation, as they are not nillable and so must be populated.

Does anyone know of a way around this limitation? I could define One-to-many relationships from the link table to each of the other tables in the relationship, but this is not very elegant.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2024-07-25 22:18:35

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

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

Yes, it is but you need to make it elegant. The following super-class can be used to define arbitrary many-to-many relationship as an entity:

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

}

The example of entity based on this super class (fragment):

@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;
  }
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文