使用hibernate和spring将持久对象的String集合加载到模型中

时间:2012-11-23 17:38:29

标签: java spring hibernate spring-mvc

当尝试从我通过hibernate从数据库加载的对象中获取一个stings列表时,我得到了这个异常。

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

我用来加载列表的方法是在一个事务中。但是当我尝试将列表放在模型中时,我得到了上述异常。我从中得知,hibernate要求我在事务中也要有这行代码。但鉴于它不是数据库操作,为什么会这样呢?

@RequestMapping(value="{id}", method=RequestMethod.POST)
public String addComment(@PathVariable String id, Model model, String comment) {
    personService.addComment(Long.parseLong(id), comment);
    Person person = personService.getPersonById(Long.parseLong(id));
    model.addAttribute(person);
    List<String> comments = personService.getComments(id);
    model.addAttribute(comments);
    return "/Review";
}

服务对象。

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


public class PersonServiceImpl implements PersonService {

private Workaround personDAO;


public PersonServiceImpl() {
}

@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public void savePerson(Person person) { 
    personDAO.savePerson(person);
}

@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public Person getPersonById(long id) {
    return personDAO.getPersonById(id);
}

@Autowired
public void setPersonDAO(Workaround personDAO) {
    this.personDAO = personDAO;
}

@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public List<Person> getAllPeople() {
    return personDAO.getAllPeople();
}

@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public void addComment(Long id, String comment) {
    Person person = getPersonById(id);
    person.addComment(comment);
    savePerson(person);
}

@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public List<String> getComments(String id) {
    return personDAO.getComments(Long.parseLong(id));
}

}

DAO

import java.util.List;

import javax.persistence.ElementCollection;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;



@Repository
public class PersonDAO implements Workaround {
private SessionFactory sessionFactory;

@Autowired
public PersonDAO(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;   
}

private Session currentSession() {
    return sessionFactory.getCurrentSession();
}

public void addPerson(Person person) {
    currentSession().save(person);
}

public Person getPersonById(long id) {
    return (Person) currentSession().get(Person.class, id);
}


public void savePerson(Person person) {
    currentSession().save(person);
}

public List<Person> getAllPeople() {
    List<Person> people = currentSession().createQuery("from Person").list();
    return people;

}

public List<String> getComments(long id) {
    return getPersonById(id).getComments();
}

}

4 个答案:

答案 0 :(得分:1)

我对Hibernate相对较新,但我会从对它的理解中猜测出来。

默认情况下,@OneToMany个集合会被延迟加载。因此,当您加载Person对象时,将创建代理以代替您的实际comments列表。在您按原样调用该列表的getter(即getComments())之前,不会加载此列表,即便如此,我也不认为完整列表是一次性加载的,更多是一个一个(是的)当你调用.size()时,在遍历列表或整个列表时迭代多个db调用。

但是,我认为这里的问题是您必须在加载父(Person)对象的同一会话中加载列表。当您加载Person对象时,您有对当前会话的引用,然后一旦您调用该Person对象上的getComments(),会话就会关闭。

我认为要将整个过程保留在一个会话中,您可以在DAO类中手动打开和关闭会话。

public List<String> getComments(long id) {
    Session session = sessionFactory.openSession();

    List<String> comments = getPersonById(id).getComments();

    sessionFactory.getCurrentSession().flush();
    sessionFactory.getCurrentSession.close();

    return comments;
}

将FetchType设置为EAGER可以解决问题,但是根据我的阅读,一般不推荐它,除非你总是需要使用Person对象加载的注释。

答案 1 :(得分:0)

将获取类型更改为FetchType.EAGER

答案 2 :(得分:0)

我认为问题在于您正在使用

currentSession()

尝试用

替换它
private Session currentSession() {
    return sessionFactory.openSession();
}

因为例外说没有任何公开会话。

答案 3 :(得分:0)

您应该查看Person类映射,可能Comments字段与LAZY属性映射,因此它未加载到DAO中。当你调用getComments方法时,Hibernate尝试从数据库加载属性,但那时没有会话,因此异常。要解决此问题,请将映射属性更改为EAGER