`
tomEnjoy
  • 浏览: 141879 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

    Hibernate关联映射

    Hibernate 多对一外键单向关联 Hibernate 多对一连接表单向关联 Hibernate 多对多单向关联 Hibernate 一对一外键双向关联 Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向...

    Hibernate基于连接表的一对多单向关联

    NULL 博文链接:https://paladin1988.iteye.com/blog/1627678

    Hibernate关联关系映射目录

    │ ├─ 多对一外键单向关联 │ ├─ 多对一连接表单向关联 │ └─ 多对多单向关联 └─双向关联 ├─ 一对一外键双向关联 ├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对...

    Hibernate Annotation 基于连接表的单向一对多关联

    NULL 博文链接:https://paladin1988.iteye.com/blog/1634669

    hibernate学习笔记

    hibernate多对多关联映射(单向User----&gt;Role) 19 hibernate多对多关联映射(双向User&lt;----&gt;Role) 20 Hibernate的继承关系 21 每棵继承树映射成一张表(hibernate_extends_1) 22 每个子类映射成一张表(hibernate_...

    Hibernate_Annotation关联映射

    和其它许多批注一样,在多对多关联中很多值是自动生成,党双向多对多关联中没有定义任何物理映射时,Hibernate根据以下规则生成相应的值,关联表名:主表表名+下划线+从表表名,关联到主表的外键名:主表名+下划线+...

    Hibernate+中文文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate注释大全收藏

    这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....

    hibernate3.2中文文档(chm格式)

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    HibernateAPI中文版.chm

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate中文详细学习文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate 中文 html 帮助文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     7.1 建立多对一的单向关联关系  7.1.1 元素的not-null属性  7.1.2 级联保存和更新  7.2 映射一对多双向关联关系  7.2.1 元素的inverse属性  7.2.2 级联删除  7.2.3 父子关系  7.3 映射一对多双向自身关联...

    hibernate 体系结构与配置 参考文档(html)

    使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    最全Hibernate 参考文档

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...

    Hibernate教程

    8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...

    Hibernate3的帮助文档

    8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...

    hibernate3.04中文文档.chm

    8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...

Global site tag (gtag.js) - Google Analytics