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
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.
load() method will throw an exception if the unique id is not found in the database.
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.
In the context of Hibernate's Session, objects can be in one of three possible states: transient, persistent, or detached.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With