Hibernate : ManyToOne getId eagerly and avoid saving ManyToOne object


Today I found good trick to achieve this.

i.e. directly use ManyToOne to getId
and to get the full object lazily loaded
and avoid saving the same object


It is from blog post
http://stackoverflow.com/questions/1900788/hibernate-manytoone-save-org-hibernate-transientobjectexception


ManyToOne(fetch=FetchType.EAGER)
@**JoinColumn**(name = "DEPARTMENT_ID", referencedColumnName = "DEPARTMENT_ID", **insertable=false, updatable=false**)
private Department department;

@Column(name = "department_id")
private Long departmentId;

Make sure to annotate all fields with @Column annotation with the name, otherwise hibernate will throw exception