Hibernate学习整理(一对多,多对多问题)

一对多
实体类(一方)
package com.wlc.pojo;

import java.util.Set;

/**
 * 大分类 
 *  一方
 * @author Administrator
 *
 */
public class BigClass {
	//id
	private int id ;
	//name
	private String name ;
	
	//维护多的一方
	private Set<SmallClass> scs ;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "BigClass [id=" + id + ", name=" + name + "]";
	}
	public BigClass() {
		
	}
	public Set<SmallClass> getScs() {
		return scs;
	}
	public void setScs(Set<SmallClass> scs) {
		this.scs = scs;
	}
	
	
	

}
实体类(多方)
package com.wlc.pojo;

/**
 * 相对于大分类来说是多的一方
 * 相对于商品来说是一的一方
 * @author Administrator
 *
 */
public class SmallClass {
	//小分类id
	private  int id ;
	
	//小分类名称
	private String name ;
	
	//关联大分类的主键
	private int classtable_id ;
	
	
	//关联大分类
	//多对一
	private BigClass  bc ;
	

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getClasstable_id() {
		return classtable_id;
	}

	public void setClasstable_id(int classtable_id) {
		this.classtable_id = classtable_id;
	}
	
	


	public SmallClass() {
		
	}
	
	

	public BigClass getBc() {
		return bc;
	}

	public void setBc(BigClass bc) {
		this.bc = bc;
	}

	@Override
	public String toString() {
		return "SmallClass [id=" + id + ", name=" + name + ", classtable_id=" + classtable_id + ", bc=" + bc + "]";
	}

	



	

	

	

	


}
一方(hbm.xml)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<!-- name指定实体类,table指定数据表名 -->
    <class name="com.wlc.pojo.BigClass" table="T_CLASSTABLE">
    	<!-- 主键字段映射描述 -->
        <id  name="id" column="id">
		<!-- 主键自增 -->
		<generator class="native"></generator>
		</id>
        <!-- 非主键映射描述 -->
        <property name="name" type="java.lang.String">
            <column name="NAME"/>
        </property>
        <!-- 一对多 -->
        <!-- 其中name值代表关联的 -->
        <set name="scs" cascade="all">
        <!-- column是两个表的关联字段即外键 -->
        <key column="classtable_id"></key>
        <one-to-many class="com.wlc.pojo.SmallClass"></one-to-many>
        </set>
    </class>
</hibernate-mapping>
多方(hbm.xml)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<!-- name指定实体类,table指定数据表名 -->
    <class name="com.wlc.pojo.SmallClass" table="T_CATEGORY">
    	<!-- 主键字段映射描述 -->
    	<id  name="id" column="id">
		<!-- 主键自增 -->
		<generator class="native"></generator>
		</id>
        <!-- 非主键映射描述 -->
        <property name="name" type="java.lang.String">
            <column name="NAME"/>
        </property>
        
        <!-- 多对一 其中name  对应 关联的大分类-->
        <many-to-one name="bc"   class="com.wlc.pojo.BigClass"   column="CLASSTABLE_ID" ></many-to-one>
        
        
    </class>
</hibernate-mapping>
表结构


测试
package com.wlc.tests;

import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import com.wlc.pojo.BigClass;
import com.wlc.pojo.Role;
import com.wlc.pojo.SmallClass;
import com.wlc.pojo.User;
import com.wlc.utils.HibernateUtil2;

public class MTest {

	public static void main(String[] args) {
		Show();

	}

	
	public static void Show() {
		Configuration cfg = new Configuration().configure();

		SessionFactory sessionFactory = cfg.buildSessionFactory();

		Session session = sessionFactory.openSession();
		String hql="from BigClass where id=?";
		Query query =session.createQuery(hql);
		//其中1代表的是ID
		query.setParameter(0, 1);
		List<BigClass> users =query.list();
		BigClass u  =users.get(0);
		Set<SmallClass>  ss =u.getScs();
		for (SmallClass smallClass : ss) {
			System.out.println(smallClass);
		}
		session.close();
		sessionFactory.close();

	}

}
多对多
实体类
package com.wlc.pojo;

import java.util.HashSet;
import java.util.Set;

/**
 * 角色
 * @author Administrator
 *
 */
public class Role {
	private int id ;
	
	private  String  rolename ;
	
	private  String rolepass  ;
	
	//一对多
	private Set<User> setUser  = new HashSet<User>();
	

	public Set<User> getSetUser() {
		return setUser;
	}

	public void setSetUser(Set<User> setUser) {
		this.setUser = setUser;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	public String getRolepass() {
		return rolepass;
	}

	public void setRolepass(String rolepass) {
		this.rolepass = rolepass;
	}

	@Override
	public String toString() {
		return "Role [id=" + id + ", rolename=" + rolename + ", rolepass=" + rolepass + "]";
	}

	
	

}
package com.wlc.pojo;

import java.util.HashSet;
import java.util.Set;

/*
 *用户 
 */
public class User {
	private int id ; 
	
	private String username ;
	
	private String password ;
	
	//一对多
	private Set<Role> setRole  = new HashSet<Role>();

	public Set<Role> getSetRole() {
		return setRole;
	}

	public void setSetRole(Set<Role> setRole) {
		this.setRole = setRole;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}

	
	

}
User.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC   
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.wlc.pojo.User" table="t_user">
		<id  name="id" column="id">
		<!-- 主键自增 -->
		<generator class="native"></generator>
		</id>
		<property name="username" column="username"></property>
		<property name="password" column="password"></property>	
		<set name="setRole" table="role_user">
		<!-- 用户在第三表的外键 -->
		<key column="userid"></key>
		<many-to-many class="com.wlc.pojo.Role" column="roleid"></many-to-many>
		</set>
	</class>
</hibernate-mapping> 



Role.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC   
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.wlc.pojo.Role" table="t_role">
		<id  name="id" column="id">
		<!-- 主键自增 -->
		<generator class="native"></generator>
		</id>
		<property name="rolename" column="rolename"></property>
		<property name="rolepass" column="rolepass"></property>	
		<!-- 多对多 -->
	<set name="setUser" table="role_user">
	<!-- 角色在第三表中的外键 -->
	<key column="roleid"></key>
	<many-to-many class="com.wlc.pojo.User" column="userid"></many-to-many>
	</set>	
	</class>
</hibernate-mapping> 



测试

package com.wlc.tests;

import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import com.wlc.pojo.BigClass;
import com.wlc.pojo.Role;
import com.wlc.pojo.SmallClass;
import com.wlc.pojo.User;
import com.wlc.utils.HibernateUtil2;

public class MTest {

	public static void main(String[] args) {
		Show();

	}

	
	public static void Show() {
		Configuration cfg = new Configuration().configure();

		SessionFactory sessionFactory = cfg.buildSessionFactory();

		Session session = sessionFactory.openSession();
		//(通过一个用户查找有多个角色)
		String hql="from User where id=?";
		Query query =session.createQuery(hql);
		//其中1代表的是ID
		query.setParameter(0, 1);
		List<User> users =query.list();
		//看看zs ID=1的扮演了几个角色
		User u  =users.get(0);
		Set<Role> roles =u.getSetRole();
		for (Role role : roles) {
			System.out.println(role);
		}
		//通过1个角色查看有几个人扮演
		String hql2="from Role where id=?";
		Query query2 =session.createQuery(hql2);
		query2.setParameter(0, 1);
		List<Role> roes =query2.list();
		Role re =roes.get(0);
		Set<User> uu =re.getSetUser();
		for (User user : uu) {
			System.out.println(user);
		}
		session.close();
		sessionFactory.close();

	}

}
数据库









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值