deltanovember
deltanovember

Reputation: 44051

How can I use a compound key with the Play framework?

I have tried to follow the instructions here

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

Along with using generic model. I end up with a primary key class as follows

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;
    }
}

My model class is as follows which does not compile

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;
    }

}

What modifications do I need to make?

Upvotes: 0

Views: 617

Answers (1)

Seb Cesbron
Seb Cesbron

Reputation: 3833

When extending GenericModel you need to Override _key method

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

Upvotes: 2

Related Questions