-->
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: Repeated column along entity associations
PostPosted: Wed Nov 12, 2008 9:51 am 
Newbie

Joined: Wed Nov 12, 2008 9:35 am
Posts: 5
Hi,
I have a mapping that looks like this

Code:
@Entity
class Parent{
     OneToMany(target = MyObject.class)
     JoinColumn(name = "DEF_ID", nullable = false)
     List<MyObject> listObjects

     OneToMany(target = Child.class)
     JoinColumn(name = "FK", nullable = false)
     List<Child> listChilds
}

@Entity
class Child{
     OneToMany(target = MyObject.class)
     JoinColumn(name = "DEF_ID", nullable = false)
     List<MyObject> listObjects
}

@Entity
class MyObject{
}


i get a
Caused by:
Code:
org.hibernate.MappingException
: Repeated column in mapping for entity: MyObject column: DEF_ID (should be mapped with insert="false" update="false")

even if i put insertable and updatable to false i get the same error
what should i do?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 4:13 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You've defined the relationship to MyObject as OneToMany from both Parent and Child but used the same join column name (DEF_ID). You need 2 different join column names - one for the relationship between MyObject to Parent and one for MyObject to Child.


Top
 Profile  
 
 Post subject: Agreed
PostPosted: Thu Nov 13, 2008 2:45 am 
Newbie

Joined: Wed Nov 12, 2008 9:35 am
Posts: 5
I guess you're right. But i'd like to elaborate my problem a little further.

There are several modules in my project that use "MyObject" in a OneToMany relation and MyObject has been defined with custom ddl statements so that the join column is not a foreign key but is barely another non-nullable column.
The ids that go into the "DEF_ID" column are Unique so that hibernate can identify the objects uniquely.

Funny thing is that multiple entities that point to MyObject work in real time scenarios (ie in Tomcat) where all the entities are in the same sessionFactory. But only during Unit Testing where ONLY 2 objects (ie Parent and Child) point to the same MyObject that a failure occurs.

One more thing: The unique id generator for the entities is a custom generator that is used.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2008 8:35 am 
Newbie

Joined: Wed Nov 12, 2008 9:35 am
Posts: 5
i also tried another thing

Code:
class Parent{
     OneToMany(target = MappingImpl.class)
     JoinColumn(name = "DEF_ID", nullable = false)
     List<MappingImpl> listObjects

     OneToMany(target = Child.class)
     JoinColumn(name = "FK", nullable = false)
     List<Child> listChilds
}


@MappedSuperClass
class abstract SuperClass{
     OneToMany(target = MyObject.class)
     JoinColumn(name = "DEF_ID", nullable = false)
     List<MyObject> listObjects
}

@Entity
class MyObject{

}

@Entity
@AssociationOverride(
     name = "listObjects"
     JoinColumn(name = "DEF_ID", nullable = false)
)
class MappingImpl extends SuperClass{

}



@Entity
@AssociationOverride(
     name = "listObjects"
     JoinColumn(name = "DEF_ID", nullable = false)
)
class Child extends SuperClass{

}


and now i get something like

Code:
org.hibernate.MappingException
: Duplicate property mapping of _listObjectsBackref found



is there another way in which i dont create the MyObjects table through hibernate and indicate that i am going to be storing unique ids into the DEF_ID column so that hibernate can get back to my parent object?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 2:11 am 
Newbie

Joined: Wed Nov 12, 2008 9:35 am
Posts: 5
Ok.
Here's clarifying the situation once again.

Apparently no other module was using the MyObject class and hence it was not crashing during Tomcat deployment. So i was mistaken here.

But nevertheless i found a solution to the problem
When hbm2dll had been turned on, hibernate went and created 2 foreign key associations on "DEF_ID" to the 2 parents that would associate with it. So if one parent would insert into MyObject.class table, it would start crying by saying that there has been a foreign key violation. (because the other parent does not have an Id that matches that of the first parent. )

So the solution was to turn off hbm2ddl and not have any foreign key constraint on the JoinColumn and also to make the column "nullable" so that it would trick hibernate to believe that even though 2 or more parents join on the same column, the parents would insert a null value into the "DEF_ID" column whereby "supposedly existent multiple foreign key constraints" would not throw exceptions.

Code:
@Entity
class Parent{
     OneToMany(target = MyObject.class)
     JoinColumn(name = "DEF_ID", nullable = true)  ----- CHANGE
     List<MyObject> listObjects

     OneToMany(target = Child.class)
     JoinColumn(name = "FK", nullable = false)
     List<Child> listChilds
}

@Entity
class Child{
     OneToMany(target = MyObject.class)
     JoinColumn(name = "DEF_ID", nullable = true) --------- CHANGE
     List<MyObject> listObjects
}

@Entity
class MyObject{
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 4:07 pm 
Newbie

Joined: Wed Nov 12, 2008 9:35 am
Posts: 5
Has anybody seen this mapping called, @Any
I guess its a short cut soln to all that i was talking about earlier :)

Drat. I should have found this annotation sooner!


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.