0

Problem with many-to-many relation with zk+spring+hibernate

asked 2011-06-14 09:55:36 +0800

hamada14br gravatar image hamada14br
36

Hi,

I want to create a Utilisateur with assign or affect a name of Usertype(by listbox) to Utilisateur,

Utilisateur.hbm.xml

<hibernate-mapping>
    <class name="com.caciopee.beans.Utilisateur" table="utilisateur" schema="public" lazy="false">
        <id name="iduser" type="int">
            <column name="iduser" />
            <generator class="increment" />
        </id>
        <property name="nomuser" type="string">
            <column name="nomuser" length="254" />
        </property>
        <property name="prenomuser" type="string">
            <column name="prenomuser" length="254" />
        </property>
        <property name="passwrduser" type="string">
            <column name="passwrduser" length="254" />
        </property>
        <property name="emailuser" type="string">
            <column name="emailuser" length="254" />
        </property>
        <property name="teluser" type="string">
            <column name="teluser" length="254" />
        </property>
        <set name="usertypes" table="user_usertype" inverse="true" lazy="false" >
            <key>
                <column name="iduser" not-null="true" />
            </key>
            <many-to-many entity-name="com.caciopee.beans.Usertype">
                <column name="idusertype" not-null="true" />
            </many-to-many>
        </set>
        <set name="projects" table="user_project" inverse="false" lazy="false" fetch="join">
            <key>
                <column name="iduser" not-null="true" />
            </key>
            <many-to-many entity-name="com.caciopee.beans.Project">
                <column name="idproj" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

Usertype.hbm.xml

<hibernate-mapping>
    <class name="com.caciopee.beans.Usertype" table="usertype" schema="public" lazy="false">
        <id name="idusertype" type="int">
            <column name="idusertype" />
            <generator class="increment" />
        </id>
        <property name="nomusertype" type="string">
            <column name="nomusertype" length="254" />
        </property>
        <property name="roleusertype" type="string">
            <column name="roleusertype" length="254" />
        </property>
        <set name="utilisateurs" table="user_usertype" inverse="false" lazy="false" >
            <key>
                <column name="idusertype" not-null="true" />
            </key>
            <many-to-many entity-name="com.caciopee.beans.Utilisateur">
                <column name="iduser" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

My DAO
UtilisateurDAOImpl.java

package com.caciopee.dao;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.hibernate.*;

import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.caciopee.beans.Usertype;
import com.caciopee.beans.Utilisateur;

public class UtilisateurDAOImpl extends HibernateDaoSupport implements InterfaceDAO{

	@Override
	public void save(Object inst) {
		// TODO Auto-generated method stub
		Utilisateur util = (Utilisateur) inst;
		this.getHibernateTemplate().save(util);
		
	}
	
//	public void saveOrUpdate(Utilisateur util,String nomuser,Set usertypes){
//		util.setNomuser(nomuser);
//		util.setUsertypes(usertypes);
//		
//		
//	}

	@Override
	public void update(Object inst) {
		// TODO Auto-generated method stub
		Utilisateur util = (Utilisateur) inst;
		this.getHibernateTemplate().update(util);
	}

	@Override
	public void delete(Object inst) {
		// TODO Auto-generated method stub
		Utilisateur util = (Utilisateur) inst;
		this.getHibernateTemplate().delete(util);
	}

	@Override
	public List findAll() {
		// TODO Auto-generated method stub
		return (List) this.getHibernateTemplate().loadAll(Utilisateur.class);
	}
	
	
	public List<Utilisateur> getAllUsers() {
		List<Utilisateur> userList = getHibernateTemplate().find("from Utilisateur usr left join fetch usr.usertypes");
		
		return userList;
	}
	
	public List<Usertype> getAllUsertypes()
	{
		List<Usertype> usertypeList = getHibernateTemplate().find("from Usertype usertypes");
		
		return usertypeList;
	}
}

UtilisateurPresentation.java (controlller)

public void onClick$add() {
		
		Set<Usertype> usertypes = new HashSet<Usertype>();

                Usertupe usert=new Usertype();
                usert.setNomusertype(usert.getNomusertype);
                usert.setRoleusertype(usert.getRoleusertype);
                usertypes.add(usert);
         	
		Utilisateur utilisateur=new Utilisateur();
		utilisateur.setUsertypes(usertypes);
		
		UtilisateurServiceImpl manager = ServiceLocator.getUtilisateurServiceImpl();
		manager.save(utilisateur);
   }

