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