I am using Spring 3.2.3 and Hibernate 4.2.3 and JDK 7.
I have a simple object:
@Entity public class Language { @Id @GeneratedValue private long id; @Column(nullable = false, length = 3, unique = true) private String code; }
I saved an instance of this object using the annotated @Service class with the annotated @Transactional method, which uses a DAO that saves the object with
sessionFactory.getCurrentSession().save(object);
After that, I used the saved Language object to create EntityX , which used it with respect to ManyToOne ...
lang=new Language(); // ... languageService.saveLanguage(lang); e=new EntityX(); // ... e.setLanguage(lang); otherService.saveEntity(e);
and EntityX is defined as ...
@Entity public class EntityX { @ManyToOne @JoinColumn(nullable = false) private Language language;
I always get an exception
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: somepackage.Language
I am trying to use some cascading definitions regarding EntityX to Language , as suggested in other posts, but this has no effect.
If I reload the saved Language object, finding it with code using some HQL query, everything works fine, but that it is far from being “good”.
Unfortunately, the save(...) org.hibernate.Session does not return the saved object.
Does anyone have any ideas how to solve it?