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.  [ 4 posts ] 
Author Message
 Post subject: @ManyToOne with @JoinColumn(referencedColumnName=)
PostPosted: Sat Sep 23, 2006 12:42 am 
Beginner
Beginner

Joined: Mon Apr 03, 2006 2:41 am
Posts: 25
Location: Mauritius
Hi,

I have got issues about @ManyToOne annotations in HibernateTools-3.2.0.beta7 in generated 'Domain code' when the @JoinColumns contains multiple columns.

For example

Code:
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name = "Year",  unique = false, nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "CompanyId",  unique = false, nullable = false, insertable = false, updatable = false) })


The issue here is that depending on the environment ( TestNG and microcontainer OR JBoss AS ), the join in the SQL query are not the same.

In TestNG with the above annotations I will get in the generated SQL query

Quote:
table1.Year = table2.CompanyId and table1.CompanyId = table2.Year


but in JBoss AS I will get the correct join :

Quote:
table1.Year = table2.Year and table1.CompanyId = table2.CompanyId


I have found out in the documentation that it's recommanded to use the attribute 'referencedColumnName' of @JoinColumn.

About this attribute I have found in the source code of EntityPOJOClass
Code:
//TODO handle referenced column name, this is a hard one


Since I got this issue in a lot of place I have modified the source code with a simple code which works for me in almost all the case :

if ( isInArray )
{
annotations.append(", referencedColumnName=\"").append( column.getName()
).append( "\"" );
}

isInArray is true in case of a join of multiple columns.

now I get the following generated code which works in both environment:

Code:
@JoinColumn(name = "Year", referencedColumnName = "Year", unique = false, nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "CompanyId", referencedColumnName = "CompanyId", unique = false, nullable = false, insertable = false, updatable = false) })


Since in my database I almost always use the column name of the primary key in the foreign key ( is this a bad or good practise ?), it works at 90%.

What i would like now is to check that the column exists in the referenced table.

could you give me a sample of how access information about the referenced table in the method 'buildJoinColumnAnnotation' of EntityPOJOClass class please ?

I have no clue about how to do that, any help would be really appreciate.

Thanks in advance,
Regards,
Christophe


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 23, 2006 9:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
good question and surprised to hear that different environments handles list of annotations arrays differently.

The reason it says "its a hard one" is that hibernate core model doesn't really care about the "other side" as long as they are in the right order.

so what can we do to figure it out ? good question ;)
You will need to get the actual Collection object, get it's Element and then I *think* getTable should give you what you want...but otherwise you will probably need to explore the object model a bit to make sure it returns what you want ;)

let me know if the above is enough to get you going ...otherwise i'll have to go more deep to find the precise path.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 23, 2006 3:16 pm 
Beginner
Beginner

Joined: Mon Apr 03, 2006 2:41 am
Posts: 25
Location: Mauritius
Thanks for the feedback Max,

sorry but I haven't find the access point to any Collection or the object model :(

I can see only two thing the PersistenceClass and the Cfg2java in the EntityPojoClass class.

Is there a factory or a static method somewhere for access information external to the POJO ?

Thanks in advance,
Christophe


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 23, 2006 3:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the Collection is simply not passed into the method you are referring to.

Follow it up through its callstack and you will at some point bump into a Collection object. It is this object you need to access.

_________________
Max
Don't forget to rate


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