11

I have a component mapped using Hibernate. If all fields in the component in the database are null, the component itself is set to null by hibernate. This is the expected behavior and also what I need.

The problem I have, is that when I add a bag to that component, the bag is initialized to an empty list. This means the component has a non null value... resulting in the component being created.

Any idea how to fix this?

<class name="foo.bar.Entity" table="Entity">
<id name="id" column="id">
    <generator class="native" />
</id>

<property name="departure" column="departure_time" />
<property name="arrival" column="arrival_time" />

<component name="statistics">
    <bag name="linkStatistics" lazy="false" cascade="all" >
        <key column="entity_id" not-null="true" />
        <one-to-many class="foo.bar.LinkStatistics" />
    </bag>

    <property name="loggedTime" column="logged_time" />

    ...
</component>

A criteria with Restirctions.isNull("statistics") does return the expected values.

6
  • Please add information about the context: Who sets the component to null? Which is the component you're talking about? Which is the container? Commented May 7, 2010 at 7:14
  • Aaron, Hibernate leaves the component property (statistics, in Entity class) to null. Commented May 7, 2010 at 7:52
  • Seems to be a rather old problem but I have it as well and couldn't find any solution. Did you have any luck in the meantime? Commented Aug 21, 2010 at 13:08
  • did you look at null vs empty collections in hibernate@stackoverflow ? Commented Jun 27, 2011 at 22:14
  • do you have to set lazy attribute of your bag to false ? Commented Feb 17, 2012 at 9:00

3 Answers 3

1

The basic problem here is that Hibernate can't distinguish between null collections and empty collections, so it treats them both as empty: non-null.

I suggest you change your Statistics component to an real entity instead. Then your foo.bar.Entity class has a reference, which can be null. This is not ideal because you'll have to create another table to store the Statistics entity, but if you want the null vs empty semantic distinction, that's a way to get it.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct. The concept of collection doesn't really exist in the RDBMS so Hibernate (or any ORM) has to do something.
0

I can't verify this but here's an idea:

public void setListProperty(List list) {
  if (list == null || list.size() == 0) {
    this.listProperty = null;
  } else {
    this.listProperty = list;
  }
}

Obviously not ideal but might be a workaround for you...

1 Comment

Does this approach really works for you? From my experience such tricks in setters lead to exception "another collection is already associated with session" in Hibernate (because Hibernate will remember the empty collection it intended to associate with parent entity). So the code above will work when entity became detached (session it was loaded with was closed), but will fail otherwise. Using null in getter is much better :)
0

Maybe this can help .it doesn't solve the problem of distinguishing between null and empty bag but it's a workaround. As you might know you can introduce an interceptor to your session that can intercept in certain actions like saving or updating entities then you can use this interceptor to check the state of your component and if it is empty make it null again so that hibernate won't save empty values. here's the docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.