Hibernate встроенный ключ с другой сущностью и полем - PullRequest
0 голосов
/ 30 марта 2012

Я не могу найти конкретный и ПОЛНЫЙ пример того, что я ищу.

В моем случае у меня есть сущности "Game" и "GameCodeColor".ПК игры является int Id.PK GameCodeColor должен быть «Game» и целым числом.Но в строке:

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

Ответы [ 2 ]

0 голосов
/ 31 марта 2012

Проблема решена.

По-видимому, я не должен был одновременно использовать @IdClass и @EmbeddedId.

0 голосов
/ 30 марта 2012

Обычно атрибуты должны быть в нижнем регистре:

protected Game game

А вам не хватает геттеров и сеттеров игры и gameCodeColorIndex

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...