几天前,我在Hibernate中遇到了一种奇怪的行为,无法说明它为何突然出现。
有两个表共享单向1:n关系。记录目录包含 n 记录。在条件下我无法发现只加载了一条记录。没有发生错误消息。也没有警告。
实际上它很容易,总是习惯工作。 (此帖子末尾的对象代码)
直到现在我只能说,如果Record-Catalog有ID 78,则只会加载一条记录。对于其他ID,它看起来没问题。
我像这样提取我的RC-Object:
public String getCatalog(){
Session session = null;
try {
SessionFactory factory = HibernateUtil.getSessionFactory();
session = factory.openSession();
Transaction tx = session.beginTransaction();
String hql = "SELECT R FROM rcatalog.Rcatalog as R WHERE rcatalogID_PK = " + rcid;
Query query = session.createQuery(hql);
return getOutputForPage(query.list().get(0)); //If I get the size of the records it's 1 here
} catch (HibernateException | java.lang.IndexOutOfBoundsException hibex) {
Logger.getLogger(BasisHibernate.class.getName()).log(Level.INFO, "Rcid: {0}", rcid);
Logger.getLogger(BasisHibernate.class.getName()).log(Level.SEVERE, null, hibex);
} finally {
try {
if (session != null) {
session.close();
}
} catch (HibernateException | NullPointerException hibex) {
}
}
return "";
}
到目前为止,这对我来说似乎没问题。 ID是正确的。我决定打开sql-output并得到以下行 (M_ID - >目录的ID D_ID - >记录的ID ):
Hibernate: select myrecord0_.M_ID as M65_9_1_, myrecord0_.D_ID as D1_1_, myrecord0_.D_ID as D1_7_0_ from [MyProj].[dbo].[MyRecord] myrecord0_ where myrecord0_.M_ID=?
通过在SQL Server Management Studio中使用查询,我获得了按预期列出的记录数量。
直到现在我已经尝试删除出现的单个记录,但行为保持不变。
我已经做了一些研究但是没有成功:
http://www.coderanch.com/t/442282/ORM/databases/Hibernate-loads-element-collection
以下是我的课程(如前所述,我没有使用FetchType.EAGER。这仅用于测试目的)
@Entity
@Table(name = "[MyProj].[dbo].[Rcatalog]")
public class Rcatalog implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "M_ID")
private Integer RcatalogID_PK;
@ManyToOne()
@JoinColumn(name = "W_ID", insertable = false, updatable = false)
private UpperElement upperelement;
@Column(name = "W_ID")
@JoinColumn(name = "upperelementID_FK", referencedColumnName = "upperelementID_PK")
private Integer upperelementID_FK;
@OneToMany(mappedBy = "myrecordID_FK", fetch = FetchType.EAGER)
@Sort(type = SortType.COMPARATOR, comparator = MyRecordComparator.class)
private SortedSet<MyRecord> myrecord = new TreeSet<>();
//Constructor, Getter, Setter...
}
还有记录:
@Entity
@Table(name = "[MyProj].[dbo].[MyRecord]")
public class MyRecord implements Serializable, PartElementInterface {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "D_ID")
private Integer myrecordID_PK;
@Column(name = "M_ID")
@JoinColumn(name = "myrecordID_FK", referencedColumnName = "myrecordID_PK")
private Integer myrecordID_FK;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "M_ID", insertable = false, updatable = false, nullable = true)
private Rcatalog rcatalog;
//Constructor, Getter, Setter and some more stuff
}
我的Hibernate核心版本是3.6.10.Final
答案 0 :(得分:0)
自己找到答案:
我们在这里有以下比较器:
@OneToMany(mappedBy = "myrecordID_FK", fetch = FetchType.EAGER)
@Sort(type = SortType.COMPARATOR, comparator = MyRecordComparator.class)
private SortedSet<MyRecord> myrecord = new TreeSet<>();
使用简单的比较算法:
公共类MyRecordComparator实现Comparator {
@Override
public int compare(Object obj1, Object obj2) {
MyRecord d1 = (MyRecord) obj1;
MyRecord d2 = (MyRecord) obj2;
return d1.getPos_nr().compareTo(d2.getPos_nr());
}
}
运行一些测试后,我发现如果compare-method返回-1或1,我的记录只能成功加载。如果两个compare-values相等且返回值为0,我们就会遇到问题{{ 1}}。
所以我把它改成了:
公共类MyRecordComparator实现Comparator {
SortedSet