-->
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: Changing a Primary Key which other tables reference
PostPosted: Sun Apr 06, 2008 4:39 am 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
I'm stuck on how to implement this and I think my issue must be very common, but my searches have turned up empty. Basically I'm using the standard schema for web security:
Code:
CREATE TABLE Users (Username VARCHAR(255) NOT NULL PRIMARY KEY, Password VARCHAR(255) NOT NULL, Enabled BIT NOT NULL);

CREATE TABLE Authorities (Username VARCHAR(255) NOT NULL, Authority VARCHAR(255) NOT NULL, FOREIGN KEY (Username) REFERENCES Users);

CREATE TABLE Person (Id INT, Name VARCHAR(255), Email VARCHAR(255), UNIQUE (Email), FOREIGN KEY (Email) REFERENCES Users (Username));

I've setup one-to-one mappings between Person and Users and a one-to-many mapping between Users and Authorities. Everything works great. I use those mappings extensively to lazily load or cascadingly save objects.

As you can tell, my usernames are email addresses. So sometimes users need to change their email addresses. I tried the following within a hibernate transaction:
Code:
User user = (User)sessionFactory.getCurrentSession().load(User.class, oldUsername);
user.setUsername(newUsername);

but I get the following exception:
Code:
org.hibernate.HibernateException: identifier of an instance of account.User was altered from scott@test.com to scott@example.com

I even tried playing tricks with hibernate, but nothing works:
Code:
Person person = ((User)sessionFactory.getCurrentSession().load(User.class, oldUsername)).getPerson();
person.getUser().setUsername(newUsername);

This makes sense, I can't just change the primary key and let the corresponding Person and Authorities rows reference a non-existing primary key. I was hoping the cascade="save-update" attributes in my hbm.xml files would magically take care of all this for me, but they don't :(. So what's the solution here? How do I change a user's email address (which is his username)?


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.