Spring + Hibernate java.lang.StackOverflowError [英] Spring+hibernate java.lang.StackOverflowError
问题描述
我是Hibernate的新手,正面临这个问题:
I'm new in Hibernate and facing the problem:
@Transactional(readOnly=true)
@Override
public List<User> fetchListUsers() {
return sessionFactory.getCurrentSession().createQuery("select u from User u").list();
}
我的use.hbm.xml
My use.hbm.xml
<hibernate-mapping>
<class name="demidov.pkg.domain.User" table="USER_DESC">
<!-- Primary key ID will be generated depends on database configuration -->
<id name="userId" column="ID">
<generator class="native"></generator>
</id>
<property name="userName" column="USER_NAME" unique="true" />
<property name="userPassword" column="USER_PASS" />
<property name="userPriveleges" column="USER_PRIVLG" />
<property name="userEmale" column="USER_EMALE" unique="true"/>
<property name="userGender" column="USER_GENDER" />
<!-- User is owner of relationships, all changes on user will effect UserMessage entity -->
<set name="userMessageList" inverse="true" lazy="false" fetch="select">
<key>
<column name="USER_ID" not-null="true"/>
</key>
<one-to-many class="demidov.pkg.domain.UserMessage" />
</set>
</class>
</hibernate-mapping>
UserMessage.hbm.xml
UserMessage.hbm.xml
<hibernate-mapping>
<class name="demidov.pkg.domain.UserMessage" table="MESSAGES_CONTENT">
<id name="messageId" column="ID">
<generator class="native"></generator>
</id>
<property name="theMessage" column="MESSAGE" length="400"/>
<many-to-one name="theUser" class="demidov.pkg.domain.User" lazy="false" fetch="select" cascade="all">
<column name="USER_ID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
主类:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
GuestBookDAOIF gustDAO = (GuestBookDAOIF) context.getBean("guestBookDAOImpl", GuestBookDAOIF.class);
List<User> uList = gustDAO.fetchListUsers();
for(User u : uList)
System.out.println(u);
}
错误堆栈:
Hibernate: select usermessag0_.ID as ID1_0_, usermessag0_.MESSAGE as MESSAGE2_0_, usermessag0_.USER_ID as USER_ID3_0_ from MESSAGES_CONTENT usermessag0_
Hibernate: select user0_.ID as ID1_1_0_, user0_.USER_NAME as USER_NAM2_1_0_, user0_.USER_PASS as USER_PAS3_1_0_, user0_.USER_PRIVLG as USER_PRI4_1_0_, user0_.USER_EMALE as USER_EMA5_1_0_, user0_.USER_GENDER as USER_GEN6_1_0_ from USER_DESC user0_ where user0_.ID=?
Hibernate: select user0_.ID as ID1_1_0_, user0_.USER_NAME as USER_NAM2_1_0_, user0_.USER_PASS as USER_PAS3_1_0_, user0_.USER_PRIVLG as USER_PRI4_1_0_, user0_.USER_EMALE as USER_EMA5_1_0_, user0_.USER_GENDER as USER_GEN6_1_0_ from USER_DESC user0_ where user0_.ID=?
Hibernate: select user0_.ID as ID1_1_0_, user0_.USER_NAME as USER_NAM2_1_0_, user0_.USER_PASS as USER_PAS3_1_0_, user0_.USER_PRIVLG as USER_PRI4_1_0_, user0_.USER_EMALE as USER_EMA5_1_0_, user0_.USER_GENDER as USER_GEN6_1_0_ from USER_DESC user0_ where user0_.ID=?
Hibernate: select usermessag0_.USER_ID as USER_ID3_1_1_, usermessag0_.ID as ID1_0_1_, usermessag0_.ID as ID1_0_0_, usermessag0_.MESSAGE as MESSAGE2_0_0_, usermessag0_.USER_ID as USER_ID3_0_0_ from MESSAGES_CONTENT usermessag0_ where usermessag0_.USER_ID=?
Hibernate: select usermessag0_.USER_ID as USER_ID3_1_1_, usermessag0_.ID as ID1_0_1_, usermessag0_.ID as ID1_0_0_, usermessag0_.MESSAGE as MESSAGE2_0_0_, usermessag0_.USER_ID as USER_ID3_0_0_ from MESSAGES_CONTENT usermessag0_ where usermessag0_.USER_ID=?
Hibernate: select usermessag0_.USER_ID as USER_ID3_1_1_, usermessag0_.ID as ID1_0_1_, usermessag0_.ID as ID1_0_0_, usermessag0_.MESSAGE as MESSAGE2_0_0_, usermessag0_.USER_ID as USER_ID3_0_0_ from MESSAGES_CONTENT usermessag0_ where usermessag0_.USER_ID=?
Exception in thread "main" java.lang.StackOverflowError
at java.util.HashMap.keySet(HashMap.java:1000)
at java.util.HashSet.iterator(HashSet.java:170)
at java.util.AbstractCollection.toString(AbstractCollection.java:450)
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:327)
at java.lang.String.valueOf(String.java:2854)
at java.lang.StringBuilder.append(StringBuilder.java:128)
at demidov.pkg.domain.User.toString(User.java:95)
at java.lang.String.valueOf(String.java:2854) ....
我来自UserMessage的toString
My toString from UserMessage
@Override
public String toString() {
return "UserMessage [messageId=" + messageId + ", theMessage="
+ theMessage + ", theUser=" + theUser + "]";
}
我的用户toString
My toString from User
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", userPassword=" + userPassword + ", userPriveleges="
+ userPriveleges + ", userEmale=" + userEmale + ", userGender="
+ userGender + ", userMessageList=" + userMessageList + "]";
}
请帮助我了解为什么会出现此错误. 一段时间以来,我未能懒惰地初始化角色集合:demidov.pkg.domain.User.userMessageList,无法初始化代理-如果我将lazy切换为true,则没有Session.请帮助我了解发生了什么.
Please help me to understand why I'm having this error. Some time I have failed to lazily initialize a collection of role: demidov.pkg.domain.User.userMessageList, could not initialize proxy - no Session if i switch lazy to true. Please help me to understand what's going on.
谢谢.
推荐答案
大多数List
实现的toString()
方法遍历List
元素并在其上调用toString()
.所以打电话
The toString()
method for most List
implementations iterates through the List
elements and calls toString()
on them. So calling
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", userPassword=" + userPassword + ", userPriveleges="
+ userPriveleges + ", userEmale=" + userEmale + ", userGender="
+ userGender + ", userMessageList=" + userMessageList + "]";
}
+ userMessageList
实际上正在调用每个UserMessage
的toString()
,后者正在每个User
上调用toString()
,这是恶心的.
the + userMessageList
is actually calling the toString()
of each UserMessage
which is calling toString()
on each User
, ad nauseam.
将您的toString()
更改为不打印它们,或仅打印其中的一些值(例如ID).
Change your toString()
to not print them, or print just some value of them (like an ID).
这篇关于Spring + Hibernate java.lang.StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!