Diaz Pradiananto
Diaz Pradiananto

Reputation: 457

JPA command to get an ID from foreign key

what is different from this syntax on JPA

q.setMerchant(em.find(Merchant.class, m.getId()));

between

q.getMerchant.setId(m.getId());

Upvotes: 1

Views: 204

Answers (2)

user463226
user463226

Reputation: 214

Your first example sets the merchant of q to the merchant found with the I'd m.getId() . Your second example retrieves the merchant returned from the call to q and then sets it's id to m.getId() . It does NOT set the merchant on q to the merchant identified my m.getId()

Upvotes: 1

prajeesh kumar
prajeesh kumar

Reputation: 1966

In the first case, the Merchant object becomes an attached entity and in the second case if it is not already attached it stays detached.

Upvotes: 2

Related Questions