gpt4 book ai didi

java - ebean 的更新方法在 playframework 中不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:41:23 24 4
gpt4 key购买 nike

最近开始使用Play framework 2.3.8。

但是模型的更新是我遇到了麻烦,没有很好地工作。

顺便说一下,save 方法有效。

更新不适用于如下代码。(不会持久化到数据库。)

User user = User.findByEmail(email);
user.remoteAddress = remoteAddress;
user.userAgent = userAgent;
user.latitude = latitude;
user.longitude = longitude;
user.lastLoginAt = new Date();
user.update();

但是以下代码将按预期工作。

User newUser = new User();
newUser.id = user.id;
newUser.remoteAddress = remoteAddress;
newUser.userAgent = userAgent;
newUser.latitude = latitude;
newUser.longitude = longitude;
newUser.lastLoginAt = new Date();
newUser.update();

为什么我不能更新原始实例?

用户类如下。

package models.entities;

import java.util.*;

import javax.persistence.*;

import com.avaje.ebean.annotation.*;

import play.db.ebean.*;
import play.db.ebean.Model.Finder;

import play.data.validation.Constraints.*;

import play.Logger;

import models.services.*;

/**
* @author satouf
*/
@Entity
@Table(name="user")
public class User extends Model {

@Id
public Long id;

@Column(nullable = false, columnDefinition = "varchar(32)")
public String userIdentifier;

@Column(nullable = false, columnDefinition = "varchar(255)")
public String loginId;

@Column(columnDefinition = "text")
public String loginPassword;

@Column(columnDefinition = "varchar(64)")
public String handleName;

@Email
@Column(nullable = false, columnDefinition = "varchar(255)")
public String email;

@Column(nullable = false, columnDefinition = "smallint default 0")
public short status;

@Column(columnDefinition = "varchar(64)")
public String lastRemoteAddress;

public String lastUserAgent;

public Date lastLoginAt;

public double latitude;

public double longitude;

@UpdatedTimestamp
public Date updatedAt;

@CreatedTimestamp
public Date createdAt;

public static Finder<Long, User> finder = new Finder<Long, User>(Long.class, User.class);

@Override
public String toString(){
return ("[id: " + id + ", userIdentifier: " + userIdentifier + ", loginId: " + loginId + ", handleName: "
+ handleName + ", latitude: " + latitude + ", longitude: " + longitude + "]");
}

}

最佳答案

由于某些奇怪的原因,直接访问成员无法更新 ebean。

添加setter,例如:

private setUserAgent(String val) {  
this.userAgent = val;
}

并调用:

user.setUserAgent(your_val);  
user.update();

关于java - ebean 的更新方法在 playframework 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28915938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com