我刚刚开始了解FetchType Lazy和Eager。我理解差异,但即使我设置为Lazy,它仍然试图获取相关数据。
关系:1人:许多电话
查看研究尝试和教程:
https://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/ http://howtodoinjava.com/hibernate/lazy-loading-in-hibernate/ https://howtoprogramwithjava.com/hibernate-eager-vs-lazy-fetch-type/
我理解得到相关数据,我仍然需要在会话中做到这一点()所以对于我的特殊例子在我的Dao中,我需要这样的东西
List<Person> persons = criteria.list();
for(Person person : persons){
Set sets = person.getPhones();
}
return persons;
到目前为止正确吗?
但问题是我没有在Dao,controller ..等任何地方调用person.getPhones()但我得到了LazyInitializationEception。对于我的生活似乎无法捕捉到什么是错的。
堆栈跟踪
Jun 19, 2017 2:24:01 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [app-dispatcher] in context with path
[/uni-starter-onetomany] threw exception [Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.app.person.Person.phones, could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.app.person.Person.phones, could not initialize proxy - no Session
Person.class
@Entity
@Table(name="person")
@Component
public class Person {
@Id
@GeneratedValue
private int person_id;
private String name;
private String age;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
private Set<Phone> phones;
// Getter and setters
// ToString method by field name not by method
Phone.class
@Entity
@Table(name="phone")
@Component
public class Phone {
@Id
@GeneratedValue
private int phone_id;
private String type;
private String phone;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="person_person_id")
private Person person;
// Getter and setters
PersonDao的
@Repository
@Transactional
@Component("personDao")
public class PersonDao {
@Autowired
private SessionFactory sessionFactory;
public Session session(){
return sessionFactory.getCurrentSession();
}
public List<Person> getPersonList(){
Criteria criteria = session().createCriteria(Person.class);
List<Person> persons = criteria.list();
// for(Person person : persons){
//
// Set sets = person.getPhones();
//
// }
return persons;
}
public void saveOrUpdate(Person person){
session().saveOrUpdate(person);
}
}
控制器
// Get list
@RequestMapping(path="/list", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Map<String, Object> getListPerson(){
Map<String, Object> data = new HashMap<String, Object>();
List<Person> persons = personDao.getPersonList();
data.put("results", persons);
System.out.println(persons);
return data;
}
问题
只要我不在会话中调用person.getPhones(),它应该只返回人吗?如果是这样的话,我可能会遇到LazyInitializationException的问题?
我还在Dao类中看过人们setFetchMode()FetchMode.JOIN例如 Hibernate Criteria Join with 3 Tables 也许这可能是主观的但是什么是更好的做法?任何性能问题?
非常感谢任何想法,链接或文章...
更新 感谢Abassa从Person.class中的toString中删除Phone in来解决问题。但我只是意识到,由于杰克逊,在序列化期间,它试图获取电话ojbect ....是否有解决方法?
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: com.app.person.Person.phones, could not initialize proxy - no Session
答案 0 :(得分:1)
从Person类中的 toString 方法中删除phone字段。
致电时:
System.out.println(persons);
您尝试访问手机字段,因为println为列表中的每个人调用toString方法,因此您获得了LazyInitializationException。