2012-03-30 50 views
0

我找不到具體和完整的示例我正在尋找。休眠嵌入式密鑰與另一個實體和字段

在我的情況下,我有實體'遊戲'和'GameCodeColor'。 遊戲的PK是一個int ID。 GameCodeColor的PK應該是一個'遊戲'和一個整數。但在行:

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 

我不斷收到:

"Unable to find properties (Game, GameCodeColorIndex) in entity annotated with @IdClass:h.GameCodeColor" 

這裏是代碼我到目前爲止: GameCodeColorPK

package h; 

import javax.persistence.*; 
import java.io.*; 

@Embeddable 
public class GameCodeColorPK implements Serializable { 

    @OneToOne 
    @JoinColumn(name = "gc_g_Id", referencedColumnName = "g_Id") 
    protected Game Game; 
    @Basic 
    @Column(name = "gc_Index") 
    protected int GameCodeColorIndex; 

    public GameCodeColorPK() { 
    } 

    public GameCodeColorPK(Game Game, int GameCodeColorIndex) { 
     this.Game = Game; 
     this.GameCodeColorIndex = GameCodeColorIndex; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final GameCodeColorPK other = (GameCodeColorPK) obj; 
     if (this.Game != other.Game && (this.Game == null || !this.Game.equals(other.Game))) { 
      return false; 
     } 
     if (this.GameCodeColorIndex != other.GameCodeColorIndex) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 7; 
     hash = 71 * hash + (this.Game != null ? this.Game.hashCode() : 0); 
     hash = 71 * hash + this.GameCodeColorIndex; 
     return hash; 
    } 
} 

GameCodeColor

package h; 

import javax.persistence.*; 
import java.io.*; 

@Entity 
@Table(name = "GameCodeColors") 
@IdClass(GameCodeColorPK.class) 
public class GameCodeColor implements Serializable { 

    @EmbeddedId 
    private GameCodeColorPK GameCodeColorPK; 

    public GameCodeColor() { 
    } 

    public h.GameCodeColorPK getGameCodeColorPK() { 
     return GameCodeColorPK; 
    } 

    public void setGameCodeColorPK(h.GameCodeColorPK GameCodeColorPK) { 
     this.GameCodeColorPK = GameCodeColorPK; 
    } 
    @Basic 
    @Column(name = "gcode_gc_Index") 
    private int GameColorIndex; 

    public int getGameColorIndex() { 
     return GameColorIndex; 
    } 

    public void setGameColorIndex(int GameColorIndex) { 
     this.GameColorIndex = GameColorIndex; 
    } 

    public GameCodeColor(h.GameCodeColorPK GameCodeColorPK, int GameColorIndex) { 
     this.GameCodeColorPK = GameCodeColorPK; 
     this.GameColorIndex = GameColorIndex; 
    } 
} 

遊戲

package h; 

import java.io.*; 
import javax.persistence.*; 
import java.util.*; 

@Entity 
@Table(name = "Games") 
public class Game implements Serializable { 

    public Game() { 
    } 

    public Set<GameCodeColor> getGameCodeColors() { 
     return GameCodeColors; 
    } 

    public void setGameCodeColors(Set<GameCodeColor> GameCodeColors) { 
     this.GameCodeColors = GameCodeColors; 
    } 

    public Set<GameColor> getGameColors() { 
     return GameColors; 
    } 

    public void setGameColors(Set<GameColor> GameColors) { 
     this.GameColors = GameColors; 
    } 

    public Set<GameTurn> getGameTurns() { 
     return GameTurns; 
    } 

    public void setGameTurns(Set<GameTurn> GameTurns) { 
     this.GameTurns = GameTurns; 
    } 