public List getAllUsertypes() {
		 UsertypeServiceImpl manager = ServiceLocator.getUsertypeServiceImpl();
		 Collection allUsertypes = manager.findAll();
	 	 return (List) allUsertypes;
	}

CreateUtilisateur.zul

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<zk>
<window title="User" border="normal" id="win" apply="com.caciopee.presentation.UtilisateurPresentation">
		<groupbox mold="3d">
			<grid>
				<columns height="22px">
					<column label="Type" width="150px" />
					<column label="Content" />
				</columns>
				<rows>

					<row>
						Name :
						<textbox id="nomuser" width="230px" constraint="no empty:Please enter Name" value="@{win$UtilisateurPresentation.utilisateur.nomuser}"/>
					</row>
					<row>
						Surname :
						<textbox id="prenomuser" width="250px" constraint="no empty:Please enter Surname" value="@{win$UtilisateurPresentation.utilisateur.prenomuser}"/>
					</row>
					<row>
						Password:
						<textbox id="passwrduser" width="250px" constraint="no empty:Please enter Password" value="@{win$UtilisateurPresentation.utilisateur.passwrduser}"/>
					</row>
					<row>
						EMAIL :
						<textbox id="emailuser" width="250px" constraint="no empty:Please enter Email" value="@{win$UtilisateurPresentation.utilisateur.emailuser}"/>
					</row>
					<row>
						Tel :
						<textbox id="teluser" width="250px" constraint="no empty:Please enter Tel" value="@{win$UtilisateurPresentation.utilisateur.teluser}"/>
					</row>
										
					<row>
					Type :
								
					<listbox id="" model="@{win$UtilisateurPresentation.AllUsertypes}" multiple="true" 
 						         selectedItem="@{win$UtilisateurPresentation.usertype}" >
						<listhead  >
							<listheader label="Type" sort="auto"></listheader>
							<listheader label="Role" sort="auto"></listheader>
						
						</listhead>
						<listitem self="@{each='event'}" value="@{event}">
							<listcell  label="@{event.nomusertype}"/>
							<listcell  label="@{event.roleusertype}"/>
							
						</listitem>
						
					</listbox>
			
					</row>
					<row spans="2">
						<toolbar>
							<button id="add" label="Save"
								orient="vertical" width="150px" onClick=""  />
							<button id="btnCancel" label="Cancel"
								orient="vertical" width="150px" />
						</toolbar>
					</row>
				</rows>
			</grid>
		</groupbox>


	</window>
</zk>

the problem is when i create a Utilisateur the creation done without a usertype and i have no exception

Thanks for your Help,
Hamada

delete flag offensive retag edit

14 Replies

Sort by » oldest newest

answered 2011-06-14 09:58:38 +0800

hamada14br gravatar image hamada14br
36

I think the problem in the methode onClick$âdd() of the controller( utilisateurPresentation) between createUtilisateur.zul and utilisateurServiceImpl.java

link publish delete flag offensive edit

answered 2011-06-14 13:27:41 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2011-06-14 13:28:13 +0800

