注意:由于提供的评论,我已更改提供的详细信息。新的技术细节实际上是我正在尝试运行的代码。
目前我的一个OneToMany / ManyToOne双向关系存在一个奇怪的问题。
在父表中,我有一个long,它是标识符,除了另一个字段之外,子节点还有该列,它组成了它的复合键。
在我查询的示例数据中,我在子表中有1条父记录和3条相关记录。目前,当我使用criteria对象查询表时,我得到3个父(CoPolicyPointer)记录,其中2个没有链接的关联子(CoTransactionSummary)表,然后是正确填充的第三个。我原以为只会得到一个结果,这就是为什么我感到困惑。你知道为什么会这样吗?
我在下面提供了更多技术数据。
Parent Table (CoPolicyPointer) Column Data Type SystemAssignId String (Primary Key) PolicyPrefixCd String PolicyId String PolicySeqNo int Child Table (CoTransaction Summary) Column Data Type SystemAssignId String (composite key) TransSeqId int (composite key) TransType varchar(4)
Criteria语句如下所示:
Criteria criteria = session.createCriteria(classToUse);
criteria.createAlias("coTransactionSummaries", "ts");
criteria.add(Restrictions.ne("ts.transInProcessCd", "VIEW"));
criteria.add(Restrictions.gt("ts.transSeqId", Short.valueOf("0")));
criteria.add(Restrictions.eq(BaseCoPolicyPointer.COL_POLICY_PREFIX_CD,
policyNumber.getPrefix().getLiteral()));
criteria.add(Restrictions.eq(BaseCoPolicyPointer.COL_POLICY_ID,
policyNumber.getId()));
Parent类看起来像:
@Entity(name = "CoPolicyPointer")
public class CoPolicyPointerRedo {
private static final String CLASS_NAME = CoPolicyPointerRedo.class
.getName();
private static final Logger logger = Logger.getLogger(CLASS_NAME);
private String systemAssignId;
private String policyPrefixCd;
private String policyId;
private String policyNumber;
private Short policySeqNo;
private Set coTransactionSummaries;
@Id
public String getSystemAssignId() {
return systemAssignId;
}
public void setSystemAssignId(String systemAssignId) {
this.systemAssignId = systemAssignId;
}
@Column
public String getPolicyId() {
return policyId;
}
public void setPolicyId(String policyId) {
this.policyId = policyId;
}
@Column
public String getPolicyPrefixCd() {
return policyPrefixCd;
}
public void setPolicyPrefixCd(String policyPrefixCd) {
this.policyPrefixCd = policyPrefixCd;
}
@Column
public Short getPolicySeqNo() {
return policySeqNo;
}
public void setPolicySeqNo(Short policySeqNo) {
this.policySeqNo = policySeqNo;
}
@OneToMany(targetEntity = CoTransactionSummaryRedo.class, mappedBy = "coPolicyPointer")
public Set getCoTransactionSummaries() {
return coTransactionSummaries;
}
public void setCoTransactionSummaries(Set coTransactionSummaries) {
this.coTransactionSummaries = coTransactionSummaries;
}
}
子类看起来像:
@Entity(name = "CoTransactionSummary")
public class CoTransactionSummaryRedo {
private static final String CLASS_NAME = CoTransactionSummaryRedo.class
.getName();
private static final Logger logger = Logger.getLogger(CLASS_NAME);
private String transTypeCd;
private String transInProcessCd;
private CoPolicyPointerRedo coPolicyPointer;
private CoTransactionSummaryIdRedo id;
@EmbeddedId
public CoTransactionSummaryIdRedo getId() {
return id;
}
public void setId(CoTransactionSummaryIdRedo id) {
this.id = id;
}
@Column
public String getTransInProcessCd() {
return transInProcessCd;
}
public void setTransInProcessCd(String transInProcessCd) {
this.transInProcessCd = transInProcessCd;
}
@Column
public String getTransTypeCd() {
return transTypeCd;
}
public void setTransTypeCd(String transTypeCd) {
this.transTypeCd = transTypeCd;
}
@ManyToOne()
@JoinColumn(name = "systemAssignId", insertable = false, updatable = false)
public CoPolicyPointerRedo getCoPolicyPointer() {
return coPolicyPointer;
}
public void setCoPolicyPointer(CoPolicyPointerRedo coPolicyPointer) {
this.coPolicyPointer = coPolicyPointer;
}
}
子类的ID类如下所示:
@Embeddable
public class CoTransactionSummaryIdRedo implements java.io.Serializable {
private String systemAssignId;
private short transSeqId;
public CoTransactionSummaryIdRedo() {
}
public CoTransactionSummaryIdRedo(String systemAssignId, short transSeqId) {
this.systemAssignId = systemAssignId;
this.transSeqId = transSeqId;
}
@Column
public String getSystemAssignId() {
return systemAssignId;
}
public void setSystemAssignId(String systemAssignId) {
this.systemAssignId = systemAssignId;
}
@Column
public short getTransSeqId() {
return transSeqId;
}
public void setTransSeqId(short transSeqId) {
this.transSeqId = transSeqId;
}
@Override
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof CoTransactionSummaryIdRedo))
return false;
CoTransactionSummaryIdRedo castOther = (CoTransactionSummaryIdRedo) other;
return ((this.getSystemAssignId() == castOther.getSystemAssignId()) || (this
.getSystemAssignId() != null
&& castOther.getSystemAssignId() != null && this
.getSystemAssignId().equals(castOther.getSystemAssignId())))
&& (this.getTransSeqId() == castOther.getTransSeqId());
}
@Override
public int hashCode() {
int result = 17;
result = 37
* result
+ (getSystemAssignId() == null ? 0 : this.getSystemAssignId()
.hashCode());
result = 37 * result + this.getTransSeqId();
return result;
}
}
我已经开启了日志记录,这就是正在生成的内容。
08:20:47,907 DEBUG SQL:111 - select this_.systemAssignId as systemAs1_1_2_, this_.policyId as policyId1_2_, this_.policyPrefixCd as policyPr3_1_2_, this_.policySeqNo as policySe4_1_2_, ts1_.systemAssignId as systemAs1_0_0_, ts1_.transSeqId as transSeqId0_0_, ts1_.transInProcessCd as transInP3_0_0_, ts1_.transTypeCd as transTyp4_0_0_, copolicypo4_.systemAssignId as systemAs1_1_1_, copolicypo4_.policyId as policyId1_1_, copolicypo4_.policyPrefixCd as policyPr3_1_1_, copolicypo4_.policySeqNo as policySe4_1_1_ from CoPolicyPointer this_ inner join CoTransactionSummary ts1_ on this_.systemAssignId=ts1_.systemAssignId left outer join CoPolicyPointer copolicypo4_ on ts1_.systemAssignId=copolicypo4_.systemAssignId where this_.policyPrefixCd=? and this_.policyId=? and this_.policySeqNo=? Hibernate: select this_.systemAssignId as systemAs1_1_2_, this_.policyId as policyId1_2_, this_.policyPrefixCd as policyPr3_1_2_, this_.policySeqNo as policySe4_1_2_, ts1_.systemAssignId as systemAs1_0_0_, ts1_.transSeqId as transSeqId0_0_, ts1_.transInProcessCd as transInP3_0_0_, ts1_.transTypeCd as transTyp4_0_0_, copolicypo4_.systemAssignId as systemAs1_1_1_, copolicypo4_.policyId as policyId1_1_, copolicypo4_.policyPrefixCd as policyPr3_1_1_, copolicypo4_.policySeqNo as policySe4_1_1_ from CoPolicyPointer this_ inner join CoTransactionSummary ts1_ on this_.systemAssignId=ts1_.systemAssignId left outer join CoPolicyPointer copolicypo4_ on ts1_.systemAssignId=copolicypo4_.systemAssignId where this_.policyPrefixCd=? and this_.policyId=? and this_.policySeqNo=? 19:14:30,938 DEBUG AbstractBatcher:513 - preparing statement 19:14:30,969 DEBUG StringType:151 - binding 'WA' to parameter: 1 19:14:30,969 DEBUG StringType:151 - binding '0100036449' to parameter: 2 19:14:30,969 DEBUG ShortType:151 - binding '0' to parameter: 3 19:14:30,969 DEBUG AbstractBatcher:426 - about to open ResultSet (open ResultSets: 0, globally: 0) 19:14:30,969 DEBUG Loader:717 - processing result set 19:14:30,969 DEBUG Loader:722 - result set row: 0 19:14:30,969 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_0_0_ 19:14:30,969 DEBUG ShortType:193 - returning '0' as column: transSeqId0_0_ 19:14:30,969 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_1_1_ 19:14:30,969 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_1_2_ 19:14:30,969 DEBUG Loader:1197 - result row: EntityKey[CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=0, systemAssignId=00502000000000}], EntityKey[CoPolicyPointerRedo#00502000000000], EntityKey[CoPolicyPointerRedo#00502000000000] 19:14:30,969 DEBUG Loader:1379 - Initializing object from ResultSet: [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=0, systemAssignId=00502000000000}] 19:14:30,969 DEBUG AbstractEntityPersister:2059 - Hydrating entity: [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=0, systemAssignId=00502000000000}] 19:14:30,969 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_0_0_ 19:14:30,969 DEBUG StringType:193 - returning 'PCNM' as column: transInP3_0_0_ 19:14:30,969 DEBUG StringType:193 - returning 'PCNM' as column: transTyp4_0_0_ 19:14:30,969 DEBUG Loader:1379 - Initializing object from ResultSet: [CoPolicyPointerRedo#00502000000000] 19:14:30,969 DEBUG AbstractEntityPersister:2059 - Hydrating entity: [CoPolicyPointerRedo#00502000000000] 19:14:30,985 DEBUG StringType:193 - returning '0100036449 ' as column: policyId1_1_ 19:14:30,985 DEBUG StringType:193 - returning 'WA ' as column: policyPr3_1_1_ 19:14:30,985 DEBUG ShortType:193 - returning '0' as column: policySe4_1_1_ 19:14:30,985 DEBUG Loader:722 - result set row: 1 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_0_0_ 19:14:30,985 DEBUG ShortType:193 - returning '1' as column: transSeqId0_0_ 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_1_1_ 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_1_2_ 19:14:30,985 DEBUG Loader:1197 - result row: EntityKey[CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=1, systemAssignId=00502000000000}], EntityKey[CoPolicyPointerRedo#00502000000000], EntityKey[CoPolicyPointerRedo#00502000000000] 19:14:30,985 DEBUG Loader:1379 - Initializing object from ResultSet: [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=1, systemAssignId=00502000000000}] 19:14:30,985 DEBUG AbstractEntityPersister:2059 - Hydrating entity: [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=1, systemAssignId=00502000000000}] 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_0_0_ 19:14:30,985 DEBUG StringType:193 - returning 'PCNM' as column: transInP3_0_0_ 19:14:30,985 DEBUG StringType:193 - returning 'PCNM' as column: transTyp4_0_0_ 19:14:30,985 DEBUG Loader:722 - result set row: 2 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_0_0_ 19:14:30,985 DEBUG ShortType:193 - returning '5' as column: transSeqId0_0_ 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_1_1_ 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_1_2_ 19:14:30,985 DEBUG Loader:1197 - result row: EntityKey[CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=5, systemAssignId=00502000000000}], EntityKey[CoPolicyPointerRedo#00502000000000], EntityKey[CoPolicyPointerRedo#00502000000000] 19:14:30,985 DEBUG Loader:1379 - Initializing object from ResultSet: [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=5, systemAssignId=00502000000000}] 19:14:30,985 DEBUG AbstractEntityPersister:2059 - Hydrating entity: [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=5, systemAssignId=00502000000000}] 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_0_0_ 19:14:30,985 DEBUG StringType:193 - returning 'PCNM' as column: transInP3_0_0_ 19:14:30,985 DEBUG StringType:193 - returning 'PCNM' as column: transTyp4_0_0_ 19:14:30,985 DEBUG Loader:722 - result set row: 3 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_0_0_ 19:14:30,985 DEBUG ShortType:193 - returning '6' as column: transSeqId0_0_ 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_1_1_ 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_1_2_ 19:14:30,985 DEBUG Loader:1197 - result row: EntityKey[CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=6, systemAssignId=00502000000000}], EntityKey[CoPolicyPointerRedo#00502000000000], EntityKey[CoPolicyPointerRedo#00502000000000] 19:14:30,985 DEBUG Loader:1379 - Initializing object from ResultSet: [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=6, systemAssignId=00502000000000}] 19:14:30,985 DEBUG AbstractEntityPersister:2059 - Hydrating entity: [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=6, systemAssignId=00502000000000}] 19:14:30,985 DEBUG StringType:193 - returning '00502000000000' as column: systemAs1_0_0_ 19:14:30,985 DEBUG StringType:193 - returning 'PCNM' as column: transInP3_0_0_ 19:14:30,985 DEBUG StringType:193 - returning 'PCNM' as column: transTyp4_0_0_ 19:14:30,985 DEBUG Loader:744 - done processing result set (4 rows) 19:14:30,985 DEBUG AbstractBatcher:433 - about to close ResultSet (open ResultSets: 1, globally: 1) 19:14:30,985 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 19:14:30,985 DEBUG AbstractBatcher:562 - closing statement 19:14:30,985 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection 19:14:30,985 DEBUG ConnectionManager:464 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 19:14:30,985 DEBUG DriverManagerConnectionProvider:152 - returning connection to pool, pool size: 1 19:14:30,985 DEBUG Loader:874 - total objects hydrated: 5 19:14:30,985 DEBUG TwoPhaseLoad:130 - resolving associations for [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=0, systemAssignId=00502000000000}] 19:14:30,985 DEBUG DefaultLoadEventListener:199 - loading entity: [CoPolicyPointerRedo#00502000000000] 19:14:30,985 DEBUG DefaultLoadEventListener:372 - attempting to resolve: [CoPolicyPointerRedo#00502000000000] 19:14:30,985 DEBUG DefaultLoadEventListener:389 - resolved object in session cache: [CoPolicyPointerRedo#00502000000000] 19:14:30,985 DEBUG TwoPhaseLoad:226 - done materializing entity [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=0, systemAssignId=00502000000000}] 19:14:30,985 DEBUG TwoPhaseLoad:130 - resolving associations for [CoPolicyPointerRedo#00502000000000] 19:14:30,985 DEBUG LoadContexts:218 - creating collection wrapper:[CoPolicyPointerRedo.coTransactionSummaries#00502000000000] 19:14:31,047 DEBUG TwoPhaseLoad:226 - done materializing entity [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG TwoPhaseLoad:130 - resolving associations for [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=1, systemAssignId=00502000000000}] 19:14:31,047 DEBUG DefaultLoadEventListener:199 - loading entity: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG DefaultLoadEventListener:372 - attempting to resolve: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG DefaultLoadEventListener:389 - resolved object in session cache: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG TwoPhaseLoad:226 - done materializing entity [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=1, systemAssignId=00502000000000}] 19:14:31,047 DEBUG TwoPhaseLoad:130 - resolving associations for [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=5, systemAssignId=00502000000000}] 19:14:31,047 DEBUG DefaultLoadEventListener:199 - loading entity: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG DefaultLoadEventListener:372 - attempting to resolve: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG DefaultLoadEventListener:389 - resolved object in session cache: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG TwoPhaseLoad:226 - done materializing entity [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=5, systemAssignId=00502000000000}] 19:14:31,047 DEBUG TwoPhaseLoad:130 - resolving associations for [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=6, systemAssignId=00502000000000}] 19:14:31,047 DEBUG DefaultLoadEventListener:199 - loading entity: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG DefaultLoadEventListener:372 - attempting to resolve: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG DefaultLoadEventListener:389 - resolved object in session cache: [CoPolicyPointerRedo#00502000000000] 19:14:31,047 DEBUG TwoPhaseLoad:226 - done materializing entity [CoTransactionSummaryRedo#component[systemAssignId,transSeqId]{transSeqId=6, systemAssignId=00502000000000}] 19:14:31,047 DEBUG StatefulPersistenceContext:860 - initializing non-lazy collections 19:14:31,047 DEBUG JDBCContext:260 - after autocommit 19:14:31,047 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection 19:14:31,047 DEBUG SessionImpl:449 - after transaction completion
答案 0 :(得分:0)
您发布的地图和您提供的说明相互冲突:
我会使用别名:
Criteria criteria = session.createCriteria(Parent.class)
.add(Restrictions.eq("policyNo", "1234"))
.createAlias("child", "children")
.add(Restrictions.ne("children.transType", "VIEW"));
更新(基于条件/生成的SQL):
根据您的标准(缩短可读性)来查看Hibernate生成的SQL:
SELECT this_.*, ts1_.*, copolicypo4_.*
FROM CoPolicyPointer this_
INNER JOIN CoTransactionSummary ts1_ ON this_.systemAssignId=ts1_.systemAssignId
LEFT JOIN CoPolicyPointer copolicypo4_ ON ts1_.systemAssignId=copolicypo4_.systemAssignId
您会注意到您的“父”表(CoPolicyPointer
)已加入两次,即使您的条件仅加入“子”表。之所以发生这种情况,是因为您在@Entity
注释中指定了实体名称,但使用原始类名称创建了您的条件,这使得Hibernate认为您正在加入三个独立的实体:
CoPolicyPointerRedo.class
CoTransactionSummaryRedo.class
,其实体名称为CoTransactionSummary
CoPolicyPointerRedo.class
@ ManyToOne` using
醇>
您需要与命名保持一致。从name
声明中删除@Entity
(您可以使用@Table
来指定表名)或使用session.createCriteria(String entityName)
方法创建条件,该方法将实体名称而非类名称作为参数