I have two entities: MyClassA and MyClassB. The second one is big and havy.
@Entity
@Table(name = tableA)
public class MyClassA {
@Id
private Long id;
@OneToOne
@JoinColumn(name = "id_b")
private MyClassB connetctedB;
... //some other values
}
I want do create and store new entity of abouve class. To my service class I pass some values and id of MyClassB entity. Usual solutaion that I saw was:
public void createNewA(long idB, String name) {
MyClassA a = new MyClassA();
a.setName(name);
MyClassB b = getBByid(idB); // <- I want to avoid this
a.setConnetctedB(b);
entityManager.persist(a);
}
The problem is the fact that MyClassB is havy and operation getBByid consume a lot of resources. How can I create and store new object of MyClassA in this case? I have already id of MyClassB, so it should be easy, but I whould have to use native query, unfortunately hibernate do not support native inserts.