0

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.

M314
  • 925
  • 3
  • 13
  • 37
  • *em.getReference(otherClass, otherId)* will not go to the database, and will return the other object. Tried that? – Neil Stockton Aug 31 '15 at 09:43
  • possible duplicate of [hibernate - how to save parent with detached child](http://stackoverflow.com/questions/32130703/hibernate-how-to-save-parent-with-detached-child) – Dragan Bozanovic Aug 31 '15 at 12:50

0 Answers0