简体   繁体   中英

Unable to save entity changes: identifier of an instance was altered

When I change the "Owner" of a piece of content on my site, I'm getting an error shown below. I'm very confused why I'm getting this error. It seems as though it's trying to change the ID of the item itself. Everything looks correct... am I looking in all the right places?

The error

Caused by: org.hibernate.HibernateException: identifier of an instance of com.site.model.User was altered from 13 to 72

My JSP

When I change owner.id to owner I get a string/integer mismatch.

        <label class="formLabel">Owner <img src="/images/s.gif" class="required"/><br/>
            <form:select path="owner.id" id="owner">
                <form:options items="${owners}" itemValue="id" />
            </form:select>
        </label>

My Service

    Content cm = em.merge(content);
    em.flush();

Content model

@NotNull
@JoinColumn(name = "owner_id", referencedColumnName = "id")
@ManyToOne
@IndexedEmbedded
@JsonIgnore
private User owner;

User model

@Id
@Basic(optional = false)
@Column(name = "id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
@JsonIgnore
private Integer id;

You are telling spring to overwrite the id field on the User object that is there, with a different value of id taken from the JSP. If you want to change the owner, you need to get the entire User object that is the new owner, and change the reference to point to it on the parent record. You can't just change the ID number on an existing object.

There are a number of ways to accomplish this, commonly a PropertyEditor is used so you bind a number directly to the value 'owner' and supply a class that tells spring how to turn a number into an instance of User.

On the form, bind to 'owner' instead of 'id':

<form:select path="owner" id="owner">
  <form:options items="${owners}" itemValue="id" />
</form:select>

In the controller, initialize a property editor:

@InitBinder
private void registerPropertyEditor(DataBinder binder) {
    binder.registerCustomEditor(User.class, "owner", new UserPropertyEditor(userService));
}

Then you make a class that tells spring how to turn ID numbers into users:

public class UserPropertyEditor extends PropertyEditorSupport {

    private UserService userService;

    public UserPropertyEditor(UserService userService) {
        this.userService = userService;
    }

    @Override
    public String getAsText() {
        //Handle null value, value of incorrect type, etc here

        return String.valueOf(((User) getValue()).getId());
    }

    @Override
    public void setAsText(String id) throws IllegalArgumentException {
        //handle empty string, number format exception, etc

        User user = userService.getUser(integerId);

        setValue(user);
    }

}

I face this problem too. In my case, i cant input number 7 in sla field. But if i change the value other than 7 the process run normally. The error message that appear such as .... was altered from 7 to null

I have try many approach such us change type data from Integer to Long, create a new model, but the problem still exist.

Until I change one of property of model, example :

@Column(name = "sla")
private String sla;

to :

@Column(name = "sla")
private String slavalue;

Viola, the problem was gone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM