org.hibernate.PersistentObjectException: detached entity passed to persist anomaly

Pathological features: Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist:

In short, this exception occurs is a free object to be persistent (save), its ID ORM framework of both ID values it generates, and this entity ID is already a value. For beginners prone to this anomaly, but some experienced programmers sometimes run into this problem, I have one, so with online friends "to share the experience."

Let ORM is about to build a lasting entity ID value (ORM's primary key strategy), is a typical approach, such as self-growth (even the DBMS to do), UUID, Hibernate framework is more. Therefore, this entity can not be manually assigned on the ID value. I designed the main entity, usually UUID primary key, it is clear that it is character of. Sometimes, however, will find form for Fu form a string of length 0, see html code:

<input name="id" type="text" value=""/>

Note value = ""

If it is increased, then do not need to form such a control form, placement, I usually will add and modify entities in a form to complete the form, I like to use Spring MVC. Then id field is Spring MVC package to have the value of the entity (its value is empty string of length 0). ORM save the above exception came. Many solutions, the author is the entity to be a property of its editor, the editor determine whether it is empty and the length of ID is 0, and if so, into a null. Check ID before you save is also a solution.

Sometimes one on one, save one to many, the related party will be there this situation, so the key to check the ID field on it.

These are I find online. .
But I also experienced similar problems, when used in the modified
em.persist (entity) Save an object then change it em.merg (entity) will be successful. . .

分类:Java 时间:2010-06-05 人气:346
blog comments powered by Disqus


  • Bash string processing (and Java control) - 5 string input (read string) 2011-09-13

    Bash string processing (and Java control) - 5 string input (read string) In Java Scanner class: Scanner.hasNext () & () & Scanner.hasNextLine () & Scanner.nextLine () JavaDoc class Scanner wrote A simple text scanner which can par

  • Bash string manipulation (control and Java) - 5 string input (read string) 2011-09-13

    Bash string manipulation (control and Java) - 5 string input (read string) In Java Scanner class: Scanner.hasNext () & () & Scanner.hasNextLine () & Scanner.nextLine () JavaDoc class Scanner wrote A simple text scanner which can p

  • Java, how to determine the empty string 2010-03-29

    The following is a Java to determine whether the empty string in several ways. Method One: Most people use a method, intuitive and convenient, but inefficient. Method 2: To compare the length of the string, high efficiency, yes I know the best method

  • Javascript in a false, zero, null, undefined and empty string object 2010-10-09

    Turn E4% B8% AD% E7% 9A% 84false% E9% 9B% B6% E5% 80% BCnullundefined% E5% 92% 8C% E7% A9% BA% E5% AD% 97% E7% AC% A6% E4% B8% B2% E5% AF% B9% E8% B1% A1.html In Javascript, we often come into contact w

  • How to determine whether the string is empty string? [C #] 2010-11-12

    1. Three commonly used method for the empty string sub-string: * Length Method: bool isEmpty = (str.Length == 0); * Empty method: bool isEmpty = (str == String.Empty); * General method: bool isEmpty = (str == ""); 2. In-depth internal mechanisms

  • textfield empty string check 2009-12-27

    form often do form an empty string check text text, Extjs provides a relatively easy way. Set textfield's allowBlank property false, The method presented in the form set up client-side validation form.submit switch clientValidation is true, In the me

  • JS empty string is the length of any character in the segmented 2010-10-12

    In the js in an empty string (not null), no matter what the character is divided, and its length after division is 1 instead of 0 we imagine. Examples are as follows: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML&

  • NULL and empty string 2010-10-16

    Mysql in NULL and empty string are two different things, and in the oracle NULL and empty string will do the same treatment; so when a column is set to empty when not allowed, mysql empty string inserted in this column is Yes, would be an error in th

  • ORACLE empty string and NULL 2010-12-30

    Oracle is basically the empty string is handled as an empty NULL, we can be proved from the following. select nvl ('',' NULL ') from dual return' NULL ', select 1 from dual where''is null return 1. If we insert the data into the data table when putti

  • SQL SERVER in the difference between Null and empty string 2011-07-24

    Has been on the difference between NULL and empty string confused. Intuitive feeling empty string as shown below: (Popular speaking, is not NULL and a blank display stuff) The NULL intuitive feel as below: --------------------------------------------

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

processed in 0.345 (s). 12 q(s)