-->
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: OneToMany: wrong parameter binding order
PostPosted: Wed Oct 04, 2006 9:59 am 
Newbie

Joined: Thu Aug 24, 2006 11:10 am
Posts: 16
Hi there,
I'm using hibernate 3.2 x with EJB3 annotations.
I tried to fetch lazy a set oneToMany data e.g. product --> tracks
One product has a numer of tracks.

The primary key of the product and track table is a compsite key and contains a client_key as second column.

Using XML configuration works fine, I retrieve all tracks.

Using annotation configuration does not work properly since hibernate changes the sequence of the prod_no, client_key in a wrong binding order.

The client_key 0000 is bound as first parameter and the prod_no as second one, but the where clause is just vice verca:


where tras009s0_.prod_no=? and tras009s0_.client_key=?

2864 [main] DEBUG org.hibernate.type.StringType - binding '0000' to parameter: 1
2864 [main] DEBUG org.hibernate.type.StringType - binding '260600' to parameter: 2

This causes hibernate not to receive any tracks.

In all pojos the order of the declaired and annotated properties is prod_no at first, client_key as second parameter. Anyway hibernate changes the binding order into a backwards order, see above.

I've added the JoinColumns annotation to the product pojo template (generated with hibernate tools) but the default order does not take affect to the binding order.

@JoinColumns( {
@JoinColumn(name="prod_no"),
@JoinColumn(name="client_key") } )

Changing this manually to the backwards order helps, now the sql statement is generated in a different order and the binding matches, but this can't be a general solution: changing all pojos manually to that order.

where tras001x0_.client_key=? and tras001x0_.prod_no=?

@JoinColumns( {
@JoinColumn(name="client_key"),
@JoinColumn(name="prod_no") } )

Can anyone explain what the problem here is and possibly provide a solution?

Thanks
MS


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 4:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I find it surprising but it can theorically happen (nobody reported it though).
You can explicit the referencedColumnName to avoid any issue.

What JDK are you running?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 6:53 am 
Newbie

Joined: Thu Aug 24, 2006 11:10 am
Posts: 16
Thanks Emmanuel,
using the referencedColumnName works fine :-)

Possibly it makes sense to extend hibernate tools to add the join columns and referencedColumnName annotation by default. Currently it doesn't so I've extended the templates by my self.

Regarding the JDK I'm currently using 5.0_06. This is not the latest one do you thing it helps to upgrade to _09? Is this a Java Bug?

One interesting thing:
I've got a lot of references (oneToMany and manyToOne) within the main pojo. I wanted to provide you a simple test case an removed mostly all of the references to other pojos except the one who causes the problem. After I've done this it works fine. So at least it seem that there are some side effects between the annotations and I'm sorry to say that I can't provide any example except the whole project, but it doesn't make sence to do this public.

Best
Meinolf


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 10:52 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
meinolf wrote:
Possibly it makes sense to extend hibernate tools to add the join columns and referencedColumnName annotation by default. Currently it doesn't so I've extended the templates by my self.

Year, that might make sense, verbose but more safe for composite pk/fk. If you can provide a patch that'd be cool.

meinolf wrote:
Regarding the JDK I'm currently using 5.0_06. This is not the latest one do you thing it helps to upgrade to _09? Is this a Java Bug?

No I thought maybe the IBM JDK or JRockit behaved differently, but apparently you're using Sun's


meinolf wrote:
I've got a lot of references (oneToMany and manyToOne) within the main pojo. I wanted to provide you a simple test case an removed mostly all of the references to other pojos except the one who causes the problem. After I've done this it works fine. So at least it seem that there are some side effects between the annotations and I'm sorry to say that I can't provide any example except the whole project, but it doesn't make sence to do this public.


Maybe one of the getter or one of the field marked @Id is not ordered compared to the JoinColumn description

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 3:58 pm 
Newbie

Joined: Thu Aug 24, 2006 11:10 am
Posts: 16
Hi Emmanuel,
I just was happy that I got the problem solved but get stuck in a followup problem. I've added the referencedColumnName for every ManyToOne/OneToMany relation by using the same name as the key property but get an hibernate configuration error now since some foreign key columns are renamed to a different name in the database.

E.g. org_country_key references country_key

Code:
  @JoinColumns( {
      @JoinColumn(name="org_country_key",
         referencedColumnName="country_key", unique=false, 
         nullable=false, insertable=false, updatable=false),
      @JoinColumn(name="client_key",
         referencedColumnName="client_key", unique=false, nullable=false,
         insertable=false, updatable=false) } )


Is there a way on how to get the columnName of the referencedColumn within the Ejb3PropertyGetAnnotation.ftl template since I don't want to change the hibernate Java sources for this issue?

I can't find anything on org.hibernate.mapping.Column.


Best
Meinolf

P.S: I tried to use jdk 1.5_09 but doesn't help either. Regarding the order of the @Id columns they are all fine for those objects that were involved in that test method. All sources are generated and not change. Hibernate Tools does generate a fine order of the annotations and the SQL at runtime is fine as well. Only the binding order is wrong when the referencedColumnName attribute is not defined. Must be a side effect in my project with other non involved fk references.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 6:11 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Since the ordered is kept in the hibernate metadata, you should be able to navigate the model and find the according column. I think the metadata model is accessible when playing with the templates

_________________
Emmanuel


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.