Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Complex Hibernate mapping (or not?)

Reply
Thread Tools

Complex Hibernate mapping (or not?)

 
 
Snyke
Guest
Posts: n/a
 
      05-17-2006
Once again I'm running against a solid wall of errors, and my head
starts to hurt really badly with this one
I have a bidirectional link between two classes

Account <=> User

this being a many-to-many relationship. Until now nothing fancy, but I
have a Class inbetween that manages accessrights called Privilge, this
just has two boolean values (read and write) that tell wether a user
can read or write to the account.

Account <-many-to-one-> Privilege <-one-to-many-> User

basically every Account-User combination has a Privilege. Now the idea
was to have first the mapping of the Privilege Class like this:

<class name="net.snyke.tmm.Privilege" table="privilege">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="read" column="isRead"/>
<property name="write" column="isWrite"/>
</class>

and then let the other classes reference each other over this table:

<set name="accountPrivileges" table="privilege">
<key column="user"/>
<many-to-many column="account" class="net.snyke.tmm.Account"/>
</set>

What I'm trying to say is, why add another two tables to manage the
relationships between Privilege-User and Privilege-Account, when I can
just use the existing table Privilege? Or did I get something wrong?

Regards,
Christian 'Snyke' Decker
http://www.Snyke.net

 
Reply With Quote
 
 
 
 
Snyke
Guest
Posts: n/a
 
      05-17-2006
By now I figured out that what I really want to do is having
one-to-many for user -> privilege, and a many-to-one for
privilege->user, pretty standard case I would assume, however I get
some really weird errors with this mapping file:

<hibernate-mapping>
<class name="net.snyke.tmm.Privilege" table="privilege">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="read" column="isRead"/>
<property name="write" column="isWrite"/>
<many-to-one name="user" class="net.snyke.tmm.UserAccount"
column="user" not-null="true" />
<many-to-one name="account"
class="net.snyke.tmm.FinancialAccount" column="account" not-null="true"
/>
</class>
<class name="net.snyke.tmm.UserAccount" table="users">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="password"/>
<bag name="userPrivileges"
inverse="true"
lazy="false"
cascade="all">
<key column="user_privileges"/>
<one-to-many class="net.snyke.tmm.Privilege"/>
</bag>
</class>
<class name="net.snyke.tmm.FinancialAccount" table="accounts">
<id name="id" column="id">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>

namely this:

