0

I am currently doing some testing on using Hibernates detached objects as DTOs. But i have the problem that the detach of the parent bean is not cascaded to the beans contained within a collection of the parent bean (although CascadeType.ALL is set).

For example, i have a parent containing a collection of attributes which have a foreign key to a attribute type. I read it the following way:

Parent bean = (Parent) session.createCriteria(Parent.class)
    .add(Restrictions.eq("id", 1223215031850009727l))
    .setFetchMode("attributes", FetchMode.JOIN)
    .createAlias("attributes.type", "attributetype")
    .uniqueResult();

After reading the bean i detach it from the session:

session.evict(bean);

After that i output some data:

System.out.println(bean.getName());
for (Attribute attr : bean.getAttributes()) {
    System.out.println(attr.getName());
    System.out.println(attr.getType().getName());
}

So far everything works as expected. But if i remove the "createAlias" from the loading of the bean i would expect to get an error on the access of the attribute type as it should also be detached from the session (the attributes-mapping contains CascadeType.ALL). Instead of an error hibernate triggers an SQL to load the type.

Why are the attributes not detached from the session?

Werzi2001
  • 2,035
  • 1
  • 18
  • 41

1 Answers1

0

I found the solution. Cascade (at least DETACH) has also to be set on the foreign key property.

Werzi2001
  • 2,035
  • 1
  • 18
  • 41