[英]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.
似乎它正在尝试更改项目本身的 ID。 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我的 JSP
When I change owner.id
to owner
I get a string/integer mismatch.当我将
owner.id
更改为owner
我得到一个字符串/整数不匹配。
<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.您告诉 spring 覆盖用户对象上的
id
字段,使用从 JSP 中获取的不同id
值。 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.如果要更改所有者,则需要获取作为新所有者的整个 User 对象,并将引用更改为在父记录上指向它。 You can't just change the ID number on an existing object.
您不能只更改现有对象的 ID 号。
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.有多种方法可以实现这一点,通常使用
PropertyEditor
将一个数字直接绑定到值 'owner' 并提供一个类来告诉 spring 如何将一个数字转换为 User 的实例。
On the form, bind to 'owner' instead of 'id':在表单上,绑定到 'owner' 而不是 '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:然后创建一个类,告诉 spring 如何将 ID 号转换为用户:
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.
就我而言,我无法在 sla 字段中输入数字7 。 But if i change the value other than 7 the process run normally.
但是,如果我更改7以外的值,则进程会正常运行。 The error message that appear such as
.... was altered from 7 to null
出现的错误信息如
.... 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.我尝试了很多方法,例如我们将类型数据从 Integer 更改为 Long,创建新模型,但问题仍然存在。
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中提琴,问题解决了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.