-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to force @ManyToOne to do join instead of separate selec
PostPosted: Wed Aug 06, 2008 6:04 am 
Newbie

Joined: Wed Aug 06, 2008 5:14 am
Posts: 4
Hello @,

we have db bottleneck after migration from Hibernate to JPA (3.2.5.ga
and Jboss 4.0.5 GA)

Scenario:

Code:

@Entity
class Order {
 
  @Id @GeneratedValue
  private long;

  @ManyToOne
  @JoinColumn(name = "ADDRESSID")
  private Address address;

}

@Entity
class Address {

  @Id @GeneratedValue
  private long;

}



When Order instance is loaded using em.find than 2 select's are executed:

select * from Order
select * from Address where id = ? // order.addressid

The same is done when I have query which returns X orders (select on Adrress table is executed only one per address instance, e.g. order1 and order2 references same addressA object than only one select is executed to retrieve the addressA)

The same behaviour is in bidirectional scenario.

In original pure hibernate code join is executed immediately. The order's hbm file contains

<many-to-one
name="address"
column="addressId"
class="Address"
cascade="all"
unique="false"/>

I tried also to use hibernate specific @Fetch(SELECT) - without success

I would also like to avoid db schema change unless really neccesary

Thanks for any hint


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 9:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

have you tried @Fetch(JOIN)? That should use a SQL join to load the data. However, I would have expected this to be the default.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 11:17 am 
Newbie

Joined: Wed Aug 06, 2008 5:14 am
Posts: 4
Hello,

also tried JOIN. Without success.

Thx


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 12:40 pm 
Newbie

Joined: Wed Aug 06, 2008 5:14 am
Posts: 4
Hello,

I realized that

em.find(Order.class, id)

will execute select where ORDER and ADDRESS tables are joined.

So it does not work for query like (given the val field is defined for order and there is exactly one instance with value val = val ==> no Exception thrown):

Code:
Query q = em.createQuery("select o from Order where o.val = :val");
Order o = q.setParameter("val", "val").getSingleResult();


Only in this case correct order/address is retrieved but 2 statements are executed in database.

In case you have lot of records in database this is not very nice solution.

I have also tried "select o from Order join o.address where o.val = :val"

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 1:40 pm 
Newbie

Joined: Wed Aug 06, 2008 5:14 am
Posts: 4
Ok - finally fetch join solved the problem


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