欢迎,
我正在使用 property-ref 将此外键与我班级的其他一些列而不是主键相关联。主键以外的列不是唯一的。现在,当我尝试获取此集合时,我遇到了异常:
org.hibernate.HibernateException: collection is not associated with any session
数据库:
T_GI_ALARM
-ALARM_ID (PK)
-METHOD_PARAM_NR (FK)
T_QC_METHOD_IN
-METHOD_IN_ID (PK)
-METHOD_PARAM_NR
结构 :
public class Alarm implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Set <QCMethodIN> methodINList = new HashSet <QCMethodIN>(0);
}
public class QCMethodIN implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Integer methodNr;
}
映射:
<hibernate-mapping>
<class name="gestint.data.model.Alarm"
table="DWH1.T_GI_ALARM">
<id name="id" type="java.lang.Integer">
<column name="ALARM_ID" />
<generator class="assigned" />
</id>
<property name="qcMethode" type="java.lang.Integer">
<column name="METHOD_PARAM_NR" not-null="true" />
</property>
<set name="methodINList" table="DWH1.T_QC_METHOD_IN" inverse="true" lazy="false" fetch="select">
<key property-ref="qcMethode">
<column name="METHOD_PARAM_NR" not-null="false" />
</key>
<one-to-many class="gestint.data.model.QCMethodIN" />
</set>
</hibernate-mapping>
<hibernate-mapping>
<class name="gestint.data.model.QCMethodIN"
table="DWH1.T_QC_METHOD_IN">
<id name="id" type="java.lang.Integer">
<column name="METHOD_IN_ID" />
<generator class="assigned" />
</id>
<property name="methodNr" type="java.lang.Integer">
<column name="METHOD_PARAM_NR" not-null="true" />
</property>
</class>
</hibernate-mapping>
我从休眠状态加载数据(会话由弹簧设置),如下所示:
public List<Alarm> listAll() {
return this.sessionFactory.getCurrentSession().createQuery("from Alarm").list();
}
当我对外国人键有不同的价值时,一切都会起作用,例如:
T_GI_ALARM
ID METHOD_PARAM_NR
10021 79
10022 80
10020 91
T_QC_METHOD_IN
METHOD_IN_ID METHOD_PARAM_NR
1 79
2 80
3 91
但是当我对外国人键具有相同的值时(例如 T_GI_ALARM 上的值 79):
T_GI_ALARM
ID METHOD_PARAM_NR
10021 79 <--!!
10022 80
10020 79 <--!!
T_QC_METHOD_IN
METHOD_IN_ID METHOD_PARAM_NR
1 79 <--is call 2 times
2 80
3 91
我收到此错误:
org.hibernate.HibernateException: collection is not associated with any session
我真的不明白发生了什么,我认为是因为我们没有具有唯一 ID 的对象。?!?