-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: many-to-many mapping using composite unique constraint
PostPosted: Fri Apr 30, 2010 2:53 am 
Newbie

Joined: Fri Apr 30, 2010 1:09 am
Posts: 1
Hi all,

I have 5 tables in postgres database.

CREATE TABLE tasks
(
task_id smallint NOT NULL,
description character varying(50),
CONSTRAINT pk_task PRIMARY KEY (task_id)
)


CREATE TABLE roles
(
role_id smallint NOT NULL,
description character varying(25) NOT NULL,
CONSTRAINT pk_roles PRIMARY KEY (role_id)
)

CREATE TABLE role_tasks
(
role_id smallint NOT NULL,
task_id smallint NOT NULL,
CONSTRAINT pk_role_tasks PRIMARY KEY (role_id, task_id),
CONSTRAINT fk_role_tasks_ref_roles FOREIGN KEY (role_id)
REFERENCES roles (role_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_role_tasks_ref_tasks FOREIGN KEY (task_id)
REFERENCES tasks (task_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)


CREATE TABLE users
(
user_id character varying(25) NOT NULL,
"name" character varying(100),
"password" character varying(25),
role_id smallint NOT NULL,
CONSTRAINT pk_users PRIMARY KEY (user_id),
CONSTRAINT fk_users_ref_roles FOREIGN KEY (role_id)
REFERENCES roles (role_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT uq_user UNIQUE (user_id, role_id)
)


CREATE TABLE user_tasks
(
user_id character varying(25) NOT NULL,
role_id smallint NOT NULL,
task_id smallint NOT NULL,
CONSTRAINT pk_user_tasks PRIMARY KEY (user_id, role_id, task_id),
CONSTRAINT fk_user_task_ref_user FOREIGN KEY (user_id, role_id)
REFERENCES users (user_id, role_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_user_tasks_ref_role_tasks FOREIGN KEY (role_id, task_id)
REFERENCES role_tasks (role_id, task_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)


Hibernate mapping --

<class name="com.stp.dto.ac.Task" table="TASKS">
<meta attribute="class-description">This class contains task details.</meta>
<id name="taskId" type="int" column="TASK_ID">
<generator class="assigned" />
</id>
<property name="description" type="string" not-null="true" column="DESCRIPTION" />
</class>




<class name="com.stp.dto.ac.Role" table="ROLES">
<meta attribute="class-description">This class contains role details.</meta>
<id name="roleId" type="int" column="ROLE_ID">
<generator class="assigned" />
</id>
<property name="description" type="string" not-null="true" column="DESCRIPTION" />
<set name="roleTasks" table="ROLE_TASKS" cascade="all" >
<key column="ROLE_ID" not-null="false"/>
<many-to-many column="TASK_ID" unique="true" class="com.stp.dto.ac.Task" />
</set>
</class>



<class name="com.stp.dto.ac.User" table="USERS">
<meta attribute="class-description">This class contains user details.</meta>
<id name="userId" type="string" column="USER_ID">
<generator class="assigned" />
</id>
<property name="roleId" type="int" not-null="true" insert="false" update="false" column="ROLE_ID" />
<many-to-one name="role" fetch="join" class="com.stp.dto.ac.Role" column="ROLE_ID" insert="true"
update="true" cascade="none" not-null="true"/>
<property name="name" type="string" not-null="true" column="NAME" />
<property name="password" type="string" not-null="true" column="PASSWORD" />


<properties name="aa" insert="false" unique="true" update="false">
<property name="userId" type="string" column="USER_ID" insert="false" update="false"></property>
<property name="roleId" type="int" column="ROLE_ID" insert="false" update="false"></property>
</properties>
<set name="userTasks" table="USER_TASKS" cascade="none" >
<key property-ref="aa">
<column name="USER_ID" ></column>
<column name="ROLE_ID"></column>
</key>
<many-to-many column="TASK_ID" unique="true" class="com.stp.dto.ac.Task">

</many-to-many>
</set>
</class>


Here I am mapping USER_TASKS to user using composite unique constraint on columns role_id and user id using property-ref.

But when I query or insert data using hibernate it throws this exception -
java.lang.ClassCastException: com.stp.dto.ac.User cannot be cast to java.lang.String


query code snippet -
queryString = new StringBuilder();
queryString.append("from User u left join fetch u.userTasks ut ");
Query query = session.createQuery(queryString.toString());
userSet = new LinkedHashSet(query.list());

please give me a suggestion to come out of this error

Thanks,
Sujnan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.