public void onClick$add() {
Set<Usertype> usertypes = new HashSet<Usertype>();
Usertupe usert=new Usertype();

link publish delete flag offensive edit

answered 2011-06-14 13:49:44 +0800

hamada14br gravatar image hamada14br
36

Hi Stephan,
i make a error just when i copied the code in this forum but the code is well

when i click on the button.. here is the show sql

Hibernate: select max(iduser) from utilisateur
Hibernate: insert into public.utilisateur (nomuser, prenomuser, passwrduser, emailuser, teluser, iduser) values (?, ?, ?, ?, ?, ?)

the insert without the set usertypes of the foreign key of idusertype

how to insert where a Utilisateur where a Usertype with join (many to many) ?

thanks for your help

link publish delete flag offensive edit

answered 2011-06-15 04:09:05 +0800

hamada14br gravatar image hamada14br
36

No one can Help me ??

link publish delete flag offensive edit

answered 2011-06-15 04:50:21 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Are the mappings handmade or generated ?


By the way. What's this ?

 Usertupe usert=new Usertype();
 usert.setNomusertype(usert.getNomusertype);
 usert.setRoleusertype(usert.getRoleusertype);

link publish delete flag offensive edit

answered 2011-06-15 05:32:51 +0800

hamada14br gravatar image hamada14br
36

The Mapping is genereted by Hibernate,

i dont now if the code of the methode onClick$add() is correct for insert Utilisateur with Join Usertype

link publish delete flag offensive edit

answered 2011-06-15 06:13:03 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

can you post the create table scripts for these two tables.

link publish delete flag offensive edit

answered 2011-06-16 03:49:48 +0800

hamada14br gravatar image hamada14br
36

Here is the 3 table,

Table: utilisateur

CREATE TABLE utilisateur
(
  iduser serial NOT NULL,
  nomuser character varying(254),
  prenomuser character varying(254),
  passwrduser character varying(254),
  emailuser character varying(254),
  teluser character varying(254),
  CONSTRAINT pk_utilisateur PRIMARY KEY (iduser)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE utilisateur OWNER TO postgres;

CREATE UNIQUE INDEX user_pk
  ON utilisateur
  USING btree
  (iduser);

table: usertype

CREATE TABLE usertype
(
  idusertype serial NOT NULL,
  nomusertype character varying(254),
  roleusertype character varying(254),
  CONSTRAINT pk_usertype PRIMARY KEY (idusertype)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE usertype OWNER TO postgres;


CREATE UNIQUE INDEX usertype_pk
  ON usertype
  USING btree
  (idusertype);

table user_usertype (this is the table association for the relation many-to-many between utilisateur and usertype)

CREATE TABLE user_usertype
(
  idusertype serial NOT NULL,
  iduser serial NOT NULL,
  CONSTRAINT pk_user_usertype PRIMARY KEY (idusertype, iduser),
  CONSTRAINT fk_user_use_associati_usertype FOREIGN KEY (idusertype)
      REFERENCES usertype (idusertype) MATCH SIMPLE
      ON UPDATE RESTRICT ON DELETE RESTRICT,
  CONSTRAINT fk_user_use_associati_utilisat FOREIGN KEY (iduser)
      REFERENCES utilisateur (iduser) MATCH SIMPLE
      ON UPDATE RESTRICT ON DELETE RESTRICT,
  CONSTRAINT fkf32ff779dba5fc82 FOREIGN KEY (iduser)
      REFERENCES utilisateur (iduser) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fkf32ff779e05b62c FOREIGN KEY (idusertype)
      REFERENCES usertype (idusertype) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE user_usertype OWNER TO postgres;


CREATE INDEX association15_fk
  ON user_usertype
  USING btree
  (idusertype);


CREATE INDEX association15_fk2
  ON user_usertype
  USING btree
  (iduser);

CREATE UNIQUE INDEX association15_pk
  ON user_usertype
  USING btree
  (idusertype, iduser);

Thanks for your help stephan

link publish delete flag offensive edit

answered 2011-06-16 04:20:27 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2011-06-16 04:37:26 +0800

Hi hamada,

1. There is no reason that the user_usertype table have NO single PrimaryKey field. (Only it's an old legacy system) .
Composite Keys are bad to handle for hibernate. It's better to make an unique index over the two foreign key fields
to prevent double entries.

2. user_usertype table is a cross cutting table that holds only two foreign keys. I simple attach the bean codes and the
mapping files how i map such a group of tables.

Here is a pic how the table looks like.


Right-table bean:

/**
 * EN: Model class for the <b>Security Right</b>.<br>
 * DE: Model Klasse fuer die <b>Zugriffsschutz Einzelrechte</b>.<br>
 * 
 * @author bbruhns
 * @author Stephan Gerth
 */
public class SecurityRight implements java.io.Serializable, Entity {

	private static final long serialVersionUID = 1L;

	private long id = Long.MIN_VALUE;
	private int version;
	private Integer rightType;
	private String shorttext;
	private String longtext;
	private String vendor;
	private boolean systemRight = false;
	private Set<SecurityGroupright> securityGrouprights = new HashSet<SecurityGroupright>(0);

	@Override
	public boolean isNew() {
		return (getId() == Long.MIN_VALUE);
	}

	public SecurityRight() {
	}

	public SecurityRight(long id, String shorttext, String vendor, boolean systemRight) {
		this.id = id;
		this.shorttext = shorttext;
		this.vendor = vendor;
		this.systemRight = systemRight;
	}

	public SecurityRight(long id, Integer rightType, String shorttext, String longtext, String vendor, boolean systemRight, Set<SecurityGroupright> securityGrouprights) {
		this.id = id;
		this.rightType = rightType;
		this.shorttext = shorttext;
		this.longtext = longtext;
		this.vendor = vendor;
		this.systemRight = systemRight;
		this.securityGrouprights = securityGrouprights;
	}

	@Override
	public void setId(long id) {
		this.id = id;
	}

	@Override
	public long getId() {
		return id;
	}

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

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

	public void setRightType(Integer rightType) {
		this.rightType = rightType;
	}

	public Integer getRightType() {
		return rightType;
	}

	public void setShorttext(String shorttext) {
		this.shorttext = shorttext;
	}

	public String getShorttext() {
		return shorttext;
	}

	public void setLongtext(String longtext) {
		this.longtext = longtext;
	}

	public String getLongtext() {
		return longtext;
	}

	public void setVendor(String vendor) {
		this.vendor = vendor;
	}

	public String getVendor() {
		return vendor;
	}

	public void setSystemRight(boolean systemRight) {
		this.systemRight = systemRight;
	}

	public boolean isSystemRight() {
		return systemRight;
	}

	public Set<SecurityGroupright> getSecurityGrouprights() {
		return this.securityGrouprights;
	}

	public void setSecurityGrouprights(Set<SecurityGroupright> securityGrouprights) {
		this.securityGrouprights = securityGrouprights;
	}

	@Override
	public int hashCode() {
		return Long.valueOf(getId()).hashCode();
	}

	public boolean equals(SecurityRight secRight) {
		return getId() == secRight.getId();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj instanceof SecurityRight) {
			SecurityRight secRight = (SecurityRight) obj;
			return equals(secRight);
		}

		return false;
	}

	public String toString() {

		StringBuilder builder = new StringBuilder();
		builder.append("SecurityRight [id=");
		builder.append(getId());
		builder.append(", shorttext=");
		builder.append(getShorttext());
		builder.append(", vendor=");
		builder.append(getVendor());
		builder.append("]");

		return builder.toString();
	}
}

Group table bean:

/**
 * EN: Model class for the <b>Security Group</b>.<br>
 * DE: Model Klasse fuer die <b>Zugriffsschutz Gruppen</b>.<br>
 * 
 * @author bbruhns
 * @author Stephan Gerth
 */
public class SecurityGroup implements java.io.Serializable, Entity {

	private static final long serialVersionUID = 1L;

	private long id = Long.MIN_VALUE;
	private int version;
	private String shorttext;
	private String longtext;
	private String vendor;
	private boolean systemGroup = false;
	private Set<SecurityGroupright> securityGrouprights = new HashSet<SecurityGroupright>(0);
	private Set<SecurityRolegroup> securityRolegroups = new HashSet<SecurityRolegroup>(0);

	@Override
	public boolean isNew() {
		return (getId() == Long.MIN_VALUE);
	}

	public SecurityGroup() {
	}

	public SecurityGroup(long id, String shorttext, String vendor, boolean systemGroup) {
		this.id = id;
		this.shorttext = shorttext;
		this.vendor = vendor;
		this.systemGroup = systemGroup;
	}

	public SecurityGroup(long id, String shorttext, String longtext, String vendor, boolean systemGroup, Set<SecurityGroupright> securityGrouprights, Set<SecurityRolegroup> securityRolegroups) {
		this.id = id;
		this.shorttext = shorttext;
		this.longtext = longtext;
		this.vendor = vendor;
		this.systemGroup = systemGroup;
		this.securityGrouprights = securityGrouprights;
		this.securityRolegroups = securityRolegroups;
	}

	@Override
	public void setId(long id) {
		this.id = id;
	}

	@Override
	public long getId() {
		return id;
	}

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

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

	public void setShorttext(String shorttext) {
		this.shorttext = shorttext;
	}

	public String getShorttext() {
		return shorttext;
	}

	public void setLongtext(String longtext) {
		this.longtext = longtext;
	}

	public String getLongtext() {
		return longtext;
	}

	public void setVendor(String vendor) {
		this.vendor = vendor;
	}

	public String getVendor() {
		return vendor;
	}

	public void setSystemGroup(boolean systemGroup) {
		this.systemGroup = systemGroup;
	}

	public boolean isSystemGroup() {
		return systemGroup;
	}

	public Set<SecurityGroupright> getSecurityGrouprights() {
		return this.securityGrouprights;
	}

	public void setSecurityGrouprights(Set<SecurityGroupright> securityGrouprights) {
		this.securityGrouprights = securityGrouprights;
	}

	public Set<SecurityRolegroup> getSecurityRolegroups() {
		return this.securityRolegroups;
	}

	public void setSecurityRolegroups(Set<SecurityRolegroup> securityRolegroups) {
		this.securityRolegroups = securityRolegroups;
	}

	@Override
	public int hashCode() {
		return Long.valueOf(getId()).hashCode();
	}

	public boolean equals(SecurityGroup securityGroup) {
		return getId() == securityGroup.getId();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj instanceof SecurityGroup) {
			SecurityGroup securityGroup = (SecurityGroup) obj;
			return equals(securityGroup);
		}

		return false;
	}

	public String toString() {

		StringBuilder builder = new StringBuilder();
		builder.append("SecurityGroup [id=");
		builder.append(getId());
		builder.append(", shorttext=");
		builder.append(getShorttext());
		builder.append(", vendor=");
		builder.append(getVendor());
		builder.append("]");

		return builder.toString();
	}

}

Group-Right table (cross cutting) bean:
Holds only the fields for the foreign keys of group-table and right-table

/**
 * EN: Model class for <b>Security Groupright</b>.<br>
 * DE: Model Klasse fuer die <b>Zugriffsschutz GruppenRechte</b>.<br>
 * 
 * @author bbruhns
 * @author Stephan Gerth
 */
public class SecurityGroupright implements java.io.Serializable, Entity {

	private static final long serialVersionUID = 1L;

	private long id = Long.MIN_VALUE;
	private int version;
	private SecurityGroup securityGroup;
	private SecurityRight securityRight;

	@Override
	public boolean isNew() {
		return (getId() == Long.MIN_VALUE);
	}

	public SecurityGroupright() {
	}

	public SecurityGroupright(long id, SecurityGroup securityGroup, SecurityRight securityRight) {
		this.setId(id);
		this.securityGroup = securityGroup;
		this.securityRight = securityRight;
	}

	@Override
	public void setId(long id) {
		this.id = id;
	}

	@Override
	public long getId() {
		return id;
	}

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

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

	public SecurityGroup getSecurityGroup() {
		return this.securityGroup;
	}

	public void setSecurityGroup(SecurityGroup securityGroup) {
		this.securityGroup = securityGroup;
	}

	public SecurityRight getSecurityRight() {
		return this.securityRight;
	}

	public void setSecurityRight(SecurityRight securityRight) {
		this.securityRight = securityRight;
	}

	@Override
	public int hashCode() {
		return Long.valueOf(getId()).hashCode();
	}

	public boolean equals(SecurityGroupright secGroupright) {
		return getId() == secGroupright.getId();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj instanceof SecurityGroupright) {
			SecurityGroupright secGroupright = (SecurityGroupright) obj;
			return equals(secGroupright);
		}

		return false;
	}

	public String toString() {

		StringBuilder builder = new StringBuilder();
		builder.append("SecurityGroupright [id=");
		builder.append(getId());
		builder.append("]");

		return builder.toString();
	}

}

Group-table mapping:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

	<class name="org.opentruuls.backend.user.model.SecurityGroup"
		schema="oikarus_user" table="sec_group">

		<id name="id" type="long">
			<column name="grp_id" />
			<generator class="sequence">
				<param name="sequence">sec_group_seq</param>
			</generator>
		</id>

		<version name="version" column="VERSION" type="integer" />

		<property name="shorttext" type="string">
			<column name="grp_shorttext" length="40" not-null="true"
				unique="true" />
		</property>
		<property name="longtext" type="string">
			<column name="grp_longtext" length="300" />
		</property>
		<property name="vendor" type="string">
			<column name="grp_vendor" length="20" not-null="true" />
		</property>
		<property name="systemGroup" type="boolean">
			<column name="grp_system" not-null="true" />
		</property>

		<set name="securityGrouprights" table="sec_groupright" inverse="true"
			lazy="true" fetch="select">
			<key>
				<column name="grp_id" not-null="true" />
			</key>
			<one-to-many class="org.opentruuls.backend.user.model.SecurityGroupright" />
		</set>
		<set name="securityRolegroups" table="sec_rolegroup" inverse="true"
			lazy="true" fetch="select">
			<key>
				<column name="grp_id" not-null="true" />
			</key>
			<one-to-many class="org.opentruuls.backend.user.model.SecurityRolegroup" />
		</set>
	</class>

	<query name="allGroupsForUserSqlQuery">
		SELECT distinct g FROM SecurityGroup g
		JOIN
		g.securityRolegroups
		AS rg
		JOIN rg.securityRole.securityUserroles AS ur
		JOIN
		ur.user AS u
		WHERE u.id = ?
	</query>
</hibernate-mapping>

Rights-table mapping:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

	<class name="org.opentruuls.backend.user.model.SecurityRight"
		table="sec_right">

		<!--
			<id name="id" type="long"> <column name="rig_id" /> <generator
			class="assigned" /> </id>
		-->

		<id name="id" type="long">
			<column name="rig_id" />
			<generator class="sequence">
				<param name="sequence">sec_right_seq</param>
			</generator>
		</id>

		<version name="version" column="VERSION" type="integer" />

		<property name="rightType" type="java.lang.Integer">
			<column name="rig_type" />
		</property>
		<property name="shorttext" type="string">
			<column name="rig_shorttext" length="50" not-null="true"
				unique="true" />
		</property>
		<property name="longtext" type="string">
			<column name="rig_longtext" length="300" />
		</property>
		<property name="vendor" type="string">
			<column name="rig_vendor" length="20" not-null="true" />
		</property>
		<property name="systemRight" type="boolean">
			<column name="rig_system" not-null="true" />
		</property>

		<set name="securityGrouprights" table="sec_groupright" inverse="true"
			lazy="true" fetch="select">
			<key>
				<column name="rig_id" not-null="true" />
			</key>
			<one-to-many class="org.opentruuls.backend.user.model.SecurityGroupright" />
		</set>
	</class>

	<query name="allRightsForUserSqlQuery">
		SELECT distinct r FROM SecurityRight r
		JOIN
		r.securityGrouprights AS gr
		JOIN gr.securityGroup.securityRolegroups AS
		rg
		JOIN
		rg.securityRole.securityUserroles AS ur
		JOIN ur.user AS u
		WHERE
		u.id = ?
	</query>

</hibernate-mapping>


Group-Right table mapping:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

	<class name="org.opentruuls.backend.user.model.SecurityGroupright"
		table="sec_groupright">

		<id name="id" type="long">
			<column name="gri_id" />
			<generator class="sequence">
				<param name="sequence">sec_groupright_seq</param>
			</generator>
		</id>

		<version name="version" column="VERSION" type="integer" />

		<many-to-one name="securityGroup"
			class="org.opentruuls.backend.user.model.SecurityGroup" fetch="select">
			<column name="grp_id" not-null="true" />
		</many-to-one>
		<many-to-one name="securityRight"
			class="org.opentruuls.backend.user.model.SecurityRight" fetch="select">
			<column name="rig_id" not-null="true" />
		</many-to-one>
	</class>
</hibernate-mapping>

sample-methods in Group-Right

	@SuppressWarnings("unchecked")
	@Override
	public SecurityGroupright getGroupRightByGroupAndRight(SecurityGroup aGroup, SecurityRight aRight) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SecurityGroupright.class);
		criteria.add(Restrictions.eq("securityGroup", aGroup));
		criteria.add(Restrictions.eq("securityRight", aRight));

		return (SecurityGroupright) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityGroupright> getGroupRightsByGroup(SecurityGroup aGroup) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SecurityGroupright.class);
		criteria.add(Restrictions.eq("securityGroup", aGroup));

		return getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public boolean isRightInGroup(SecurityRight aRight, SecurityGroup aGroup) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SecurityGroupright.class);
		criteria.add(Restrictions.eq("securityGroup", aGroup));
		criteria.add(Restrictions.eq("securityRight", aRight));
		criteria.setProjection(Projections.rowCount());

		int count = DataAccessUtils.intResult(getHibernateTemplate().findByCriteria(criteria));
		return count > 0;
	}

best
Stephan

link publish delete flag offensive edit

answered 2011-06-16 04:46:20 +0800

hamada14br gravatar image hamada14br
36

I did not create the Data Base Manually, i gereted the Script by the The Power AMC software

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-06-14 09:55:36 +0800

Seen: 800 times

Last updated: Sep 26 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More