2009-04-29 64 views
0

我遇到了使用EJB3和旧数据库的情况。我有一种情况,即通过第三个(链接)表L定义的两个表A和B之间存在多对多的关系。EJB3 - 处理非标准链接表

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

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

谢谢,

回答

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; 
    } 
... 
} 
+0

您好格里戈里米,感谢您的回复,但我不确定我在这里正确关注您。这实际上给你什么?目前它使用A和B上的@ManyToOne批注和L上的OneToMany - 我宁愿摆脱L(作为实体)的需要。我不确定你的超类在这里实现了什么。 – 2009-04-30 09:06:33