hibernate多对多单向连接表关联

多对多单向连接表关联:只要一个端有Set就可以了,在<set>的属性中指明POJO中的Set集合,对应的链接表名,子元素<key>声明本端对应的连接表中的主键,<many-to-many>声明对应的连接表中的另一端的主键及其对应的POJO类.

以mis系统中的role(角色)和action(权限)表为例:
一个Role可以有多个Action, 一个Action也可以对应多个Role;
POJO如下:

package mis.hibernate.model;

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


/**
* Role entity. @author MyEclipse Persistence Tools
*/

public class Role implements java.io.Serializable {

private static final long serialVersionUID = 7198786374572086190L;

private Integer id;
private String name;
private String note;
private Integer flag;
private Integer version;
private Set actions = new HashSet(0);


// Constructors

/** default constructor */
public Role() {
}

/** minimal constructor */
public Role(String name) {
this.name = name;
}

/** full constructor */
public Role(String name, String note, Integer flag, Integer version, Set actions) {
this.name = name;
this.note = note;
this.flag = flag;
this.version = version;
this.actions = actions;
}


// Property accessors

public Integer getId() {
return this.id;
}

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

public String getName() {
return this.name;
}

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

public String getNote() {
return this.note;
}

public void setNote(String note) {
this.note = note;
}

public Integer getFlag() {
return this.flag;
}

public void setFlag(Integer flag) {
this.flag = flag;
}

public Integer getVersion() {
return this.version;
}

public void setVersion(Integer version) {
this.version = version;
}

public Set getActions() {
return this.actions;
}

public void setActions(Set actions) {
this.actions = actions;
}
}


package mis.hibernate.model;

/**
* Action entity.
*
* @author MyEclipse Persistence Tools
*/

public class Action implements java.io.Serializable {

// Fields

private Integer id;
private String name;
private String displayName;
private String description;
private Integer parentId;
private String url;
private Integer flag;
private Integer version;

// Constructors

/** default constructor */
public Action() {
}

/** minimal constructor */
public Action(String name) {
this.name = name;
}

/** full constructor */
public Action(String name, String displayName, String description,
Integer parentId, String url, Integer flag, Integer version) {
this.name = name;
this.displayName = displayName;
this.description = description;
this.parentId = parentId;
this.url = url;
this.flag = flag;
this.version = version;
}

// Property accessors

public Integer getId() {
return this.id;
}

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

public String getName() {
return this.name;
}

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

public String getDisplayName() {
return this.displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

public Integer getParentId() {
return this.parentId;
}

public void setParentId(Integer parentId) {
this.parentId = parentId;
}

public String getUrl() {
return this.url;
}

public void setUrl(String url) {
this.url = url;
}

public Integer getFlag() {
return this.flag;
}

public void setFlag(Integer flag) {
this.flag = flag;
}

public Integer getVersion() {
return this.version;
}

public void setVersion(Integer version) {
this.version = version;
}

}


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>
<class name="mis.hibernate.model.Role" table="role" catalog="mis2009">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="128" not-null="true" unique="true">
<comment>权限组名称</comment>
</column>
</property>
<property name="note" type="java.lang.String">
<column name="note" length="128">
<comment>备注</comment>
</column>
</property>
<property name="flag" type="java.lang.Integer">
<column name="flag">
<comment>标识</comment>
</column>
</property>
<property name="version" type="java.lang.Integer">
<column name="version">
<comment>版本号</comment>
</column>
</property>
<set name="actions" table="role_action">
<key column="roleId"></key>
<many-to-many column="actionId" class="mis.hibernate.model.Action">
</many-to-many>
</set>
</class>
</hibernate-mapping>


<?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>
<class name="mis.hibernate.model.Action" table="action" catalog="mis2009">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="128" not-null="true" unique="true">
<comment>权限名称</comment>
</column>
</property>
<property name="displayName" type="java.lang.String">
<column name="displayName" length="128">
<comment>显示到界面的名称,用中文表示更直观</comment>
</column>
</property>
<property name="description" type="java.lang.String">
<column name="description" length="128">
<comment>描述</comment>
</column>
</property>
<property name="parentId" type="java.lang.Integer">
<column name="parentId">
<comment>父权限节点Id</comment>
</column>
</property>
<property name="url" type="java.lang.String">
<column name="url" length="128">
<comment>对应的url地址</comment>
</column>
</property>
<property name="flag" type="java.lang.Integer">
<column name="flag">
<comment>标识</comment>
</column>
</property>
<property name="version" type="java.lang.Integer">
<column name="version">
<comment>版本号</comment>
</column>
</property>
</class>
</hibernate-mapping>


hql查询语句:

select distinct action from Action action,Role role where action in elements(role.actions) and role.id=5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值