    public Game(h.User User, Date StartTime, Date EndTime, int TurnsPlayed, int TurnsToBePlayed, int CodeLength, int AvailableColoursCount, int ResetCount, boolean DoubleAllowed, int GameResult_Id) { 
     this.User = User; 
     this.StartTime = StartTime; 
     this.EndTime = EndTime; 
     this.TurnsPlayed = TurnsPlayed; 
     this.TurnsToBePlayed = TurnsToBePlayed; 
     this.CodeLength = CodeLength; 
     this.AvailableColoursCount = AvailableColoursCount; 
     this.ResetCount = ResetCount; 
     this.DoubleAllowed = DoubleAllowed; 
     this.GameResult_Id = GameResult_Id; 
    } 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "g_Id") 
    private int Id; 

    public int getId() { 
     return Id; 
    } 
    @ManyToOne 
    @JoinColumn(name = "g_u_Id") 
    private User User; 

    public h.User getUser() { 
     return User; 
    } 

    public void setUser(h.User User) { 
     this.User = User; 
    } 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "g_StartTime") 
    private java.util.Date StartTime; 

    public java.util.Date getStartTime() { 
     return StartTime; 
    } 

    public void setStartTime(java.util.Date StartTime) { 
     this.StartTime = StartTime; 
    } 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "g_EndTime") 
    private java.util.Date EndTime; 

    public java.util.Date getEndTime() { 
     return EndTime; 
    } 

    public void setEndTime(java.util.Date EndTime) { 
     this.EndTime = EndTime; 
    } 
    @Basic 
    @Column(name = "g_TurnsPlayed") 
    private int TurnsPlayed; 
    @Basic 
    @Column(name = "g_TurnsToBePlayed") 
    private int TurnsToBePlayed; 
    @Basic 
    @Column(name = "g_CodeLength") 
    private int CodeLength; 
    @Basic 
    @Column(name = "g_AvailableColoursCount") 
    private int AvailableColoursCount; 
    @Basic 
    @Column(name = "g_ResetCount") 
    private int ResetCount; 
    @Basic 
    @Column(name = "g_doubleAllowed") 
    private boolean DoubleAllowed; 
    @Basic 
    @Column(name = "g_gr_Id") 
    private int GameResult_Id; 

    public int getAvailableColoursCount() { 
     return AvailableColoursCount; 
    } 

    public void setAvailableColoursCount(int AvailableColoursCount) { 
     this.AvailableColoursCount = AvailableColoursCount; 
    } 

    public int getCodeLength() { 
     return CodeLength; 
    } 

    public void setCodeLength(int CodeLength) { 
     this.CodeLength = CodeLength; 
    } 

    public boolean isDoubleAllowed() { 
     return DoubleAllowed; 
    } 

    public void setDoubleAllowed(boolean DoubleAllowed) { 
     this.DoubleAllowed = DoubleAllowed; 
    } 

    public int isGameResult_Id() { 
     return GameResult_Id; 
    } 

    public void setGameResult_Id(int GameResult_Id) { 
     this.GameResult_Id = GameResult_Id; 
    } 

    public int getResetCount() { 
     return ResetCount; 
    } 

    public void setResetCount(int ResetCount) { 
     this.ResetCount = ResetCount; 
    } 

    public int getTurnsPlayed() { 
     return TurnsPlayed; 
    } 

    public void setTurnsPlayed(int TurnsPlayed) { 
     this.TurnsPlayed = TurnsPlayed; 
    } 

    public int getTurnsToBePlayed() { 
     return TurnsToBePlayed; 
    } 

    public void setTurnsToBePlayed(int TurnsToBePlayed) { 
     this.TurnsToBePlayed = TurnsToBePlayed; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Game other = (Game) obj; 
     if (this.Id != other.Id) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 5; 
     hash = 37 * hash + this.Id; 
     return hash; 
    } 
    @OneToMany(mappedBy = "GameTurn") 
    private Set<GameTurn> GameTurns = new HashSet<GameTurn>(); 

    public GameTurn addNewGameTurn(java.util.Date DateTime, int Correct, int Misplaced, boolean IsWon) { 
     GameTurn result = new GameTurn(
       this, 
       this.GameTurns != null ? this.GameTurns.size() : 0, 
       DateTime, 
       Correct, 
       Misplaced, 
       IsWon); 

     this.GameTurns.add(result); 
     return result; 
    } 
    @OneToMany(mappedBy = "GameColor") 
    private Set<GameColor> GameColors = new HashSet<GameColor>(); 

    public void addNewGameColor(int RGBvalue) { 
     GameColor gc = new GameColor(); 
     gc.setGame(this); 
     gc.setGameColorIndex(this.GameColors != null ? this.GameColors.size() : 0); 
     gc.setRGBvalue(RGBvalue); 
     this.GameColors.add(gc); 
    } 
    @OneToMany(mappedBy = "GameCodeColor") 
    private Set<GameCodeColor> GameCodeColors = new HashSet<GameCodeColor>(); 

    public void addGameCodeColor(int GameColorIndex) { 
//   this.GameCodeColors.add(new GameCodeColor(
//     this, 
//     this.GameCodeColors != null ? this.GameCodeColors.size() : 0, 
//     GameColorIndex)); 
    } 

    public int getGameResult_Id() { 
     return GameResult_Id; 
    } 

    public void setId(int Id) { 
     this.Id = Id; 
    } 

    public Game(int Id, h.User User, Date StartTime, Date EndTime, int TurnsPlayed, int TurnsToBePlayed, int CodeLength, int AvailableColoursCount, int ResetCount, boolean DoubleAllowed, int GameResult_Id) { 
     this.Id = Id; 
     this.User = User; 
     this.StartTime = StartTime; 
     this.EndTime = EndTime; 
     this.TurnsPlayed = TurnsPlayed; 
     this.TurnsToBePlayed = TurnsToBePlayed; 
     this.CodeLength = CodeLength; 
     this.AvailableColoursCount = AvailableColoursCount; 
     this.ResetCount = ResetCount; 
     this.DoubleAllowed = DoubleAllowed; 
     this.GameResult_Id = GameResult_Id; 
    } 
} 

回答

0

問題。

顯然,我不應該同時使用@IdClass和@EmbeddedId。

0

通常的屬性應爲小寫:

protected Game game 

而且你缺少解決了getter和遊戲的制定者和gameCodeColorIndex

+0

那麼你是對的,但它並沒有解決它。同樣的問題。 – TTT 2012-03-30 12:21:46