Say you have a Car with a collection of Tires.
@Entity
public class Car {
private Long id;
@OneToMany(mappedBy = "car")
private Set<Tire> tires = new HashSet<>();
}
@Entity
public class Tire {
private Long id;
...
}
Now if you want to add a new Car and add existing Tires you could go fetch the entire existing Tire Entities to populate the Car's Set.
Is is possible to simply have some Tire IDs and save the Car without fetching the entire Tire entity(ies) into memory first? Is there a way to save it with just a Tire Id if it were just a Single Tire instance instead of a Set? Using JPA and the Criteria API, or maybe JPQL.
You can use an id to load a proxy "deputy" object, via EntityManager.getReference. It will not issue any DB queries unless you access any property of the object.
If you do access a property, a lazy-loading mechanism will kick-in and load an actual object. The following question demoed the anatomy of the proxy object. Usefull for getting a better picture
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With