FetchMode和FetchType之间的区别 [英] difference between FetchMode and FetchType
问题描述
指定 lazy =true
并使用 fetch =select或join
?哪一个比另一个优先?
问候
jayendra
@Entity
@Table
public class父母{
@Id
私人长ID;
@OneToMany(mappedBy =parent,fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
private List< Child>儿童;
// getter setters
}
@Entity
@Table
public class Child {
@Id
私人长ID;
@ManyToOne(fetch = FetchType.LAZY)
私有父母;
// getter setter
}
在上面的例子中,当获取 Parent
实体时,hibernate会使用join自动加载所有子
实体。另一方面,当你读取 Child
时, child.getParent()
。
FetchType(Lazy / Eager)告诉我们是否希望实体被加载或者懒惰,当有代码调用的时候。
FetchMode(Select / Join)告诉我们是否希望我们的实体被加载额外的select或者带有join或subselect的查询。
what is the difference in specifying lazy = "true"
and using fetch = "select" or "join"
? which one is preferred over the other ?
regards jayendra
Let's say we have entities like this:
@Entity
@Table
public class Parent {
@Id
private Long id;
@OneToMany(mappedBy="parent", fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
private List<Child> child;
//getter setters
}
@Entity
@Table
public class Child {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Parent parent;
//getter setter
}
In above example, when getting Parent
entity, hibernate will automaticly load all child
entities eagerly using join. On the other hand, when you fetch Child
, Parent
entity won't be selectet unless you call it explicity in your code child.getParent()
.
FetchType (Lazy/Eager) tells whether we want entity to be loaded eagerly or lazy, when there's call in code.
FetchMode (Select/Join) tells whether we want our entitity to be loaded with additional select or in one query with join or subselect.
这篇关于FetchMode和FetchType之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!