-->
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.  [ 6 posts ] 
Author Message
 Post subject: why an entity cannot have 2 references of another entity?
PostPosted: Wed Oct 31, 2007 1:33 pm 
Newbie

Joined: Wed Oct 31, 2007 1:16 pm
Posts: 4
Hi, Is there any particular reason that 1 entity can not have 2 references of another table, ie, the entity has FK aID1 and aID2 that refers to different records of another table? I checked the hibernate 3.2 implementation in
org.hibernate.mapping.PersistentClass.checkPropertyDuplication

Seems to me that it is only using local data structure to make sure no 2 referenced tables are of the same name.

thank you for any hints



[b]Hibernate version: 3.2 [/b]

[b]Mapping documents:
<?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="popclonerecords.Mytest" table="mytest">
<id name="id" column="id" type="integer" unsaved-value="0">
<generator class="increment"/>
</id>



<many-to-one
name="testBranch"
column="t1ID"
class="popclonerecords.TestBranch"
not-null="true" />


<many-to-one
name="testBranch"
column="t2ID"
class="popclonerecords.TestBranch2"
not-null="true" />


</class>

</hibernate-mapping>
[/b]

[b]Code between sessionFactory.openSession() and session.close():
TestBranch tb = new TestBranch();
TestBranch2 tb2 = new TestBranch2();

Mytest cmap = new Mytest();
cmap.setTestBranch(tb);
cmap.setTestBranch2(tb2);
[/b]

[b]Full stack trace of any exception that occurs:
The exception is org.hibernate.MappingException:
org.hibernate.MappingException: Duplicate property mapping of testBranch found in mypack.Mytest


org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:459)
org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:449)
org.hibernate.mapping.RootClass.validate(RootClass.java:192)
org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
popclonerecords.TestParentChild.main(TestParentChild.java:156)

[/b]

[b]Name and version of the database you are using:[/b]
MySQL 4.0.27

[b]The generated SQL (show_sql=true):[/b]

[b]Debug level Hibernate log excerpt:[/b]


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp


Top
 Profile  
 
 Post subject: PS: TestBranch2 and TestBranch map to the same table
PostPosted: Wed Oct 31, 2007 1:46 pm 
Newbie

Joined: Wed Oct 31, 2007 1:16 pm
Posts: 4
PS: TestBranch2 and TestBranch map to the same table


Top
 Profile  
 
 Post subject: typo?
PostPosted: Wed Oct 31, 2007 6:07 pm 
Newbie

Joined: Wed Oct 31, 2007 5:36 pm
Posts: 13
Is there a typo in your mapping? From your mapping, you are mapping two different entities (that map to the same table), to the same property name of your class.

Maybe this is what you want:

<class name="popclonerecords.Mytest" table="mytest">
<id name="id" column="id" type="integer" unsaved-value="0">
<generator class="increment"/>
</id>

<many-to-one
name="testBranch"
column="t1ID"
class="popclonerecords.TestBranch"
not-null="true" />

<many-to-one
name="testBranch2"
column="t2ID"
class="popclonerecords.TestBranch2"
not-null="true" />

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 6:36 pm 
Newbie

Joined: Wed Oct 31, 2007 1:16 pm
Posts: 4
thank you for your reply. Unfortunately, 2 entities (TestBranch & TestBranch2) do map to the same table. I created 2 entities only to comply with JavaBean's naming scheme. They are precisely identical except for the class name. Or Hibernate would complain that it can't find the getter of the property that is not named as its class name with the first letter underscored.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 5:52 am 
Newbie

Joined: Wed Oct 31, 2007 5:36 pm
Posts: 13
I understand that there are 2 entities mapped to the same table. The typo I'm referring to is the attribute name. Your mapping shows both entities mapped to the same attribute name. I don't think that is intended. Here is how I envision your source files and mapping:


Mytest.java
---------------
package popclonerecords;

public class Mytest implements Serializable {

private Integer id;
private TestBranch testBranch;
private TestBranch2 testBranch2;

public Mytest() {}

public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }

public TestBranch getTestBranch() { return testBranch; }
public void setTestBranch(TestBranch tb) { this.testBranch = tb; }

public TestBranch2 getTestBranch2() { return testBranch2; }
public void setTestBranch2(TestBranch2 tb) { this.testBranch2 = tb; }
}

hibernate.cfg.xml (or other mapping file)
-----------------------
<class name="popclonerecords.Mytest" table="mytest">
<id name="id" column="id" type="integer" unsaved-value="0">
<generator class="increment"/>
</id>

<many-to-one
name="testBranch"
column="t1ID"
class="popclonerecords.TestBranch"
not-null="true" />

<many-to-one
name="testBranch2"
column="t2ID"
class="popclonerecords.TestBranch2"
not-null="true" />

</class>

<class name="popclonerecords.TestBranch" table="TEST_BRANCH">
<id name="id" column="id" type="integer" unsaved-value="0">
<generator class="assigned" />
</id>
...
</class>

<class name="popclonerecords.TestBranch2" table="TEST_BRANCH">
<id name="id" column="id" type="integer" unsaved-value="0">
<generator class="assigned" />
</id>
...
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 3:10 pm 
Newbie

Joined: Wed Oct 31, 2007 1:16 pm
Posts: 4
Thank you so much for your reply and spending time to write down such a clear elucidation! It is my mal-understanding rather than a typo. Now everything works just as I like. Very grateful for your help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.