User类:
package com.bryantd.hibernate.entity;import java.io.Serializable;
import java.util.*;import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;public class User implements Serializable {
private Integer id;
private String name;
private int age;
private Set addresses;
public User(String name, int age) {
super();
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
}
public User(){}
public Set getAddresses() {
if (addresses == null) {
return new HashSet();
}
return addresses;
}
public void setAddresses(Set addresses) {
this.addresses = addresses;
} public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} 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;
}
}User的映射文件 user.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
<class
name="com.bryantd.hibernate.entity.User"
table="undirectional_one_to_many_user">
<id name="id" type="java.lang.Integer">
<column name="id" sql-type="INTEGER" not-null="true"/>
<generator class="native"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" sql-type="VARCHAR"/>
</property>
<property name="age" type="int">
<column name="age" sql-type="INTEGER"/>
</property>
<!-- We usually set the class with the associated column as the domain class. -->
<set name="addresses" table="undirectional_one_to_many_address" cascade="all" inverse="true">
<key column="user_id"/>
<one-to-many class="com.bryantd.hibernate.entity.Address"/>
</set>
</class></hibernate-mapping>Address类:
package com.bryantd.hibernate.entity;import java.io.Serializable;
import java.util.*;import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;public class Address implements Serializable {
private Integer id;
private String address;
private String telephone;
private User user;
public Address(String address, String telephone) {
super();
// TODO Auto-generated constructor stub
this.address = address;
this.telephone = telephone;
}
public Address() {}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean equals(Object object) {
if (!(object instanceof Address)) {
return false;
}
Address anotherAddress = (Address)object;
return new EqualsBuilder().appendSuper(super.equals(object))
.append(this.getAddress(), anotherAddress.getAddress())
.append(this.getTelephone(), anotherAddress.getTelephone())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder().appendSuper(super.hashCode())
.append(this.getAddress())
.append(this.getTelephone())
.hashCode();
} /*
public boolean equals(Object object) {
// TODO Auto-generated method stub
if (onject == this) {
return true;
}
if (object instanceof Address) {
Address address = (Address)object;
if (getAddress() == address.getAddress()) return true;
}
return false;
} public int hashCode() {
// TODO Auto-generated method stub
return getTelephone().hashCode() * getAddress().hashCode();
}
*/
}Address的映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
<class
name="com.bryantd.hibernate.entity.Address"
table="undirectional_one_to_many_address">
<id name="id" type="java.lang.Integer">
<column name="id" sql-type="INTEGER" not-null="true"/>
<generator class="native"/>
</id>
<property name="address" type="java.lang.String">
<column name="address" sql-type="VARCHAR)"/>
</property>
<property name="telephone" type="java.lang.String">
<column name="telephone" sql-type="VARCHAR"/>
</property>
<many-to-one name="user" class="com.bryantd.hibernate.entity.User" cascade="none"
column="user_id"
not-null="true"/>
</class></hibernate-mapping>
package com.bryantd.hibernate.entity;import java.io.Serializable;
import java.util.*;import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;public class User implements Serializable {
private Integer id;
private String name;
private int age;
private Set addresses;
public User(String name, int age) {
super();
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
}
public User(){}
public Set getAddresses() {
if (addresses == null) {
return new HashSet();
}
return addresses;
}
public void setAddresses(Set addresses) {
this.addresses = addresses;
} public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} 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;
}
}User的映射文件 user.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
<class
name="com.bryantd.hibernate.entity.User"
table="undirectional_one_to_many_user">
<id name="id" type="java.lang.Integer">
<column name="id" sql-type="INTEGER" not-null="true"/>
<generator class="native"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" sql-type="VARCHAR"/>
</property>
<property name="age" type="int">
<column name="age" sql-type="INTEGER"/>
</property>
<!-- We usually set the class with the associated column as the domain class. -->
<set name="addresses" table="undirectional_one_to_many_address" cascade="all" inverse="true">
<key column="user_id"/>
<one-to-many class="com.bryantd.hibernate.entity.Address"/>
</set>
</class></hibernate-mapping>Address类:
package com.bryantd.hibernate.entity;import java.io.Serializable;
import java.util.*;import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;public class Address implements Serializable {
private Integer id;
private String address;
private String telephone;
private User user;
public Address(String address, String telephone) {
super();
// TODO Auto-generated constructor stub
this.address = address;
this.telephone = telephone;
}
public Address() {}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean equals(Object object) {
if (!(object instanceof Address)) {
return false;
}
Address anotherAddress = (Address)object;
return new EqualsBuilder().appendSuper(super.equals(object))
.append(this.getAddress(), anotherAddress.getAddress())
.append(this.getTelephone(), anotherAddress.getTelephone())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder().appendSuper(super.hashCode())
.append(this.getAddress())
.append(this.getTelephone())
.hashCode();
} /*
public boolean equals(Object object) {
// TODO Auto-generated method stub
if (onject == this) {
return true;
}
if (object instanceof Address) {
Address address = (Address)object;
if (getAddress() == address.getAddress()) return true;
}
return false;
} public int hashCode() {
// TODO Auto-generated method stub
return getTelephone().hashCode() * getAddress().hashCode();
}
*/
}Address的映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
<class
name="com.bryantd.hibernate.entity.Address"
table="undirectional_one_to_many_address">
<id name="id" type="java.lang.Integer">
<column name="id" sql-type="INTEGER" not-null="true"/>
<generator class="native"/>
</id>
<property name="address" type="java.lang.String">
<column name="address" sql-type="VARCHAR)"/>
</property>
<property name="telephone" type="java.lang.String">
<column name="telephone" sql-type="VARCHAR"/>
</property>
<many-to-one name="user" class="com.bryantd.hibernate.entity.User" cascade="none"
column="user_id"
not-null="true"/>
</class></hibernate-mapping>
解决方案 »
- hibernate映射mysql数据类型varchar为longtext
- No bean named 'entityManagerFactory' is defined 异常怎么解决
- 为防止ssh数据缓存带来的问题一般会怎么做
- MBean问题
- Hibernate中可以把一张表的几个属性封装成一个list么?在线等!
- 高手求高手讲讲Hibernate的表的多对一关系,
- 关于Hibernate Criteria查询的排序问题
- 帮我看看这个程序,node.getAttributes().getLength()值怎么是零阿
- 紧急求助!!!!高手请进,WebLogic Server的问题!!!!!!1
- java调用.net webservice服务参数返回是空
- 2003 下安装2个mysql
- 请教一个dom4j解析xml的问题
package com.bryantd.hibernate.test;import com.bryantd.hibernate.entity.User;
import com.bryantd.hibernate.entity.Address;import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.Transaction;import java.util.*;public class UndirectionalOneToManyTest {
public static void saveObject(Session session, Object savedObject) {
Transaction transaction = session.beginTransaction();
session.save(savedObject);
transaction.commit();
}
public static void updateObject(Session session, Object updatedObject) {
Transaction transaction = session.beginTransaction();
session.saveOrUpdate(updatedObject);
transaction.commit();
}
public static void deleteObjects(Session session, String deletedObjectName) {
Transaction transaction = session.beginTransaction();
String hql = "delete " + deletedObjectName;
Query query = session.createQuery(hql);
query.executeUpdate();
transaction.commit();
}
public static void showUsers(Session session) {
Transaction transaction = session.beginTransaction();
System.out.println("Show Users:");
String hql = "from User";
Query query = session.createQuery(hql);
List list = query.list();
Iterator userIterator = list.listIterator();
while (userIterator.hasNext()) {
User user = (User)userIterator.next();
System.out.println(user.getName());
Set addresses = user.getAddresses();
Iterator addressIterator = addresses.iterator();
while (addressIterator.hasNext()) {
Address address = (Address)addressIterator.next();
System.out.println(address.getAddress());
}
}
System.out.println();
transaction.commit();
}
public static void showAddresses(Session session) {
Transaction transaction = session.beginTransaction();
System.out.println("Show Addresses:");
String hql = "from Address";
Query query = session.createQuery(hql);
List list = query.list();
Iterator addressIterator = list.iterator();
while (addressIterator.hasNext()) {
Address address = (Address)addressIterator.next();
System.out.println(address.getAddress());
User user = address.getUser();
if (user != null) {
System.out.println(user.getName());
}
}
System.out.println();
transaction.commit();
}
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
User user = new User("Bryantd", 23);
Address address1 = new Address("address1", "111111");
Address address2 = new Address("address2", "222222");
address1.setUser(user);
address2.setUser(user);
deleteObjects(session, "User");
deleteObjects(session, "Address");
saveObject(session, user);
saveObject(session, address1);
saveObject(session, address2);
user.getAddresses().add(address1);
user.getAddresses().add(address2);
updateObject(session, user);
showUsers(session);
showAddresses(session);
}
}
column="user_id"
not-null="true"/>在这个标签中加上 lazy="false"这个属性试一下
但是address的cascade='none'
当然出现你碰到的现象
主控方和级联操作是没有关系的阿。
user被保存的时候,为什么address方没有被保存?