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.  [ 1 post ] 
Author Message
 Post subject: @JoinTable results in redundant join in the generated SQL
PostPosted: Thu May 13, 2010 3:29 pm 
Newbie

Joined: Mon Aug 10, 2009 10:14 pm
Posts: 3
An Account can have several Clients:

@Entity
@Table(name = "CLIENT")
public class Client implements Serializable {
private String street;
private String code;
private String city;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@ManyToOne
@JoinTable(name = "CLIENT_ACCOUNT",
joinColumns = {@JoinColumn(name = "FK_CLIENT", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "FK_ACCOUNT", referencedColumnName = "ID")})
private Account account;
.....
}

@Entity
@Table(name = "ACCOUNT")
public class Account implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String number;
private double balance;

@OneToMany (fetch = FetchType.EAGER, mappedBy="account")
private Set<Client> holders;
...
}


When we load the Account entity:
Account a1 = (Account) session.load(Account.class, accountId);

We see that there is a redundant join at the end - from the CLIENT table to the CLIENT_ACCOUNT table. Normally, the JoinWalker detects circular joins while
traversing associations; however, in this case, the 'Select' statement generated by the persister for the Client entity is responsible for generating the extra join with the CLIENT_ACCOUNT table at the end:

select
account0_.id as id2_1_,
account0_.balance as balance2_1_,
account0_.number as number2_1_,
holders1_.FK_ACCOUNT as FK1_2_3_,
client2_.id as FK2_3_,
client2_.id as id0_0_,
client2_.city as city0_0_,
client2_.code as code0_0_,
client2_.street as street0_0_,
client2_1_.FK_ACCOUNT as FK1_1_0_
from
ACCOUNT account0_
left outer join
CLIENT_ACCOUNT holders1_
on account0_.id=holders1_.FK_ACCOUNT
left outer join
CLIENT client2_
on holders1_.FK_CLIENT=client2_.id
left outer join
CLIENT_ACCOUNT client2_1_
on client2_.id=client2_1_.FK_CLIENT
where
account0_.id=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.