在本文中主要实现数据的级联插入下面模拟的场景是用户有多个订单在插入用户的时候将用户的订单添加到订单表中
下面是User 和 Order两个pojo类的代码
[java]
public class Order {
private int id;
private int whoid; // thi id of the orders User
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
thisuser = user;
}
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public int getWhoid() {
return whoid;
}
public void setWhoid(int whoid) {
thiswhoid = whoid;
}
}
public class Order {
private int id;
private int whoid; // thi id of the orders User
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
thisuser = user;
}
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public int getWhoid() {
return whoid;
}
public void setWhoid(int whoid) {
thiswhoid = whoid;
}
}
[java]
public class User {
private int id;
private String username;
private String password;
//这里需要注意的是使用集合必须使用Set而不能用HashSet否者在插入数据的时候会抛出一个参数不匹配异常
private Set<Order> orders;
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
thisorders = orders;
}
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
}
public class User {
private int id;
private String username;
private String password;
//这里需要注意的是使用集合必须使用Set而不能用HashSet否者在插入数据的时候会抛出一个参数不匹配异常
private Set<Order> orders;
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
thisorders = orders;
}
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
}
下面这个是UserDao这个就没有什么好说的了
[java]
import orghibernateSession;
import orghibernateTransaction;
import comexampledomainUser;
public class UserDao {
private Session session;
public UserDao(Session session )
{
thissession = session;
}
public void savaUser(User user)
{
Transaction tx = sessionbeginTransaction();
try
{
sessionsave(user);
mit();
}
catch(Exception ex)
{
exprintStackTrace();
txrollback();
}
}
}
import orghibernateSession;
import orghibernateTransaction;
import comexampledomainUser;
public class UserDao {
private Session session;
public UserDao(Session session )
{
thissession = session;
}
public void savaUser(User user)
{
Transaction tx = sessionbeginTransaction();
try
{
sessionsave(user);
mit();
}
catch(Exception ex)
{
exprintStackTrace();
txrollback();
}
}
}
下面的这两个hbmxml文件十分重要
Userhbmxml
[html]
<?xml version= encoding=UTF?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
hiber/hibernatemappingdtd>
<hibernatemapping>
<class name=comexampledomainUser table=user >
<id name=id column=id type=javalangInteger>
<generator class=increment></generator>
</id>
<property name=username column=username type=javalangString></property>
<property name=password column=password type=javalangString></property>
<set name=orders cascade=saveupdate inverse=false >
<key column=whoid></key>
<onetomany class=comexampledomainOrder/>
</set>
</class>
</hibernatemapping>
<?xml version= encoding=UTF?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
hiber/hibernatemappingdtd>
<hibernatemapping>
<class name=comexampledomainUser table=user >
<id name=id column=id type=javalangInteger>
<generator class=increment></generator>
</id>
<property name=username column=username type=javalangString></property>
<property name=password column=password type=javalangString></property>
<set name=orders cascade=saveupdate inverse=false >
<key column=whoid></key>
<onetomany class=comexampledomainOrder/>
</set>
</class>
</hibernatemapping>
其中要理解的就是set这个标签这个标签对应了User对象中的集合对象name依然是User中属性的名字inserse为false表示了这种一对多的关系由User来维护默认值就是false那么那一方就有责任负责之间的关联关系说白了就是hibernate如何生成Sql来维护关联的记录比如说当User的状态发生了改变这种改变会延伸到每一个关联到这个User的Order上
key标签用来表明这两个关系是通过那个属性来连接的那么Order表中的外键是whoid那么这两个关系是通过whoid来维护的onetomany表示一对多的关系其中class表示Set集合中的对象类型也可以这么理解即一对多的关系多的一方的类型是什么
Orderhbmxml
[html]
<?xml version= encoding=UTF?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
hiber/hibernatemappingdtd>
<hibernatemapping>
<class name=comexampledomainOrder table=Orders>
<id name=id column=id type=javalangInteger>
<generator class=increment></generator>
</id>
<manytoone name=user class=comexampledomainUser column=whoid></manytoone>
</class>
</hibernatemapping>
<?xml version= encoding=UTF?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
hiber/hibernatemappingdtd>
<hibernatemapping>
<class name=comexampledomainOrder table=Orders>
<id name=id column=id type=javalangInteger>
<generator class=increment></generator>
</id>
<manytoone name=user class=comexampledomainUser column=whoid></manytoone>
</class>
</hibernatemapping>
其中的 manytoone表示多对一的关系name表示Order中表示用来关联用户的那个属性的名称class表示一的那一方的类型column表示关联关系中用来连接的列这里仍然是whoid
下面是测试代码
[java]
public class Test {
public static void main(String args[])
{
User user = new User();
usersetUsername(Neway);
usersetPassword(admin);
HashSet<Order> orders = new HashSet<Order>();
Order o = new Order();
Order o = new Order();
Order o = new Order();
ordersadd(o);
ordersadd(o);
ordersadd(o);
usersetOrders(orders);
Session session = HibernateSessionFactorygetSession();
UserDao dao = new UserDao(session);
daosavaUser(user);
}
}
public class Test {
public static void main(String args[])
{
User user = new User();
usersetUsername(Neway);
usersetPassword(admin);
HashSet<Order> orders = new HashSet<Order>();
Order o = new Order();
Order o = new Order();
Order o = new Order();
ordersadd(o);
ordersadd(o);
ordersadd(o);
usersetOrders(orders);
Session session = HibernateSessionFactorygetSession();
UserDao dao = new UserDao(session);
daosavaUser(user);
}
}