我有 Persistent 类,引用了许多具有 LAZY fetch 类型的集合,例如
@Entity
@Table(name = "TABLE")
public class Table implements Serializable {
....
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "LIST1", nullable = true)
private ArrayList list1;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "LIST2", nullable = true)
private ArrayList list2;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "LIST1", nullable = true)
private ArrayList list1;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "LIST2", nullable = true)
private ArrayList list2;
....
}
我已经通过 Hibernate 实例化了一些 TABLE 类型的对象,将其添加到表列表中,现在想要为该列表中的所有对象获取该集合之一(例如 list2)。
for(Table table:tables){
result=table.list2;
....
}
但是这样 Hibernate 将生成单独的 SQL 查询序列。 hibernate可以在一次查询中获取集合中所有对象的list2吗? (重要的是不要创建新的 Table 类实例,而是修改已经存在的对象)
最佳答案
A "fetch" join allows associations or collections of values to be initialized along with their parent objects using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections.
select t from Table t
left join fetch t.list2
where ...
关于java - Hibernate - 如何在一个查询中获取对象列表的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8939784/