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=?
|