Many-to-Many

hibernate多对多关联映射(单向User---->Role)
<class name="com.bjsxt.hibernate.User" table="t_user">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="roles" table="t_user_role">
			<key column="userid"/>
			<many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/>
		</set>
	</class>


	<class name="com.bjsxt.hibernate.Role" table="t_role">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
	</class>


-----------------------------------------------------------------
hibernate多对多关联映射(双向User<---->Role)

映射方法:
<key>中column属性值要与单向关联中的<many-to-many>标签中的column属性值一致
在<many-to-many>中的column属性值要与单向关联中<key>标签的column属性值一致


也可以采用inverse和cascade

 
<class name="com.bjsxt.hibernate.Role" table="t_role">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="users" table="t_user_role" order-by="userid"><!-- orderby 用来排序 -->
			<key column="roleid"/>
			<many-to-many class="com.bjsxt.hibernate.User" column="userid"/>
		</set>
	</class>


<class name="com.bjsxt.hibernate.User" table="t_user">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="roles" table="t_user_role">
			<key column="userid"/>
			<many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/>
		</set>
	</class>

你可能感兴趣的:(java,Hibernate)