0

The problem is not always reproducible, it's when getting my object from the database using CriteriaBuilder.

Code :

@Entity
@Table(name = MyEntityObject.TABLE_NAME)
@Access(AccessType.FIELD)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@BatchSize(size = 50)
public class MyEntityObject extends AbstractCodeEntity implements Comparable<MyEntityObject> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<MyEntityObject> criteriaQuery = builder.createQuery(MyEntityObject.class);

Root<MyEntityObject> root = criteriaQuery.from(MyEntityObject.class);

Predicate condition = builder.equal(root.get(MyEntityObject.code), code);
boolean distinct = FetchBuilder.buildFetchs(root, fetchsOperations);
criteriaQuery.distinct(distinct).where(condition);

TypedQuery<MyEntityObject> query = entityManager.createQuery(criteriaQuery);

return query.getSingleResult();

Of course MyEntityObject has attributes as lazy and others as eager

When I'm debugging, the returned object contains a handler = {JavassistLazyInitializer@19574} + objet attributes which are null.

I've tried to find out if hibernate go look for it on the database => he did

Then if the object exists on 1st cach => it doesn't

Anyone have an idea ?

Belham
  • 155
  • 1
  • 1
  • 12
  • I'm not sure what the question is, that behavior is perfectly normal when getting an Entity that has relationships with `FetchType.LAZY`. Are you aware of Hibernate/JPA Lazy and Eager fetching/loading and what it does? See: [Difference between FetchType LAZY and EAGER in Java Persistence API?](https://stackoverflow.com/questions/2990799/difference-between-fetchtype-lazy-and-eager-in-java-persistence-api) – OH GOD SPIDERS Nov 20 '20 at 12:00
  • @OHGODSPIDERS Yes, I'm aware of that, but here the object I'm looking to get itsef looks lazy, I don't really understand this behaviour. (I added the code above) – Belham Nov 20 '20 at 13:03
  • Oh, so the whole root `MyEntityObject` that you select is returned as a Hibernate Lazy-Load Proxy object instead of an actual object? If that is the case I'm not able to explain that behavior either. – OH GOD SPIDERS Nov 20 '20 at 13:20
  • Did you give it a try without the CacheConcurrencyStrategy.READ_WRITE? – Thomas Nov 20 '20 at 13:42
  • @Thomas it's not that easy to reproduce. Can you explain how does it impact? – Belham Nov 20 '20 at 14:08
  • It's just a guess. What is actually the problem with JavassistLazyInitializer? Do you get an exception when accessing the fields? – Thomas Nov 20 '20 at 14:37
  • @Thomas Yes, good question, the object becomes null later on the code – Belham Nov 20 '20 at 15:01
  • It becomes null as a result of serializing the object which is considered empty – Belham Nov 20 '20 at 15:17

1 Answers1

0

Two notes from my side - not sure if this fully covers your issues:

Thomas
  • 451
  • 2
  • 5