6

Let's say I have a Hibernate entity that declares a OneToMany relationship to a different entity:

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

  [...]
}

When mapping SomeEntity to the corresponding DTO, all I need are the IDs that identify OtherEntity as primary key (i.e., I am not actually interested in OtherEntity instances).

Does Hibernate support this pattern, i.e., only retrieving the IDs of entities referenced via a OneToMany relationship?

I cannot influence how SomeEntity is retrieved (i.e., I have an existing SomeEntity instance retrieved within te scope of the current Hibernate session), but let's assume that lazy loading has not yet taken place, so just retrieving the child objects' IDs (rather than the complete objects) would actually yield a performance benefit.

Thilo-Alexander Ginkel
  • 6,679
  • 9
  • 40
  • 56

4 Answers4

1

Well, if you only need the entities' ids and you want to be economical about it, when you get those entities from the database you should state in your query that you only want to get the ids of each entry, using projections, something like :

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

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

Shivan Dragon
  • 14,526
  • 9
  • 54
  • 96
0

You can try obtaining the primary key without accessing the entity itself (without otherEntities.get(0).getId()). To do this you can use the PersistenceUnitUtil class:

PersistenceUnitUtil#getIdentifier(yourEntity)

The PersistenceUnitUtil can be obtained from the EntityManagerFactory. So it could be something like:

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

Unfortunately, I'm not aware if this will prevent the entity loading from occuring. However, just accessing the otherEntities collection or even obtaining references to each entity will not make the instance to be loaded; you need to invoke a method on the fetched entity in order to be sure it will be loaded.

You also might consider creating a @NamedQuery and return only the OtherEntity ID's.

HTH!

Piotr Nowicki
  • 16,436
  • 8
  • 54
  • 79
0

From 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 instrument it. There is an example here:

Making a OneToOne-relation lazy

Community
  • 1
  • 1
dcernahoschi
  • 13,894
  • 5
  • 31
  • 55
0

You can use the below HQL as told in the documentation to establish this.

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

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

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

ManuPK
  • 10,995
  • 9
  • 54
  • 75