Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Hibernate always load associated object even if it can be null?

I have a question regarding Hibernate.

I have two objects with a Many-to-One relationship:

Eg: Object 1:

 public class Person {

 @Basic
 @Column(length = 50)
 protected String name;

 @NotFound(action=NotFoundAction.IGNORE)
 @ManyToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "groupCode", referencedColumnName = "code", updatable=false)
 protected Group group;

 ...all the getters and setters...
 }

Object 2:

 public class Group {
  @Id
  @Basic
  @Column(length = 3, nullable = false)
  protected String code;

  @Basic
  @Column(length = 30, nullable = false)
  protected String groupName;

   @Basic
   @Column(precision = 15, scale = 0)
   protected long exampleFieldId;

   ...rest of code....
  }

I have tried to make this example as simple as possible. My issue is that the associated object (Group) on Person can be null. Currently, Hibernate loads up an instance of Group when I load up a particular Person and throws an exception because it cannot set exampleFieldId to be null (as it is a primitive type).

I can stop this error by changing long to be Long however, I would have thought that the Group object on Person should be null and thus no Group object loaded in the first place?

Does anyone know if Hibernate loads the associated object regardless of it being allowed to be null, or have I missed some important annotation?

Thanks

like image 596
rioubenson Avatar asked Dec 24 '12 11:12

rioubenson


People also ask

What is difference between GET and load in hibernate?

In hibernate, get() and load() are two methods which is used to fetch data for the given identifier. They both belong to Hibernate session class. Get() method return null, If no row is available in the session cache or the database for the given identifier whereas load() method throws object not found exception.

Which of the following method will throw object not found exception when the retrieving record not found in the database?

load() method will throw an exception if the unique id is not found in the database.

Can embeddable class be used as primary key?

The EJB 3.0 specification allows you to define a primary key class as a @Embeddable and use it as the primary key of your Entity bean. One or more properties can be used as members of the primary key for that particular table.

What is the state of object in hibernate?

In the context of Hibernate's Session, objects can be in one of three possible states: transient, persistent, or detached.


1 Answers

As mentioned by Firo:

have you disabled lazy loading and set fetchmnode to join because NHibernate has to fetch them to decide if it should nullify it or not and it can not decide that only with an id

This seems to be the same issue you are experiencing even though it is in NHibernate. Worth checking though!

Why does Hibernate attempt to load "not-found=ignore" associations?

Edit: It could be that you're missing @Fetch(FetchMode.JOIN).

like image 54
Tony Day Avatar answered Sep 21 '22 15:09

Tony Day