EBean中的更新无效

时间:2014-07-04 00:17:00

标签: playframework playframework-2.0 ebean

我在Java中使用Play / EBean有以下场景

@Entity
public class Client extends Model {

    @javax.persistence.Id
    public Long Id;

    public String host;
    public String mac;
    public String os;
    public Integer cores;

    @OneToOne
    public Groups group;

    public Client(String host, String mac, String os, Integer cores, Groups group){
        this.mac = mac;
        this.host = host;
        this.os = os;
        this.cores = cores;
        this.group = group;
    }
// truncated
}

@Entity
public class Groups extends Model {

    @javax.persistence.Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long Id;

    @Required
    public String groupName;

    @Required
    public String groupDescription;

    @Temporal(TemporalType.TIMESTAMP)
    @Column()
    public Date createTime;

    @OneToMany(mappedBy="group", cascade=CascadeType.PERSIST)
    public List<Client> clients = new ArrayList<>();

    public Groups(String name, String description){
        groupName = name;
        groupDescription = description;
        //clients = new ArrayList<>();
    }
}

我有领域的getter和setter。

客户端已存储在数据库中,在创建新组时,我可以分配客户端。 以下代码用于保存组。

public static Result save(){
        Form<Groups> boundForm = groupForm.bindFromRequest();
        if(boundForm.hasErrors())
        {
            flash("error", "Please correct the form below");
            //group.render(boundForm)
            return badRequest(group.render(boundForm, Client.findAll()));
        }
        Groups group = boundForm.get();
        HashMap<String, String> map = (HashMap<String, String>) boundForm.data();

        Long clientID = Long.parseLong(map.get("Client"));
        Client c = (Client) Ebean.find(Client.class,clientID);
        BeanState bs = Ebean.getBeanState(c);
        group.clients = new ArrayList<>();
        //group.clients.add(c);
        group.clients.add(new Client(c.getHost(), c.getMac(), c.getOS(), c.getCores(), c.getGroup()));
        Ebean.save(group);

        flash("success", String.format("Succesfully added group %s", group));
        //routes.GroupController.list()
        return redirect(routes.GroupController.list());
    }

问题是新记录被插入到Client表中而不是更新前一个记录。

1 个答案:

答案 0 :(得分:2)

对于保存新对象使用save()(就像你一样)

用于更新现有用途update(id)而不是

顺便说一下,当你使用 public 字段时,你不需要覆盖getter / setter,Play会自动执行。