2012-03-30 6 views
0

Ich kann nicht finden, ein konkretes und vollständiges Beispiel von dem, was ich suche.Hibernate eingebetteten Schlüssel mit einer anderen Entität und einem Feld

In meinem Fall habe ich die Entitäten "Game" und "GameCodeColor". Die PK des Spiels ist eine Int Id. Die PK von GameCodeColor sollte ein "Spiel" und eine ganze Zahl sein. Aber in Zeile:

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

Ich erhalte:

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

Hier sind die Codes Ich habe bisher: 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; 
    } 
} 

Spiel

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

Antwort

0

Problem gelöst.

Anscheinend hätte ich @ IdClass und @EmbeddedId nicht gleichzeitig verwenden sollen.

0

Normalerweise sollten die Attribute in Kleinbuchstaben geschrieben werden:

protected Game game 

Und Sie vermissen die Getter und Setter von Spiel und gameCodeColorIndex

+0

Nun, Sie hatten Recht, aber es hat es nicht gelöst. Das gleiche Problem noch einmal. – TTT

Verwandte Themen