Hibernate - Set key foreign object without fetching object. Direct by Id

0

Does anyone know if it is possible for me to set an object that is foreign key if I have to fetch it. That is, just setting the direct id?

Example:

low.CompanySet (daoCompany.searchPorId (new Long (rsBeixa.getString (6))));

way you'd like to do

low.CompanyAddress (new Long (rsLoad.getString (6));

    
asked by anonymous 20.02.2017 / 21:41

1 answer

0

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.

    
20.02.2017 / 22:06