You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by vinbr88 <vi...@googlemail.com> on 2012/06/14 13:28:23 UTC

Change value of primary key field

Hello,

I have a table in my database which stores our user data. The first field is
the username which is primary key. I can change the username in the database
without any problem. The primary key is never referenced as foreign key of
an other table. But when I change the username of a persisted object (user)
in my application and merge the new user with his new username, I get this
message:

javax.servlet.ServletException: javax.el.ELException:
<openjpa-2.2.0-r422266:1244990 nonfatal user error>
org.apache.openjpa.persistence.InvalidStateException: Attempt to change a
primary key field of an instance that already has a final object id.  Only
new, unflushed instances whose id you have not retrieved can have their
primary keys changed.
FailedObject: jpa.RolloutUser-TEST
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:229)

org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:357)

I don't understand what this means. Does it mean, that it isn't even
possible changing the value of an primary key field with openJPA? Something
like a security feature?

The only thing I could do imho is deleting the user and create a new one
with the new username. But this is not very smart.

Do you have any suggestion?

Thanks in advance?

--
View this message in context: http://openjpa.208410.n2.nabble.com/Change-value-of-primary-key-field-tp7580285.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Change value of primary key field

Posted by vinbr88 <vi...@googlemail.com>.
Thank you afryer for that helfpul explanation.
The User is and will never referenced by an other table. Therefore the
database allows changing username/primary key. I am not allowed to change
the tables, so I can't add an Id attribute for instance.

I didn't know that the EntityManager caches entities by primary key.
Changing the key would surely cause problems. So I solved my problem
deleting the user and adding the user again with new values.

Thank you for your help.

--
View this message in context: http://openjpa.208410.n2.nabble.com/Change-value-of-primary-key-field-tp7580285p7580301.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Change value of primary key field

Posted by afryer <ap...@hotmail.com>.
If a user can change their userName, then userName is not really a primary
key for the user entity.  You should use a different field that won't
change, like an ID or emailAddress as the primary key.

The trouble with changing the primary key in a database is if you have other
tables that reference the User table using the primary key, then those
references are no longer valid.  You would not only have to update the
UserName column in the User table but also every other table that links to
it. 

In JPA, the EntityManager would cache your entities by primary key, so
changing that would create all sorts of issues, not to mention if you had a
2nd level cache as well.  Everything would suddenly get so much harder and
break if you were allowed to change an @Id column.

--
View this message in context: http://openjpa.208410.n2.nabble.com/Change-value-of-primary-key-field-tp7580285p7580297.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.