17:16:10,052 INFO Configuration:1286 - configuring from resource:
/hibernate.cfg.xml
17:16:10,052 INFO Configuration:1263 - Configuration resource:
/hibernate.cfg.xml
17:16:10,056 INFO Configuration:468 - Reading mappings from resource:
mapping.hbm.xml
17:16:10,144 INFO DriverManagerConnectionProvider:147 - cleaning up
connection pool: jdbc:mysql://localhost/test
17:16:10,153 INFO Mappings:137 - duplicate import:
net.snyke.tmm.Privilege->net.snyke.tmm.Privilege
17:16:10,153 INFO Mappings:137 - duplicate import:
net.snyke.tmm.Privilege->Privilege
17:16:10,154 INFO HbmBinder:265 - Mapping class:
net.snyke.tmm.Privilege -> privilege
%%%% Error Creating HibernateSessionFactory %%%%
org.hibernate.MappingException: Could not read mappings from resource:
mapping.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Config uration.java:484)
at
org.hibernate.cfg.Configuration.parseMappingElemen t(Configuration.java:1443)
at
org.hibernate.cfg.Configuration.parseSessionFactor y(Configuration.java:1411)
at
org.hibernate.cfg.Configuration.doConfigure(Config uration.java:1392)
at
org.hibernate.cfg.Configuration.doConfigure(Config uration.java:136
at org.hibernate.cfg.Configuration.configure(Configur ation.java:128
at
net.snyke.tmm.persister.HibernateSessionFactory.in itSessionFactory(HibernateSessionFactory.java:9
at
net.snyke.tmm.persister.HibernateSessionFactory.ge tInstance(HibernateSessionFactory.java:54)
at
net.snyke.tmm.TestUserAccount.testPersistence(Test UserAccount.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154 )
at junit.framework.TestCase.runBare(TestCase.java:127 )
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:11
at junit.framework.TestSuite.runTest(TestSuite.java:2 0
at junit.framework.TestSuite.run(TestSuite.java:203)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:47
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:344)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.DuplicateMappingException: Duplicate
class/entity mapping net.snyke.tmm.Privilege
at org.hibernate.cfg.Mappings.addClass(Mappings.java: 117)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.jav a:110)
at org.hibernate.cfg.Configuration.add(Configuration. java:385)
at
org.hibernate.cfg.Configuration.addInputStream(Con figuration.java:426)
at org.hibernate.cfg.Configuration.addResource(Config uration.java:481)
... 23 more

Any idea?

 
Reply With Quote
 
 
 
 
Danno
Guest
Posts: n/a
 
      05-17-2006
show us mapping.hbm.xml

 
Reply With Quote
 
Snyke
Guest
Posts: n/a
 
      05-18-2006
Well ok the above should be enough of mapping.hbm.xml since this causes
me the errors.
Anyway here goes the full file:

--------------------------------------
<?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="net.snyke.tmm.Privilege" table="privilege">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="read" column="isRead"/>
<property name="write" column="isWrite"/>
<many-to-one name="user" class="net.snyke.tmm.UserAccount"
column="user" not-null="true" />
<many-to-one name="account"
class="net.snyke.tmm.FinancialAccount" column="account" not-null="true"
/>
</class>

<class name="net.snyke.tmm.UserAccount" table="users">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="password"/>
<bag name="userPrivileges"
inverse="true"
lazy="false"
cascade="all">
<key column="user_privileges"/>
<one-to-many class="net.snyke.tmm.Privilege"/>
</bag>
</class>

<class name="net.snyke.tmm.FinancialAccount" table="accounts">
<id name="id" column="id">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
---------------------

With this mapping I get the aforementioned error:


%%%% Error Creating HibernateSessionFactory %%%%
org.hibernate.MappingException: Could not read mappings from resource:
mapping.hbm.xml
[...]
Caused by: org.hibernate.DuplicateMappingException: Duplicate
class/entity mapping net.snyke.tmm.Privilege
[...]

 
Reply With Quote
 
Danno
Guest
Posts: n/a
 
      05-19-2006
Say, are you using hibernate annotations too?

 
Reply With Quote
 
Snyke
Guest
Posts: n/a
 
      05-19-2006
No idea what you are talking about ^^"
The mapping file is the only actual mapping in all my application, if
that's what you're asking for, I'm not using any XDoclet style mapping
at all.

Regards,
Snyke

 
Reply With Quote
 
Snyke
Guest
Posts: n/a
 
      05-20-2006
Nobody any idea?

 
Reply With Quote
 
Danno
Guest
Posts: n/a
 
      05-20-2006
What is this referring to?

17:16:10,153 INFO Mappings:137 - duplicate import:
net.snyke.tmm.Privilege->net.snyke.tmm.Privilege
17:16:10,153 INFO Mappings:137 - duplicate import:
net.snyke.tmm.Privilege->Privilege
17:16:10,154 INFO HbmBinder:265 - Mapping class:
net.snyke.tmm.Privilege -> privilege


Do you have more than one configuration listing the same thing?

 
Reply With Quote
 
Snyke
Guest
Posts: n/a
 
      05-20-2006
No all I have is a Class Privilege that is mapped to a table directly.
Every Privilege has a reference to a UserAccount and a
FinancialAccount.
Every UserAccount has a List to Privileges (the same goes for
FinancialAccounts but they aren't yet implemented...) and I try to tell
hibernate to take the reference from UserAccount to Privilege as the
mapping in the Privilege class.
Hibernate probably does not understand why I'm using an existing
(mapped class) Privilege to map it to the List in UserAccount.

Any idea?

P.S.:Sorry if the message is not quite understandable, already had some
beers ^^

 
Reply With Quote
 
Danno
Guest
Posts: n/a
 
      05-21-2006
oooh, last but not least, show us your hibernate.cfg.xml

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hibernate LazyInitializationException in web application Struts asMVC, Spring as IOC and Hibernate as ORM layer Amit Jain Java 7 04-27-2009 10:27 AM
HIBERNATE - java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration Eleanna Skouta Java 2 12-09-2008 02:09 AM
Hibernate cannot find hibernate.cfg.xml jstorta Java 1 01-19-2008 01:45 PM
Hibernate Syncronizer now generates code for Hibernate 3.0 msenin@covad.net Java 0 07-14-2005 05:47 AM
[HIBERNATE] [EVALUATION] - Gavin King Censors Hibernate Forum Ilias Lazaridis Java 0 12-27-2004 04:26 AM



Advertisments