我做的一個Hibernate 一對多 多對一的 保存和讀取操作 保存 沒問題 但是 讀取那老是提示 內存溢出
下面是 實體類
Department 類 有setter 和getter 方法
private Integer id;
private String name;
private Set employees=new HashSet();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
Emloyee 類 有setter 和getter 方法
private Integer id;
private String name;
private Department department;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
下面是映射類
Department.hbm.xml
<class name="Department" table="department">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="string" column="name" />
<set name="employees">
<key column="departmentId"></key>
<one-to-many class="Employee"/>
</set>
</class>
Employee.hbm.xml
<class name="Employee" table="employee">
<id name="id">
<generator class="native" />
</id>
<property name="name" type="string" column="name" />
<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
</class>
下面是測試類
private static SessionFactory sessionFactory;
static{
sessionFactory=new Configuration()//
.configure()//
.buildSessionFactory();
}
@Test
public void testget() throws Exception{
Session session=sessionFactory.openSession();
session.beginTransaction();
Department department=(Department) session.get(Department.class, 1);
System.out.println(department);
System.out.println(department.getEmployees());
session.getTransaction().commit();
session.close();
}
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- 數據庫基本配置信息 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///htest</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 數據庫其他相關配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="org/xxf/OneToManyA/Department.hbm.xml"/>
<mapping resource="org/xxf/OneToManyA/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
測試後 它就老提示 我內存溢出
java.lang.StackOverflowError
at java.lang.Integer.toString(Integer.java:305)
at java.lang.Integer.toString(Integer.java:116)
at java.lang.String.valueOf(String.java:2932)
at java.lang.Integer.toString(Integer.java:682)
at java.lang.String.valueOf(String.java:2827)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at org.xxf.OneToManyA.Employee.toString(Employee.java:13)
at java.lang.String.valueOf(String.java:2827)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at java.util.AbstractCollection.toString(AbstractCollection.java:422)
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:327)
往下 就一直重復出現錯誤
別人用一樣的代碼 測試就沒問題 我就老報錯 求各位大神幫指導一下
你不是內存溢出,是棧溢出,因為你的裡面有循環調用,我看了一下
org.xxf.OneToManyA.Employee.toString(Employee.java:13)
你是不是重載了toString方法,因為你沒有貼出全部代碼,
你的toString方法中存在循環調用,這個是你遇到stackoverflow的原因!!!