Hibernate级联和外键问题

时间:2015-06-25 14:42:19

标签: java hibernate hibernate-mapping cascade

我正在学习hibernate级联来为我们的应用程序实现它。应用程序的SQL Server数据库设计基于一些具有弱/不正确FK关系的传统数据库模型。在使用Hibernate Cascade从相关表中获取数据时,我无法映射实体。

考虑一下情景:

TABLE_A:
ColA1 and ColA2 makes composite key

(int)   (int)   (string)
ColA1   ColA2   ColA3
1       1       dummy111
1       2       dummy222
2       1       dummy333
2       2       dummy444

Table_B
ColB1 and ColB2 makes composite key

(int)   (int)
ColB1   ColB2
1       1
1       2
2       1
2       2

Table_C
ColC1 and ColC2 makes composite key

(int)   (int)   (int)   (str)   (str)
ColC1   ColC2   ColC3   ColC4   ColC5
1       1       1       AA      dummy999
1       2       1       BB      dummy888
1       2       2       AA      dummy777

Colc1 and ColC3 makes foreign key with (ColA1 and ColA2 for Table_A when ColC4=AA) or (ColB1 and ColB2 for Table_B when ColC4=BB). 
This FK is not defined in table or schema but is just written on documents and is implemented across all stored procedures currently used in the application.  
So if you notice Table C, ColC1 and ColC3 can have same values (ex, 1,1) and are not unique values that properly make a FK relation. And ColC4 is not present in Table_A or Table_B.

三个实体:

@Entity
@Table(name="A")
public class AEntity implements Serializable {

@EmbeddedId
private AEntityPK id; //Composite key of ColA1 and ColA2 


//bi-directional many-to-one association to CEntity
@OneToMany(mappedBy="centity", cascade = {CascadeType.ALL})
private List<WmktEmpSbadWd> centityList;

}


@Entity
@Table(name="B")
public class BEntity implements Serializable {


@EmbeddedId
private BAEntityPK id; //Composite key of ColB1 and ColB2 


//bi-directional many-to-one association to CEntity
@OneToMany(mappedBy="centity", cascade = {CascadeType.ALL})
private List<WmktEmpSbadWd> centityList;

}


@Entity
@Table(name="C")
public class CEntity implements Serializable {

@EmbeddedId
private CAEntityPK id; //Composite key of ColC1 and ColC2 


//bi-directional many-to-one association to AEntity
@ManyToOne
@JoinColumns({
    @JoinColumn(name="ColA1", referencedColumnName="ColC1", nullable=false, insertable=false, updatable=false),
    @JoinColumn(name="ColA2", referencedColumnName="ColC3", nullable=false, insertable=false, updatable=false),
    })
private AEntity aentityObj;



//bi-directional many-to-one association to BEntity
@ManyToOne
@JoinColumns({
    @JoinColumn(name="ColB1", referencedColumnName="ColC1", nullable=false, insertable=false, updatable=false),
    @JoinColumn(name="ColB2", referencedColumnName="ColC3", nullable=false, insertable=false, updatable=false),
    })
private BEntity bentityObj;



}

Sample Test Class to fetch AEntoty and its related data from CEntity:

public class TestFetchEntityA{
public static void main(){
.....
Transaction tx = session.beginTransaction();
aentityList = session.createQuery("FROM AEntity WHERE ColA1=1 and ColA2=1").list();

}

}

执行main()从AEntity的centityList中获取CEntity类的两个对象,因为我还没有在Table_C中为FK定义的映射中定义条件(ColC4 =&#34; AA&#34;)。

那么在映射AEntity和CEntity的同时我必须添加条件(ColC4 =&#34; AA&#34;)该怎么办?

注意:在实际应用程序中,有20-30个表,其中包含大量行,根据上述情况使用5-6个表进行映射。

我尝试过以下但没有工作:

  1. 在createquery()中加入AEntity和CEntity,它只从这两个表中获取值。但我有多个实体要级联,当我使用join时会忽略这些实体。

  2. 使用@JoinFormula,我看到可以根据条件确定列,而不是映射本身。

0 个答案:

没有答案