我有2个班级
具有以下结构:
public class Verification implements Serializable {
private Long verificationId;
private Long salesManCode;
private Long clientCode;
...
}
public class Contract implements Serializable {
private Long contractId;
private Long salesManCode;
private Long clientCode;
...
}
这些类有各自的hbm.xml
映射,在数据库模型中表没有关联,在hibernate映射中都没有,但是在保存契约时,它必须具有相同的verificationId
y contractId
字段(业务规则),但有些情况下也没有验证合同。
verification
verificationId | salesManCode | clientCode
1050 1001 2056
1051 1001 2248
1054 1002 2856
contract
contractId | salesManCode |clientCode
1050 1001 2056 <- this contract have verification
1051 1001 2248 <- this contract have verification
1052 1025 2822 <- this contract not have verification
1053 1254 1547 <- this contract not have verification
1054 1002 2856 <- this contract have verification
我的问题是当我运行HQL查询时:
select con.salesManCode, ver.salesManCode, con.clientCode, ver.clientCode, con.contracId, ver.verificationId
from Verification ver, Contract con
where ver.verificationId = con.contractId
但是Hibernate使用交叉连接句子进行翻译并组合所有记录。
是否有任何方法可以在hbm.xml
文件中的不相关类之间执行HQL查询?
RULE:不应该映射实体。
答案 0 :(得分:0)
您可以按如下方式映射两个表之间的连接关系:
@Entity
public class Verification implements Serializable {
private Long verificationId;
private Long salesManCode;
private Long clientCode;
...
}
@Entity
public class Contract implements Serializable {
private Long contractId;
@MapsId
@OneToOne
@JoinColumn(name = "contractId", referencedColumnName = "verificationId")
private Verification verification;
private Long salesManCode;
private Long clientCode;
}
您的查询变为:
select con.salesManCode, ver.salesManCode, con.clientCode, ver.clientCode, con.contracId, ver.verificationId
from Contract con
join con.verification ver
因此,您可以获得内部联接而不是CROSS JOIN。