-->
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.  [ 3 posts ] 
Author Message
 Post subject: Email is my primary key - nothing else
PostPosted: Fri May 07, 2004 6:26 pm 
Newbie

Joined: Fri May 07, 2004 6:15 pm
Posts: 2
I know this is strange, but hey ...

I'm parsing out some user information from an XML feed. The only key I have to work with is the value of the email.

Therefore, my db is setup to have the email as a primary key.

I've used Hibernate on a couple simple projects up to now (this one being one of them), but I've always had ID's to work with. It's the first time I have a string key to work with that isn't autogenerated, and it's not working out at all.

Mapping file looks something like:
<class name="clas" table="table">
<id name="Email" type="string">
<column name="email" unique="true" length="250" not-null="true"/>
<generator class="assigned"/>
</id>

<property name="status" type="boolean">
<column name="status" length="50" not-null="true"/>
</property>
......
</class>


And I've got some simple code to do the insert/update:
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(profile);
tx.commit();
HibernateUtil.closeSession();

The error I get when a row does not exist in the database (therefore, when it's time to do an update) is:
There was a fatal exception:net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)

I tried to get around it by doing a session.get() first, but my key is a String , hence not Serializable, so I'm not sure how to do it.

Any help for this embarassingly stupid problem of mine would be appreciated.

(I'm using the latest version of Hibernate)
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 08, 2004 12:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
If its assign use the sess.save() or sess.update() approrpriately. You could use the version tag's unsaved-value I guess (but I have not tried it).


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 08, 2004 9:50 am 
Newbie

Joined: Fri May 07, 2004 6:15 pm
Posts: 2
Thanks for the reply David.

Turns out I couldn't use what you mentionned. I did find a clumsy solution though and I'm sure there's a much easier way.

Basically, I use a Query to deteremine if the row exists in the db (where Email=?). If the List returned is > 0 then I know to do an update(). Else I do a save(). Of course, I need 2 sessions to do it.

Not great, but it works. I'm guessing that in a few months when I have a bit more experience with this sublime product, I'll figure out a better solution.


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

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.