2009-04-29 44 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