Can I use saveOrUpdate() to add a collection(Set) to the database? Can I use the saveOrUpdate() method on the session object for adding a record to the database.
What I need actually is if the given record doesn't exist in the database I need to add it or if it already exists I need to modify it.
Presently I am doing like this,
public void setExample(Example theExample) {
Transaction tx1 = session.beginTransaction();
session.saveOrUpdate(theExample.getField(),theExample);
tx1.commit();
session.close();
}
Here I am getting the value of the primary key field by using theExample.getField() and comparing it with the given record and accordingly doing the update or save.
If I pass a new record ie set the record's primary key as a new value not present in the database (say l2;l2 not in the database), it returns an exception. The exception is as follows:
org.hibernate.MappingException: Unknown entity: l2
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1089)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:409)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:82)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at com.saaa.deen.setExample (ExampleFactory.java:150)
Exception in thread "main"
The same thing if I pass a collection and want to save or modify. Can anyone help me with doing this for a collection and individual record.
Thanks
|