我对 Play Framework 还很陌生,我正在开发一个现有项目来解决一些问题。
我的问题如下:使用 Ebean update() 函数更新数据库中的某些值(上次编辑的用户和上次编辑的日期)时,出现“OptomisticLockException”。在尝试了我在互联网上找到的几种解决方案后,最有效的一个是使用原始 sql 查询,如下所述:
Update query instead of select + save
PlayFramework 2 + Ebean - raw Sql Update query - makes no effect on db
使用这个,我对日期没有任何问题,但是当我也想更新用户时,我收到以下错误:“[PersistenceException:没有为类模型注册ScalarType。User]”
这是我的查询:
String sql = "UPDATE product_release SET lastedited_date= :id2 lastedited_user_id= :id3 WHERE id= :id1";
SqlUpdate update = Ebean.createSqlUpdate(sql);
update.setParameter("id1", productReleaseId);
update.setParameter("id2", currentDate);
update.setParameter("id3", currentUser);
int modifiedCount = update.execute();
使用以下方法检索当前用户:
public static User findCurrentUser() {
String currentUserTgi = Context.current().session().get(Constants.TGI);
return User.find.where().eq(Constants.TGI, currentUserTgi).findUnique();
}
这是 User 类的开头:
@Entity
public class User extends Model implements Subject{
@Id
public Long id;
@Required
@Column(unique=true)
public String tgi;
@Required
public String firstName;
@Required
public String lastName;
@ManyToMany
public List<SecurityRole> roles;
public User(String tgi) {
this.tgi = tgi;
}
public static User.Finder<Long, User> find = new User.Finder<Long, User>(Long.class, User.class);
我已经在互联网上查找过,但不幸的是我没有找到任何解决方案......最接近的一个是这个 StackOverflow question但它涉及枚举值的特定问题。
有什么想法可以解决这个错误吗?
这是我的第一个问题,如果我不够清楚,请原谅!
谢谢大家的帮助!! :-)
最佳答案
当然,一如既往,在花了几个小时寻找解决方案后,我在这里寻求帮助后几分钟就找到了 -_-
我将其发布在这里,以防其他人遇到同样的问题! 所以基本上我的代码有两个错误:
我忘记了 SQL 查询中的逗号,它应该是这样的:
String sql = "UPDATE product_release SET lastedited_date= :id2, lastedited_user_id= :id3 WHERE id= :id1";
定义参数时,我试图直接传递用户:在我的表“product_release”和列“lastedited_user_id”的数据库中,该 ID 作为指向该用户的链接插入到用户表中。
这是一个错误的想法,我必须将用户的 ID 传递给 数据库 !所以参数的设置应该是这样的:
update.setParameter("id3", currentUser.id);
这就是大家! :-)
关于java - Play 框架(Java)-Ebean : PersistenceException: No ScalarType registered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524168/