I think it does not make sense to execute query and load full entity if I just need to fetch id of proxy object and this proxy has FK id. I have Client and Account entities. Account table has a FK on Client. (id_client) relationship - one-to-many:
{code} @Entity class Client { @OneToMany(mappedBy="client") private List<Account> accounts; }
@Entity class Account { @JoinColumn(name = "id_client") @ManyToOne(fetch = FetchType.LAZY) private Client client; }
{code}
{code} Account account = em.find(Account.class, 2); account.getClient().getId(); {code}
console output: {code} Hibernate: select account0_.id_account as id_accou1_0_0_, account0_.AMOUNT as AMOUNT2_0_0_, account0_.id_client as id_clien3_0_0_ from ACCOUNT account0_ where account0_.id_account=2 Hibernate: select client0_.ID_CLIENT as ID_CLIEN1_4_0_, client0_.age as age2_4_0_, client0_.create_date as create_d3_4_0_, client0_.modify_date as modify_d4_4_0_, client0_.NAME as NAME5_4_0_, client0_.vip as vip6_4_0_ from client client0_ where client0_.ID_CLIENT=1 {code}
There is id_client column in Account table {code} id_account | amount | id_client {code} Account object has javassist proxy client object, that proxy client has JavassistLazyInitializer handler field and there is id field. Take a look at attached file.
Why don't just retrieve that id from handler object??? It would be great performance optimization I suppose, cause very often just id is needed P.S.: the same stuff is for load method: {code} Client client = em.unwrap(Session.class).load(Client.class, 1); // - proxy client.getId(); // select is executed {code} |
|