-->
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.  [ 8 posts ] 
Author Message
 Post subject: Error while mapping a single POJO class to two tables
PostPosted: Wed Jun 09, 2004 2:52 pm 
Newbie

Joined: Wed Jun 09, 2004 2:41 pm
Posts: 14
Hi,

I have a single POJO class called 'User' that is mapped to two tables (USERS and USER_ROLES)

When I invoke session.save(user) it gives the following error:

net.sf.hibernate.MappingException: duplicate import: User
at net.sf.hibernate.cfg.Mappings.addImport(Mappings.java:85)
at net.sf.hibernate.cfg.Binder.bindClass(Binder.java:126)
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:221)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1243)
at net.sf.hibernate.cfg.Configuration.add(Configuration.java:249)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:285)

Is there a specific way to define the *.hbm.xml files when the same bean is mapped to more than one table.

I have defined two hbm.xml files one for user table and one for role table and both refer to, obviously, the same POJO class.

//user.hbm.xml
<hibernate-mapping package="com.proj.beans">
<class name="User" table="USERS">
<id name="userId" column="USERNAME">
<generator class="assigned" />
</id>
<property name="password" column="PASSWORD"/>
<property name="repId" column="SALESREPID"/>
</class>
</hibernate-mapping>

//userrole.hbm.xml
<hibernate-mapping package="com.proj.beans">
<class name="User" table="USER_ROLES">
<id name="userId" column="USERNAME">
<generator class="assigned"/>
</id>
<property name="roleName" column="ROLENAME"/>
</class>
</hibernate-mapping>

and I do the following in my configuaration:

cfg.addInputStream(this.getClass().
getClassLoader().getResourceAsStream("User.hbm.xml"));

cfg.addInputStream(this.getClass().
getClassLoader().getResourceAsStream("UserRole.hbm.xml"));

Any help will be much appreciated.

Thanks,
Jeelani


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 3:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
In Hibernate 2.1 you will need two session factories to do this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 3:08 pm 
Newbie

Joined: Wed Jun 09, 2004 2:41 pm
Posts: 14
Thanks Gavin. But I want both to be part of same transaction. If I use two Session Factories, how to make them part of same transaction? Do I need to go for XA* ?

Jeelani


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 3:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You could use openSession(Connection)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 3:45 pm 
Newbie

Joined: Wed Jun 09, 2004 2:41 pm
Posts: 14
Creating two connections just for taking to two tables of same Database is not convincing!

Anyways, Thanks a lot Gavin for the quick response!

Thanks,
Jeelani


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 3:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
???!


I just told you a way to do it without two connections!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 4:33 pm 
Newbie

Joined: Wed Jun 09, 2004 2:41 pm
Posts: 14
My bad! Sorry.

Jeelani


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 4:54 pm 
Newbie

Joined: Wed Jun 09, 2004 2:41 pm
Posts: 14
I am able to fix it using openSession(Connection). This requires creating two SessionFactories and a session object from each factory using the same Connection. And set AutoCommit on Connection to false and finally once both transactions are successful, do Connection.commit();

Thanks Gavin for the help!
Jeelani


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

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.