Reputation: 427
Person has a foreign key of the field "Language". How can I avoid loading the whole "Language" object if I just got the PK of a language ("json.get("language").asInt()) and want to insert a new Person?
@Entity
public class Person {
String prename;
String lastname;
Language language;
}
Person p = new Person("Robert", "Meyer");
Language l = JPA.em().find(Language.class, json.get("language").asInt();
p.setLanguage(l);
Is it possible to do it in a way without having to load the whole Language object?
Upvotes: 0
Views: 114
Reputation: 24433
You could use EntityManager#getReference().
Get an instance, whose state may be lazily fetched. If the requested instance does not exist in the database, the EntityNotFoundException is thrown when the instance state is first accessed.
Person p = new Person("Robert", "Meyer");
Language l = JPA.em().getReference(Language.class, json.get("language").asInt();
p.setLanguage(l);
Upvotes: 3