创建表语句
create table t_user(
u_id varchar(255),
u_name varchar(255),
u_age Integer,
u_price double(5,2),
uc_id varchar(255),
primary key(u_id)
);
create table t_user_class(
uc_id varchar(255),
uc_classNo Integer,
uc_funds double(5,2),
uc_back varchar(255),
primary key(uc_id)
);
java类:
1、UserT.java
package com.wolf.pojo.oneToone;
import java.io.Serializable;
public class UserT implements Serializable{
private String id;
public String name;
private Integer age;
private double price;
private UserClass userclass;
public UserT(){}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public UserClass getUserclass() {
return userclass;
}
public void setUserclass(UserClass userclass) {
this.userclass = userclass;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result
+ ((userclass == null) ? 0 : userclass.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserT other = (UserT) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double
.doubleToLongBits(other.price))
return false;
if (userclass == null) {
if (other.userclass != null)
return false;
} else if (!userclass.equals(other.userclass))
return false;
return true;
}
@Override
public String toString() {
return "UserT [age=" + age + ", id=" + id + ", name=" + name
+ ", price=" + price + ", userclass=" + userclass + "]";
}
public UserT(String id, String name, Integer age, double price,
UserClass userclass) {
super();
this.id = id;
this.name = name;
this.age = age;
this.price = price;
this.userclass = userclass;
}
}
2、UserClass.java
package com.wolf.pojo.oneToone;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class UserClass implements Serializable{
private String id;
private Integer classNo;
private double funds;
private String back;
private Set<UserT> users=new HashSet<UserT>();
public UserClass(){}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getClassNo() {
return classNo;
}
public void setClassNo(Integer classNo) {
this.classNo = classNo;
}
public double getFunds() {
return funds;
}
public void setFunds(double funds) {
this.funds = funds;
}
public String getBack() {
return back;
}
public void setBack(String back) {
this.back = back;
}
public Set<UserT> getUsers() {
return users;
}
public void setUsers(Set<UserT> users) {
this.users = users;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((back == null) ? 0 : back.hashCode());
result = prime * result + ((classNo == null) ? 0 : classNo.hashCode());
long temp;
temp = Double.doubleToLongBits(funds);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((users == null) ? 0 : users.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserClass other = (UserClass) obj;
if (back == null) {
if (other.back != null)
return false;
} else if (!back.equals(other.back))
return false;
if (classNo == null) {
if (other.classNo != null)
return false;
} else if (!classNo.equals(other.classNo))
return false;
if (Double.doubleToLongBits(funds) != Double
.doubleToLongBits(other.funds))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (users == null) {
if (other.users != null)
return false;
} else if (!users.equals(other.users))
return false;
return true;
}
@Override
public String toString() {
return "UserClass [back=" + back + ", classNo=" + classNo + ", funds="
+ funds + ", id=" + id + ", users=" + users + "]";
}
public UserClass(String id, Integer classNo, double funds, String back,
Set<UserT> users) {
super();
this.id = id;
this.classNo = classNo;
this.funds = funds;
this.back = back;
this.users = users;
}
}
映射文件:
UserT.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wolf.pojo.oneToone" auto-import="false">
<class name="UserT" table="t_user">
<!-- 多对一:只有单项 -->
<id name="id" column="u_id">
</id>
<property name="name" column="u_name"/>
<property name="age" column="u_age"/>
<property name="price" column="u_price"/>
<many-to-one name="userclass"
class="com.wolf.pojo.oneToone.UserClass"
column="uc_id"
></many-to-one>
</class>
</hibernate-mapping>
UserClass.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wolf.pojo.oneToone">
<class name="UserClass" table="t_user_class">
<id name="id" column="uc_id"/>
<property name="classNo" column="uc_classNo"/>
<property name="funds" column="uc_funds"/>
<property name="back" column="uc_back"/>
<!-- inverse="true":关系维护由UserClass负责维护 -->
<set name="users" lazy="false" cascade="all" inverse="true">
<key column="uc_id"></key>
<one-to-many class="com.wolf.pojo.oneToone.UserT"/>
</set>
</class>
</hibernate-mapping>
测试类:
package com.wolf.main;
import java.util.UUID;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.wolf.pojo.oneToone.UserClass;
import com.wolf.pojo.oneToone.UserT;
public class UtilMain {
public static void main(String[] args) {
Session session= HibernateSessionFactory.getSession();
Transaction tr=session.beginTransaction();
UserClass usercls=new UserClass();
usercls.setId(UUID.randomUUID().toString());
UserT user=new UserT();
user.setId(UUID.randomUUID().toString());
user.setName("user0");
UserT user1=new UserT();
user1.setId(UUID.randomUUID().toString());
user1.setName("user1");
usercls.getUsers().add(user1);
usercls.getUsers().add(user);
user.setUserclass(usercls);
user1.setUserclass(usercls);
System.out.println(usercls.getUsers());
session.save(usercls);
tr.commit();
}
}
出现错误是:
Exception in thread "main" java.lang.StackOverflowError
at java.util.HashMap$KeyIterator.<init>(HashMap.java:826)
at java.util.HashMap.newKeyIterator(HashMap.java:840)
at java.util.HashMap$KeySet.iterator(HashMap.java:874)
at java.util.HashSet.iterator(HashSet.java:153)
at java.util.AbstractCollection.toString(AbstractCollection.java:414)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.wolf.pojo.oneToone.UserClass.toString(UserClass.java:109)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.wolf.pojo.oneToone.UserT.toString(UserT.java:108)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at java.util.AbstractCollection.toString(AbstractCollection.java:422)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.wolf.pojo.oneToone.UserClass.toString(UserClass.java:109)
最后解决方案是:java-hashCode内部实现直接循环自己的hashCode方法,测试进入死循环,一直出现错误
解决方案:
1、删除hashCode的方法
2、在使用的bean类中set不要实例化,在使用的时候临时创建set集合,然后再存放到pojo对象所以引用的集合中
3、这是javaAPI中说的异常结论:
public class StackOverflowErrorextends VirtualMachineError
Thrown when a stack overflow occurs because an application recurses too deeply. (堆栈溢出发生时抛出一个应用程序是递归因为太深。)
下面是其他朋友的测试地址:
http://bantouyan.iteye.com/blog/963759
http://www.myexception.cn/ruby-rails/1050378.html
如果有哪位高手知道这个错误的解决方案,还请多多指教