Здравствуйте!
Возникает ошибка следующего рода, когда вывожу на контролере данные из бд.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ua.home.entity.Question.answers, no session or session was closed
Сущность
@Entity<br>
@Table(name = "Question")<br>
public class Question implements Serializable{<br>
<br>
@Id<br>
@GeneratedValue(strategy=GenerationType.IDENTITY)<br>
private int id;<br>
<br>
private String question;<br>
<br>
@OneToMany(mappedBy="question")<br>
private List<Answer> answers;<br>
.....<br>
DAO
@Repository<br>
@Transactional <br>
public class QuestionDAOImpl implements QuestionDAO {<br>
<br>
@Autowired<br>
private SessionFactory sessionFactory;<br>
<br>
@SuppressWarnings("unchecked")<br>
// @Transactional<br>
public List<Question> getQuestion() {<br>
return sessionFactory.getCurrentSession().createQuery("from Question").list();<br>
}<br>
}<br>
Service
@Service<br>
public class QuestionServiceImpl implements QuestionService {<br>
<br>
@Autowired<br>
private QuestionDAO questionDAO;<br>
<br>
@Transactional<br>
public List<Question> getQuestion() {<br>
return questionDAO.getQuestion();<br>
}<br>
}<br>
Conroller
@Controller<br>
public class QuestionController {<br>
<br>
@Autowired<br>
private QuestionService questionService;<br>
<br>
@RequestMapping(value = "/", method = RequestMethod.GET)<br>
public String home(Model model){<br>
List<Question> qu =questionService.getQuestion();<br>
System.out.println(qu);<br>
return "home";<br>
}<br>
}<br>
Пробовал использовать OpenSessionInViewFilter, с и без url-pattern
<filter><br>
<filter-name>springSecurityFilterChain</filter-name><br>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class><br>
</filter><br>
<filter-mapping><br>
<filter-name>springSecurityFilterChain</filter-name><br>
<url-pattern>/*</url-pattern><br>
</filter-mapping> <br>
Пробовал использовать EAGER, другая ошибка
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError
Нашел ещё совет использовать Hibernate.initialize, но не пойму как его точно сюда применить, если возможно в обще.
В data.xml добавил SessionFactoryUtils, тоже нашёл такой совет, не помогло.
<?xml version="1.0" encoding="UTF-8"?><br>
<beans xmlns="http://www.springframework.org/schema/beans"<br>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"<br>
xmlns:context="http://www.springframework.org/schema/context"<br>
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"<br>
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"<br>
xmlns:util="http://www.springframework.org/schema/util"<br>
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd<br>
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd<br>
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd<br>
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd<br>
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd<br>
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><br>
<br>
<tx:annotation-driven transaction-manager="transactionManager" /><br>
<br>
<bean id="transactionManager"<br>
class="org.springframework.orm.hibernate3.HibernateTransactionManager"><br>
<property name="sessionFactory" ref="sessionFactory" /><br>
</bean><br>
<br>
<bean id="messageSource"<br>
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><br>
<property name="basename" value="classpath:messages" /><br>
<property name="defaultEncoding" value="UTF-8" /><br>
</bean><br>
<br>
<bean id="propertyConfigurer"<br>
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"<br>
p:location="/WEB-INF/jdbc.properties" /><br>
<br>
<bean id="dataSource"<br>
class="org.springframework.jdbc.datasource.DriverManagerDataSource"<br>
p:driverClassName="${jdbc.driverClassName}" <br>
p:url="${jdbc.databaseurl}"<br>
p:username="${jdbc.username}" <br>
p:password="${jdbc.password}" /><br>
<br>
<bean name="hibernateSession" class="org.springframework.orm.hibernate3.SessionFactoryUtils" factory-method="getSession"<br>
scope="prototype"><br>
<constructor-arg index="0" ref="hibernateSessionFactory"/><br>
<constructor-arg index="1" value="false"/><br>
<aop:scoped-proxy/><br>
</bean><br>
<br>
<bean id="sessionFactory"<br>
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><br>
<property name="dataSource" ref="dataSource" /><br>
<property name="configLocation"><br>
<value>classpath:hibernate.cfg.xml</value><br>
<!-- <value>classpath*:**/hibernate.cfg.xml</value> --><br>
<br>
</property><br>
<property name="configurationClass"><br>
<value>org.hibernate.cfg.AnnotationConfiguration</value><br>
</property><br>
<property name="hibernateProperties"><br>
<props><br>
<prop key="hibernate.show_sql">true</prop><br>
<prop key="hibernate.dialect">${jdbc.dialect}</prop><br>
<prop key="hibernate.connection.charSet">UTF-8</prop><br>
</props><br>
</property><br>
</bean><br>
<br>
</beans><br>
Какие ещё могут быть решения моей проблемы или возможно где то допустил ошибку?
Спасибо
UPDATE
@Entity<br>
public class Answer implements Serializable {<br>
<br>
<br>
@Id<br>
@GeneratedValue(strategy=GenerationType.IDENTITY)<br>
// @GeneratedValue<br>
private int id;<br>
<br>
private String answer;<br>
<br>
private byte isCorrect;<br>
<br>
@ManyToOne<br>
@JoinColumn(name="QuestionID")<br>
private Question question;<br>
.........<br>
@Override<br>
public String toString() {<br>
return "Answer [id=" + id + ", answer=" + answer + ", isCorrect="<br>
+ isCorrect + ", questions=" + question + "]";<br>
}<br>
В общем если убрать строку + ", questions=" + question, тогда нету ошибки. Но в результате запроса нету FK на сущность Question.
toString Question
@Override<br>
public String toString() {<br>
return "Question [id=" + id + ", question=" + question + ", answers="<br>
+ answers + "]";<br>
}<br>
PS как автору ответить на комментарий пользователя? Раньше получалось, сейчас нету жму кажись.