2012-01-11 62 views
0

我試圖在這裏如何在Play框架中使用複合鍵?

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

} 

我需要什麼樣的修改做什麼呢?

回答

2

當延長GenericModel你需要重寫_key方法

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