作者:_MIN_Y | 来源:互联网 | 2023-09-24 12:40
双向多对一(一对多)关联是最最常见的关联关系javabean如下:****双向的一对多(多对一)*少的一端,需要对多的一端进行集合的引用*publicclassAccount{
双向多对一(一对多)关联是最最常见的关联关系
javabean如下:
/**
*
* 双向的一对多(多对一)
* 少的一端,需要对多的一端进行集合的引用
*/
public class Account {
private int id;
private String accName;
//对多端对象集合的引用
private Set setOrders;
public Account(){
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the accName
*/
public String getAccName() {
return accName;
}
/**
* @param accName the accName to set
*/
public void setAccName(String accName) {
this.accName = accName;
}
/**
* @return the setOrders
*/
public Set getSetOrders() {
return setOrders;
}
/**
* @param setOrders the setOrders to set
*/
public void setSetOrders(Set setOrders) {
this.setOrders = setOrders;
}
}
/**
*
* 双向的一对多(多对一)
* 多的一端需要对少的一端进行对象的引用
*/
public class Orders {
private int id;
private String orderNum;
private Date orderTime;
private Account account;
public Orders(){
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the orderNum
*/
public String getOrderNum() {
return orderNum;
}
/**
* @param orderNum the orderNum to set
*/
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
/**
* @return the orderTime
*/
public Date getOrderTime() {
return orderTime;
}
/**
* @param orderTime the orderTime to set
*/
public void setOrderTime(Date orderTime) {
this.orderTime = orderTime;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
映射文件:
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
生成的数据库表:
@Test
public void add(){
Transaction tx=null;
Session session=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Account account=new Account();
account.setAccName("lm");
Orders order1=new Orders();
order1.setOrderNum("100");
order1.setOrderTime(new Date());
//session.save(order1);
Orders order2=new Orders();
order2.setOrderNum("10061");
order2.setOrderTime(new Date());
//session.save(order2);
order1.setAccount(account);
order2.setAccount(account);
Set setOrders=new HashSet();
setOrders.add(order1);
setOrders.add(order2);
account.setSetOrders(setOrders);
session.save(account);
tx.commit();
}catch(HibernateException he){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
自身关联:
javabean如下:
/**
*
* 双向一对多或多对一 自身关联
*
*/
public class Category {
private int id;
private String name;
//如果把Category看成是多的一端
private Category parent;
//如果把Category看成是少的一端,则需要对多的一端进行对象集合的引用
private Set clist;
public Category(){
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the parent
*/
public Category getParent() {
return parent;
}
/**
* @param parent the parent to set
*/
public void setParent(Category parent) {
this.parent = parent;
}
/**
* @return the clist
*/
public Set getClist() {
return clist;
}
/**
* @param clist the clist to set
*/
public void setClist(Set clist) {
this.clist = clist;
}
}
映射文件
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
数据库表:
测试代码:
@Test
public void add(){
Transaction tx=null;
Session session=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
//添加一级分类
Category c1=new Category();
c1.setName("computer");
session.save(c1);
//添加二级分类
Category c2=new Category();
c2.setName("java");
c2.setParent(c1);
session.save(c2);
//添加二级分类
Category c3=new Category();
c3.setName(".net");
c3.setParent(c1);
session.save(c3);
tx.commit();
}catch(HibernateException he){
if(tx!=null){
tx.rollback();
}
he.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
数据结构: