使用另一个实体和一个字段休眠嵌入的键



我找不到一个具体完整的例子来说明我正在寻找什么。

在我的情况下,我有实体"Game"one_answers"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;
    }
}

通常属性应该是小写的:

protected Game game

并且您错过了game和gameCodeColorIndex 的getter和setter

问题已解决。

显然,我不应该同时使用@IdClass和@EmbeddedId。

最新更新