If you know that the entity already exists in the database, you can use the getReference
method of EntityManager
. Using this method, Hibernate will not fetch the entity in the database, it will just create a proxy .
It would basically be like this:
//Ele não faz um SELECT aqui, apenas cria um proxy do objeto
Empresa empresa = entityManager.getReference(Empresa.class, idDaEmpresa);
baixa.setEmpresa(empresa);
Hibernate will only attempt to load the attributes of the class if you evoked some method from it. If you try to invoke a method or flush the entity does not exist in the database, a EntityNotFoundException
is posted.