Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 休眠:错误未知列';foo';在';字段列表';-从一个实体到同一父实体的两个@manytone关系_Java_Mysql_Hibernate - Fatal编程技术网

Java 休眠:错误未知列';foo';在';字段列表';-从一个实体到同一父实体的两个@manytone关系

Java 休眠:错误未知列';foo';在';字段列表';-从一个实体到同一父实体的两个@manytone关系,java,mysql,hibernate,Java,Mysql,Hibernate,我正在使用Hibernate 5.2.10持久化MySQL数据库 儿童桌游戏有两个特点: 首先,它有一个由4列组成的复合主键,即leagueId,这也是联赛的外键,seasenid,这也是赛季的外键,roundNo,gameNo 其次,表的两个外键指向同一个表,即指向Club.id:homeTeamId和awayTeamId 我试图查询俱乐部实体,但我一直收到相同的错误消息 查询: List<Game> gameList = session.createQuery("FROM Gam

我正在使用Hibernate 5.2.10持久化MySQL数据库

儿童桌游戏有两个特点:

首先,它有一个由4列组成的复合主键,即leagueId,这也是联赛的外键,seasenid,这也是赛季的外键,roundNogameNo

其次,表的两个外键指向同一个表,即指向Club.id:homeTeamIdawayTeamId

我试图查询俱乐部实体,但我一直收到相同的错误消息

查询:

List<Game> gameList = session.createQuery("FROM Game g WHERE g.id.leagueId = 1L  AND g.id.seasonId = 1L AND g.id.roundNo = 35 AND g.id.gameNo = 7")
    .getResultList();
配子ID类:

@Embeddable
public class GameId implements Serializable{

    private static final long serialVersionUID = 1L;
    private Long leagueId;
    private Long seasonId;
    private Integer roundNo;
    private Integer gameNo;

    public GameId() {
    }

    //getters and setters omitted for brevity

    @Override
    public int hashCode() {
        // code omitted for brevity
    }

    @Override
    public boolean equals(Object obj) {
        //code omitted for brevity
    }
}
@Entity
@Table(name = "Club")
public class Club implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String clubName;

    private League league;

    public Club() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    //unannotated getters and setters omitted for brevity

    @ManyToOne
    @JoinColumn(name = "currentLeagueId")
    public League getLeague() {
        return league;
    }
}
@Entity
public class League implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private String leagueName;
    private String descr;

    public League() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    //unannotated getters and setters omitted for brevity
}
俱乐部课程:

@Embeddable
public class GameId implements Serializable{

    private static final long serialVersionUID = 1L;
    private Long leagueId;
    private Long seasonId;
    private Integer roundNo;
    private Integer gameNo;

    public GameId() {
    }

    //getters and setters omitted for brevity

    @Override
    public int hashCode() {
        // code omitted for brevity
    }

    @Override
    public boolean equals(Object obj) {
        //code omitted for brevity
    }
}
@Entity
@Table(name = "Club")
public class Club implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String clubName;

    private League league;

    public Club() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    //unannotated getters and setters omitted for brevity

    @ManyToOne
    @JoinColumn(name = "currentLeagueId")
    public League getLeague() {
        return league;
    }
}
@Entity
public class League implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private String leagueName;
    private String descr;

    public League() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    //unannotated getters and setters omitted for brevity
}
联盟课程:

@Embeddable
public class GameId implements Serializable{

    private static final long serialVersionUID = 1L;
    private Long leagueId;
    private Long seasonId;
    private Integer roundNo;
    private Integer gameNo;

    public GameId() {
    }

    //getters and setters omitted for brevity

    @Override
    public int hashCode() {
        // code omitted for brevity
    }

    @Override
    public boolean equals(Object obj) {
        //code omitted for brevity
    }
}
@Entity
@Table(name = "Club")
public class Club implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String clubName;

    private League league;

    public Club() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    //unannotated getters and setters omitted for brevity

    @ManyToOne
    @JoinColumn(name = "currentLeagueId")
    public League getLeague() {
        return league;
    }
}
@Entity
public class League implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private String leagueName;
    private String descr;

    public League() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    //unannotated getters and setters omitted for brevity
}
季节课程:

@Entity
@Table(name="season")
public class Season implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private Integer yearBegin;
    private Integer yearEnd;
    private String descr;

    public Season(){
    }

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    //unannotated getters and setters omitted for brevity
}

这里显示的表是整个数据库的一个子集,它由7个表组成,但我已经针对所有其他6个实体测试了查询,它们工作得很顺利。问题一定出在游戏实体中,但我不知道它出了什么问题。有人能解释一下这个问题吗?提前非常感谢所有愿意帮忙的人

我找到了解决办法。这似乎很疯狂,但我所需要做的就是将@EmbeddedId注释从字段移动到游戏类中的getter

现在游戏类是:

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

    private static final long serialVersionUID = 1L;
    private GameId id;
    @Temporal(TemporalType.DATE)
    private Date date;
    private Boolean postponed;
    private Boolean played;
    private String gameDescr;

    private League league;
    private Season season;
    private Club clubHome;
    private Club clubAway;

    public Game() {
    }

    @EmbeddedId
    public GameId getId() {
        return id;
    }
    //everything else stays the same

游戏表中的club表缺少外键实际上,游戏实体中的getClubHome()和GetClubWay()方法都有一个指向club实体的@ManyToOne注释。请查看ER模型。你需要作为外键导入我真的听不懂你说的。您将如何准确地修复代码?