100分!跪求hibernate4 用户、组、权限模块,双向一对多增、删、改、查,操作实例!
小弟初学SSH,就接手了一个用户组权限模块的任务,到现在为止一宿没睡已经被hibernate的关联映射及相关的增删改查折腾疯了,跪求大神指条活路,救命之恩当100分相报啊!带犯人上堂!该用户角色权限模块共有5张表:
用户信息表
yhxx : 有字段 yhid(主键),yhmc
组信息表
zxx : 有字段 zid(主键),zmc
权限信息表
qxxx : 有字段 qxid(主键),qxmc
用户组
yhzxx : 是yhxx和zxx中间表,有字段 yhzid(主键),yhid(yhxx外键),zid(zxx外键)
组权限
zqxxx : 是zxx和qxxx中间表,有字段 zqxid(主键),zid(zxx外键),qxid(qxxx外键)
用户信息 组信息 是双向多对多关系,映射到hibernate就是两个双向一对多关系
组信息 权限信息 也是双向多对多关系,映射到hibernate也是两个双向一对多关系
(应该没理解错吧?)
用户bean:
public class Yhxx implements java.io.Serializable {
private long yhid;
private String yhxm;
private Integer yhxb;
private Set<Yhzxx> yhzxxes = new HashSet();
get..set..
}
组信息bean:
public class Zxx implements java.io.Serializable {
private long zid;
private String zmc;
private Set<Zqxxx> zqxxxes = new HashSet();
private Set<Yhzxx> yhzxxes = new HashSet();
get..set..
}
权限信息bean:
public class Qxxx implements java.io.Serializable {
private long qxid;
private String qxmc;
private Set<Zqxxx> zqxxxes = new HashSet();
get..set..
}
用户组信息bean:
public class Yhzxx implements java.io.Serializable {
private long yhzid;
private Zxx zxx;
private Yhxx yhxx;
get..set..
}
组权限信息bean:
public class Zqxxx implements java.io.Serializable {
private long zqxid;
private Qxxx qxxx;
private Zxx zxx;
get..set..
}
用户信息.hbm.xml
<hibernate-mapping package="com.sf.entity">
<class name="com.sf.entity.Yhxx" table="yhxx" catalog="sf">
<id name="yhid" type="long">
<column name="yhid" />
<generator class="identity" />
</id>
<property name="yhxm" type="string">
<column name="yhxm" length="50" not-null="true" />
</property>
<property name="yhxb" type="java.lang.Integer">
<column name="yhxb" />
</property>
<set name="yhzxxes" table="yhzxx" cascade="all" inverse="true" lazy="false" fetch="select">
<key>
<column name="yhid" not-null="true" />
</key>
<one-to-many class="com.sf.entity.Yhzxx" />
</set>
</class>
</hibernate-mapping>
组信息.hbm.xml
<hibernate-mapping package="com.sf.entity">
<class name="com.sf.entity.Zxx" table="zxx" catalog="sf">
<id name="zid" type="long">
<column name="zid" />
<generator class="identity" />
</id>
<property name="zmc" type="string">
<column name="zmc" length="50" not-null="true" />
</property>
<set name="zqxxxes" table="zqxxx" cascade="all" inverse="true" lazy="true" fetch="select">
<key>
<column name="zid" not-null="true" />
</key>
<one-to-many class="com.sf.entity.Zqxxx" />
</set>
<set name="yhzxxes" table="yhzxx" cascade="all" inverse="true" lazy="true" fetch="select">
<key>
<column name="zid" not-null="true" />
</key>
<one-to-many class="com.sf.entity.Yhzxx" />
</set>
</class>
</hibernate-mapping>
权限信息.hbm.xml
<hibernate-mapping package="com.sf.entity">
<class name="com.sf.entity.Qxxx" table="qxxx" catalog="sf">
<id name="qxid" type="long">
<column name="qxid" />
<generator class="identity" />
</id>
<property name="qxmc" type="string">
<column name="qxmc" length="50" not-null="true" />
</property>
<set name="zqxxxes" table="zqxxx" cascade="all" inverse="true" lazy="true" fetch="select">
<key>
<column name="qxid" not-null="true" />
</key>
<one-to-many class="com.sf.entity.Zqxxx" />
</set>
</class>
</hibernate-mapping>
用户组信息.hbm.xml
<hibernate-mapping package="com.sf.entity">
<class name="com.sf.entity.Yhzxx" table="yhzxx" catalog="sf">
<id name="yhzid" type="long">
<column name="yhzid" />
<generator class="identity" />
</id>
<many-to-one name="zxx" class="com.sf.entity.Zxx" cascade="all" fetch="select">
<column name="zid" not-null="true" />
</many-to-one>
<many-to-one name="yhxx" class="com.sf.entity.Yhxx" cascade="all" fetch="select">
<column name="yhid" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
组权限信息.hbm.xml
<hibernate-mapping package="com.sf.entity">
<class name="com.sf.entity.Zqxxx" table="zqxxx" catalog="sf">
<id name="zqxid" type="long">
<column name="zqxid" />
<generator class="identity" />
</id>
<many-to-one name="qxxx" class="com.sf.entity.Qxxx" cascade="all" fetch="select">
<column name="qxid" not-null="true" />
</many-to-one>
<many-to-one name="zxx" class="com.sf.entity.Zxx" cascade="all" fetch="select">
<column name="zid" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
这是用来查询用户信息,及该用户所在的组:
public List<Yhxx> findAll() {
String hql = "from Yhxx";
return (List<Yhxx>)sessionFactory.getCurrentSession().createQuery(hql).list();
}
这是上面HQL打印出的SQL:
Hibernate:
select
yhxx0_.yhid as yhid1_2_,
yhxx0_.yhxm as yhxm2_2_,
yhxx0_.yhxb as yhxb3_2_
from
sf.yhxx yhxx0_
Hibernate:
select
yhzxxes0_.yhid as yhid3_2_1_,
yhzxxes0_.yhzid as yhzid1_3_1_,
yhzxxes0_.yhzid as yhzid1_3_0_,
yhzxxes0_.zid as zid2_3_0_,
yhzxxes0_.yhid as yhid3_3_0_
from
sf.yhzxx yhzxxes0_
where
yhzxxes0_.yhid=?
Hibernate:
select
yhzxxes0_.yhid as yhid3_2_1_,
yhzxxes0_.yhzid as yhzid1_3_1_,
yhzxxes0_.yhzid as yhzid1_3_0_,
yhzxxes0_.zid as zid2_3_0_,
yhzxxes0_.yhid as yhid3_3_0_
from
sf.yhzxx yhzxxes0_
where
yhzxxes0_.yhid=?
Hibernate:
select
yhzxxes0_.yhid as yhid3_2_1_,
yhzxxes0_.yhzid as yhzid1_3_1_,
yhzxxes0_.yhzid as yhzid1_3_0_,
yhzxxes0_.zid as zid2_3_0_,
yhzxxes0_.yhid as yhid3_3_0_
from
sf.yhzxx yhzxxes0_
where
yhzxxes0_.yhid=?
用户编号:1 用户名称:曹操
用户组编号:1 用户组_用户编号:1
用户组编号:1 用户组_用户编号:1
用户组编号:1 用户组_用户编号:1
用户组编号:1 用户组_用户编号:1
用户组编号:1 用户组_用户编号:1
各位大神也看出来了,查到用户组信息中间表的时候已经变成了死循环,小弟实在是无力解决了,
在此跪求大神基于小弟这些表、关系、bean等信息,赐一套完整的用户、组、权限的增删改查操作实例吧~! hibernate ssh hql java --------------------编程问答-------------------- 帮你顶一下吧 --------------------编程问答-------------------- 死循环,?双向一对多 然后配置了级联 不死循环才怪。。
--------------------编程问答-------------------- 看这几个拼音。。。
我把我项目里面的映射文件关于权限的部分抽出给你看看。
User{
private Set<Role> roles = new HashSet<Role>();// 拥有的角色
}
User.hbm.xml:
<!--roles属性,User与Role多对多 -->
<set name="roles" table="user_role" lazy="false">
<key column="userId"/>
<many-to-many class="Role" column="roleId" />
</set>
---------------------------------------------------------------------
Privilege{
private Set<Role> roles=new HashSet<Role>();//一个权限可以有多个角色
private Privilege parent;//上级权限
private Set<Privilege> children=new HashSet<Privilege>();//下级权限
}
-------------------------------------------------------------
Privilege.hbm.xml:
<!--roles属性,Privilege与Role的多对多关系 -->
<set name="roles" table="role_privilege">
<key column="privilegeId"/>
<many-to-many class="Role" column="roleId"/>
</set>
<!--parent Privilege与上级权限多对一 -->
<many-to-one name="parent" class="Privilege" column="parentId"/>
<!-- children 属性,Privilege对下级权限一对多,级联删除下级权限 -->
<set name="children" order-by="id" lazy="false" >
<key column="parentId"></key>
<one-to-many class="Privilege"/>
</set>
------------------------------------------------------------------------
Role{
private Set<User> users = new HashSet<User>();// 角色所对应的用户
private Set<Privilege> privileges=new HashSet<Privilege>();
}
----------------------------------------------------------
Role.hbm.xml:
<!-- users属性,Role与User的多对多关系 -->
<set name="users" table="user_role">
<key column="roleId"/>
<many-to-many class="User" column="userId"/>
</set>
<!--privileges属性,Role与Privilege的多对多关系 -->
<set name="privileges" table="role_privilege" lazy="false">
<key column="roleId"/>
<many-to-many class="Privilege" column="privilegeId"/>
</set>
补充:Java , Java EE