Effectively identify object identifiers referenced by OneToMany relationships

Let's say I have a Hibernate object that declares OneToMany relation to another object:

 @Entity public class SomeEntity { @OneToMany(fetch = FetchType.LAZY) private List<OtherEntity> otherEntities = new LinkedList<OtherEntity>(); [...] } 

When matching SomeEntity with the corresponding DTO, all I need is identifiers that identify OtherEntity as the primary key (i.e., I am not actually interested in OtherEntity instances).

Does Hibernate support this pattern, i.e. retrieves only the identifiers of the objects referenced by the OneToMany ?

I can’t influence how SomeEntity restored (i.e. I have an existing SomeEntity instance obtained within te area of ​​the current Hibernate session), but suppose that lazy loading has not yet taken place, so just get the identifiers of the child objects (and incomplete objects) will actually benefit performance.

+6
source share
4 answers

Well, if you only need entity identifiers and you want to be economical when you get these entities from the database, you should indicate in your request that you want to get only the identifiers of each record using forecasts, something like:

  SELECT Entity.id as entity FROM Entity WHERE ... 

This will return an array of objects of the same type as the field type Entity id.

+1
source

You can try to get the primary key without access to the object itself (without otherEntities.get(0).getId() ). For this you can use the PersistenceUnitUtil class:

PersistenceUnitUtil # getIdentifier (yourEntity)

PersistenceUnitUtil can be obtained from EntityManagerFactory . It could be something like:

 EntityManager em = ... PersistenceUnitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil(); 

Unfortunately, I do not know if this will prevent the object from loading. However, accessing the otherEntities collection or even getting references to each object will not load the instance; you need to call the method on the extracted object to be sure that it will be loaded.

You can also consider creating @NamedQuery and return only OtherEntity tags.

NTN!

0
source

From the hibernate reference, section 2.2.2.1.

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-property

Declare your columns as lazy initialized:

@Basic (fetch = FetchType.LAZY) private String getYourProperty () {}

You also need to disable proxies for your entity class and byte tool. Here is an example:

Creating a OneToOne lazy relationship

0
source

You can use the HQL below as described in the documentation to set this up.

 session.createQuery(select new OtherEntity(oe.id) OtherEntity oe where oe.parentSomeEntity.someId = :someId).list();//also set someId. 

Add a constructor to OtherEntity to set the id , there should also be a mapping in SomeEntity to OtherEntity .

This HQL will provide you with a List<OtherEntity> with only the id set to the bean.

0
source

Source: https://habr.com/ru/post/901520/


All Articles