Reputation: 10896
I have written hql which work correctly in Hql editor but when I apply it in java code it give me Error.
HQL Query which is working correctly
select auhority.id from Authority as auhority where auhority.action = 'USER_MASTER_FULL_ACCESS' and auhority.aclObject is NULL
Java Code for executing query
String hql = "from " + Authority.class.getCanonicalName() + " as model where model." + Authority.ACTION + " = ? and model." + Authority.ACL_OBJECT + " is null";
Query query = getSession().createQuery(hql);
query.setParameter(0, action);
List<Authority> list = query.list();
return list;
Here SecuredObject is ACL_OBJECT which is foreign key for Authority table which can be null. Otherwise other stuff ie add, update and delete is working correctly. I am using MsSqlserver 2005 for database.
It give me error as follow
org.hibernate.TransientObjectException: object references an unsaved transientinstance - save the transient instance before flushing: com.xxxxx.xxxx.xxxx.SecuredObject
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3403)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)....
Upvotes: 3
Views: 1975
Reputation: 691635
The problem is not the query. The problem is that Hibernate needs to flush the session (save changes it has in memory to the database) before executing the query, to make sure that the query takes these changes into account.
And the flush can't be done because some persisted or modified SecuredObject references a transient (i.e. not persisted yet) instance.
As the message says, this transient instance must be saved (or persisted) before the flush can be done.
Upvotes: 1