hibernate双向多对一(一对多)及自身一对多关联

双向多对一(一对多)关联是最最常见的关联关系

hibernate双向多对一(一对多)及自身一对多关联_第1张图片

javabean如下:

/**
 * 
 * 双向的一对多(多对一)
 * 少的一端,需要对多的一端进行集合的引用
 */
public class Account {

	private int id;
	private String accName;
	
	//对多端对象集合的引用
	private Set<Orders> 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<Orders> getSetOrders() {
		return setOrders;
	}

	/**
	 * @param setOrders the setOrders to set
	 */
	public void setSetOrders(Set<Orders> 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;
	}
	
}


映射文件:

<?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>
	<!-- name属性指定类名(全限定名) table指明表名,不指明table数据默认的表名和实体名一致 -->
    <class name="com.entity.Account" table="acc_tab">
    	<!-- type指明当前字段的类型    name对应实体中的属性名 -->
        <id type="integer" name="id">
        	<!-- 提供ID自增的策略  native会根据数据库自行判断 -->
            <generator class="native"/>
        </id>
        <property name="accName" type="string"></property>
        <set name="setOrders" cascade="all" inverse="true">
        	<key column="acc_id"></key>
        	<one-to-many class="com.entity.Orders"/>
        </set>
      
        
    </class>
</hibernate-mapping>

<?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>
	<!-- name属性指定类名(全限定名) table指明表名,不指明table数据默认的表名和实体名一致 -->
    <class name="com.entity.Orders" table="order_tab">
    	<!-- type指明当前字段的类型    name对应实体中的属性名 -->
        <id type="integer" name="id">
        	<!-- 提供ID自增的策略  native会根据数据库自行判断 -->
            <generator class="native"/>
        </id>
        <property name="orderNum" type="string"></property>
       	<property name="orderTime" type="timestamp"></property>
       	
       	<many-to-one name="account" column="acc_id"></many-to-one>
       	
    </class>
</hibernate-mapping>

生成的数据库表:

hibernate双向多对一(一对多)及自身一对多关联_第2张图片

hibernate双向多对一(一对多)及自身一对多关联_第3张图片

@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<Orders> setOrders=new HashSet<Orders>();
			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);
		}
	}

自身关联:

hibernate双向多对一(一对多)及自身一对多关联_第4张图片

javabean如下:

/**
 * 
 * 双向一对多或多对一 自身关联
 * 
 */
public class Category {

	private int id;
	private String name;
	
	//如果把Category看成是多的一端
	private Category parent;
	
	//如果把Category看成是少的一端,则需要对多的一端进行对象集合的引用
	private Set<Category> 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<Category> getClist() {
		return clist;
	}

	/**
	 * @param clist the clist to set
	 */
	public void setClist(Set<Category> clist) {
		this.clist = clist;
	}
	
}

映射文件

<?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>
	<!-- name属性指定类名(全限定名) table指明表名,不指明table数据默认的表名和实体名一致 -->
    <class name="com.entity.Category" table="cate_tab">
    	<!-- type指明当前字段的类型    name对应实体中的属性名 -->
        <id type="integer" name="id">
        	<!-- 提供ID自增的策略  native会根据数据库自行判断 -->
            <generator class="native"/>
        </id>
        <property name="name" type="string"></property>
       <set name="clist" inverse="true">
       		<key column="parent_id"></key>
       		<!-- 配置一对多的关联映射 -->
       		<one-to-many class="com.entity.Category"/>
       </set>
       <many-to-one name="parent" column="parent_id"></many-to-one>
        
    </class>
</hibernate-mapping>

数据库表:

hibernate双向多对一(一对多)及自身一对多关联_第5张图片

测试代码:

@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);
		}
	}
	


数据结构:














你可能感兴趣的:(hibernate双向多对一(一对多)及自身一对多关联)