如何在Play框架中使用复合键?

时间:2012-01-11 10:38:41

标签: playframework

我试图按照这里的说明进行操作

http://docs.oracle.com/cd/B31017_01/web.1013/b28221/cmp30cfg001.htm

随着使用通用模型。我最终得到了一个主键类,如下所示

import javax.persistence.Embeddable;
import java.io.Serializable;

@Embeddable
public class DailyPK implements Serializable {

    private int match_id;
    private int user_id;

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public int getMatch_id() {
        return match_id;
    }

    public void setMatch_id(int match_id) {
        this.match_id = match_id;
    }

    public int hashCode() {
        return match_id * 1000000 + user_id;
    }

    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (!(obj instanceof DailyPK)) return false;
        if (obj == null) return false;
        DailyPK pk = (DailyPK) obj;
        return pk.match_id == match_id && pk.user_id == user_id;
    }
}

我的模型类如下,不编译

public class Daily extends GenericModel {

    @Id
    DailyPK primaryKey;


    public int round;
    public int score;


    public Daily(int round, int score) {
        this.round = round;
        this.score = score;
    }

    @EmbeddedId
    public DailyPK getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(DailyPK pk) {
        primaryKey = pk;
    }

}

我需要做出哪些修改?

1 个答案:

答案 0 :(得分:2)

扩展GenericModel时,需要覆盖_key方法

@Override
public Object _key() {
    return getPrimaryKey();
}