问题描述
我有一个简单的用户模型,如下所示:
i have a simple User model like this:
package models;
import java.util.*;
import javax.persistence.*;
import play.db.ebean.*;
@Entity
public class User extends Model {
@Id
public Long id;
public String userName;
public String email;
public String workPlace;
public Date birthDate;
@Version
public Long version;
public static Finder<Long,User> find = new Finder<Long,User>(
Long.class, User.class
);
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWorkPlace() {
return workPlace;
}
public void setWorkPlace(String workPlace) {
this.workPlace = workPlace;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
在MySql中已经有一个ID为1,版本为1的记录
in MySql there is already a record with id=1, version=1
我创建一个具有相同ID +版本的新模型,更改用户名
i create a new model with the same id+version, change the userName
然后我尝试使用save()方法
then i try the save() method
它失败并显示:
javax.persistence.PersistenceException: ERROR executing DML bindLog[] error[Duplicate entry '1' for key 'PRIMARY']
at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.execute(DmlBeanPersister.java:116) ~[ebean.jar:na]
at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.insert(DmlBeanPersister.java:76) ~[ebean.jar:na]
at com.avaje.ebeaninternal.server.persist.DefaultPersistExecute.executeInsertBean(DefaultPersistExecute.java:91) ~[ebean.jar:na]
at com.avaje.ebeaninternal.server.core.PersistRequestBean.executeNow(PersistRequestBean.java:527) ~[ebean.jar:na]
at com.avaje.ebeaninternal.server.core.PersistRequestBean.executeOrQueue(PersistRequestBean.java:557) ~[ebean.jar:na]
at com.avaje.ebeaninternal.server.persist.DefaultPersister.insert(DefaultPersister.java:404) ~[ebean.jar:na]
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [na:1.7.0_05]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) [na:1.7.0_05]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) [na:1.7.0_05]
at java.lang.reflect.Constructor.newInstance(Unknown Source) [na:1.7.0_05]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.Util.getInstance(Util.java:386) ~[mysql-connector-java-5.1.18.jar:na]
我在做什么错?插入新记录的作品.因此在我看来,它就像是一些ebean错误,其中它没有意识到应该进行更新.还是我忘记了某个配置?
what am i doing wrong?inserting a new record works. so it looks to me like some ebean bug where its not recognizing that it should do an update. or is it some config that i forgot somewhere?
谢谢
推荐答案
我认为这与play和ebean都执行的字节码增强有关.
I think it's related to the bytecode enhancement that both play and ebean performs.
当我将公共场所与吸气剂和吸气剂混合使用时,我遇到了与您类似的怪异问题.有时在直接使用该字段时未设置值:
I had weird issues similar to yours when I mixed public fields with getters and setters. Sometimes values weren't set when using the field directly:
user.id = 1L; // didn't work
user.setId(1L); // ok
这感觉就像是一个潜在的死亡陷阱,因此我决定保持一致,只使用公共场所.缺点是ebeans autofetch停止工作,但是另一方面,这迫使我使用ebean fetches调整查询.
This felt like a potential death trap so I decided to be consistent and only use public fields. The drawback was that ebeans autofetch stopped working, but on the other hand that forced me to tune my queries with ebean fetches.
这篇出色的帖子揭示了如何玩豆和玩耍执行他们的字节码魔术.
This excellent post by Timo shed some light on how ebean and play performs their bytecode magic.
这篇关于EBean没有进行更新!它正在尝试插入并失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!