【问题标题】:Ebean delete object not refresh realtionshipEbean删除对象不刷新关系
【发布时间】:2015-05-16 15:40:35
【问题描述】:

下面的问题让我很困惑。我想从关系中删除一个测量值,并且引用的对象应该更新测量值。那么我的问题是什么,如果我删除一个测量值,那效果很好,但是如果我从固化对象中获取所有测量值,我会看到删除对象在里面。恢复关系的最佳方式是什么?

Measurement.java

@Entity
@Table(name = "measurements")
public class Measurement extends Model {

    @Id
    public Long id;

    @Formats.DateTime(pattern = "dd.MM.yyyy")
    public Date created = new Date();

    @Constraints.Required
    public double weight;

    @Constraints.Required
    public double belly;

    @Constraints.Required
    public double thigh;

    @Constraints.Required
    public double gluteal;

    @ManyToOne
    @JsonBackReference
    public Cure cure;
}

Cure.java

@Entity
@Table(name = "cures")
public class Cure extends Model  implements Comparable<Cure> {

    @Id
    public Long id;

    @Formats.DateTime(pattern = "dd.MM.yyyy")
    public Date created = new Date();

    public int startHour = 10;

    @ManyToOne
    @JsonIgnore
    public Challenge challenge;

    @JsonManagedReference
    @OneToMany
    public List<Measurement> measurements;
}

我应该处理删除的操作

public static Result delete(Long id){
    Measurement measurement = Measurement.find.byId(id);

    if(measurement == null && !measurement.belongsToUser(user)){
        return noContent();
    }

    measurement.delete();
    return ok();
}

【问题讨论】:

    标签: playframework playframework-2.0 ebean


    【解决方案1】:

    嗯,我没有在你的模型课上看到 finder。实现如下

    public static Finder<Long, Measurement> find = new Finder<Long, Measurement>(Long.class, Measurement.class);
    

    那就试试吧。 ebean 也有很多问题,您可以使用原始查询来删除,如下所示

    SqlUpdate deletObject = Ebean
                            .createSqlUpdate("delete from table_name where id = "
                                    + id); 
    deletObject.execute();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多