-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: how do we save a collection in the database
PostPosted: Thu Apr 06, 2006 6:21 pm 
Newbie

Joined: Thu Apr 06, 2006 4:04 pm
Posts: 9
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.