多对多关系映射(many-to-many)

例子阐述:一个用户有多个角色,一个角色可以由多个用户扮演

public class User {
	private Integer id;
	private String name;
	private Set<Role>roles; 
	public User(){}
	public User(Integer id){
		this.id=id;
	}
	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;
	}
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
}

 

<hibernate-mapping>
	<!-- 每一个class类对应一个持久化对象 -->
	<class name="model.User" table="mw_user">
		<id name="id" column="id">
		<!-- 主键的生成策略 -->
			<generator class="identity"></generator>
		</id>
		<property name="name"></property>
		<set name="roles" table="mw_mapping">
			<key column="user_id"></key>
			<many-to-many class="model.Role" column="role_id"></many-to-many>
		</set>
		</class>
</hibernate-mapping>

 

public class Role {
	private int id;
	private String roleName;
	private Set<User> users;
	public Role(){};
	public Role(Integer id){
		this.id=id;
	}
	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 Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	
}

 

<hibernate-mapping>
	<!-- 每一个class类对应一个持久化对象 -->
	<class name="model.Role" table="mw_role">
		<id name="id" column="id">
		<!-- 主键的生成策略 -->
			<generator class="identity"></generator>
		</id>
		<property name="roleName"></property>
		<set name="users" table="mw_mapping">
			<key column="role_id"></key>
			<many-to-many class="model.User" column="user_id"></many-to-many>
		</set>
		</class>
</hibernate-mapping>

 

public static void main(String[] args) {
		Configuration configuration=new Configuration().configure();
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		Session session=sessionFactory.openSession();
		Transaction transaction=session.beginTransaction();
		User user=new User();
		user.setName("zhansan");
		Role role1=new Role();
		role1.setRoleName("manager");
		Role role2=new Role();
		role2.setRoleName("manager2");
		Set<Role> roles = new HashSet<Role>();
		session.save(role1);
		session.save(role2);
		roles.add(role1);
		roles.add(role2);
		user.setRoles(roles);
		session.save(user);
		transaction.commit();
		session.close();
	}

 

public static void main(String[] args) {
		Configuration configuration=new Configuration().configure();
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		Session session=sessionFactory.openSession();
		Transaction transaction=session.beginTransaction();
		User user=(User)session.get(User.class, new Integer(3));
		Set<Role>roles=user.getRoles();
		Iterator<Role> iterator=roles.iterator();
		while(iterator.hasNext()){
			Role role=iterator.next();
			System.out.println(role.getRoleName());
		}
		transaction.commit();
		session.close();
	}

 

你可能感兴趣的:(many-to-many)