我在将外键映射为主键时遇到问题。
我的桌子是:
client:
PK: id_client
games:
PK: id_game
tickets:
PK: (id_game_fk references game(id_game), id_client_fk references client(id_client))
下面是我定义为实体的类:
客户爪哇:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_client")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
游戏。爪哇:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "games")
public class Games {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_game")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
票爪哇:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import Client;
import Games;
@Entity
@Table(name = "tickets")
public class Ticket implements Serializable {
private static final long serialVersionUID = 3287868602749718327L;
@EmbeddedId
private TicketId ticketId;
@ManyToOne
@JoinColumn(name = "id_game")
private Games games;
@ManyToOne
@JoinColumn(name = "id_client")
private Client client;
public TicketId getId() {
return ticketId;
}
public void setId(TicketId id) {
this.ticketId = id;
}
public Games getGames() {
return games;
}
public void setGames(Games games) {
this.games = games;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
}
票d.java:
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class TicketId implements Serializable {
private static final long serialVersionUID = 6220676431741410239L;
private int idGameFk;
private int idClientFk;
public TicketId(int idGameFk, int idClientFk) {
this.idGameFk = idGameFk;
this.idClientFk = idClientFk;
}
public int getIdGameFk() {
return idGameFk;
}
public void setIdGameFk(int idGameFk) {
this.idGameFk = idGameFk;
}
public int getIdClientFk() {
return idClientFk;
}
public void setIdClientFk(int idClientFk) {
this.idClientFk = idClientFk;
}
}
到目前为止,我已经尝试了所有的建议,但没有一个有用。另外,我需要这个PK由外键组成,所以我真的需要帮助解决,我应该如何正确地映射它。
您可以使用@MapsId:
@Entity
@Table(name = "tickets")
public class Ticket implements Serializable {
private static final long serialVersionUID = 3287868602749718327L;
@EmbeddedId
private TicketId ticketId;
@ManyToOne
@MapsId("idGameFk")
@JoinColumn(name = "id_game_fk")
private Games games;
@ManyToOne
@MapsId("idClientFk")
@JoinColumn(name = "id_client_fk")
private Client client;
....
}
更多信息请点击此处:http://docs.oracle.com/javaee/6/api/javax/persistence/MapsId.html
我在jpa/Hibernate中映射复合键时遇到了问题。父实体和子实体都具有复合主键。 在运行时保存它时会出现以下异常: 我认为这是虚假的,因为有getter和setter。如果在priceRequestLegModel上使用mappedby=“leg”,在allocationModel上使用@mapsid,也会出现同样的错误。有人能指出我在这里做错了什么吗?
假设您有一个表,如下所示: 可以看到列是表的主键和外键。是的,MySQL成功生成了这个表。 问题很简单,我将如何在JPA实体中映射这一点?我是否应该有映射到列的1个id和连接列的另一个字段?欢迎提出建议。
我有两个表:A和B,都有一个复合主键。表B的PK也是表a主键的外键。 当我试图获取映射表B的类的实例时,我得到了以下异常: org.hibernate.TypeMismatchException 这两个类都位于package:com.cairone.ejemple01.entities 完整的日志输出为: 2016-09-21 12:28:24.505错误8568--[main] O.S.Boot
问题内容: 我正在尝试建立一个PostgreSQL表,该表具有两个指向另一个表中相同主键的外键。 运行脚本时出现错误 sqlalchemy.exc.AmbiguousForeignKeysError:无法确定关系Company.stakeholder的父/子表之间的联接条件- 有多个链接表的外键路径。指定“ foreign_keys”参数,提供这些列的列表,这些列应被视为包含对父表的外键引用。 那
我有2个表,即user和user_session。
我正在尝试我们非主键作为外键在我的应用程序。场景如下:我有EMPLOYEE和EMPLOYEE_PROPERTIES表。Employee和Employee属性之间存在一对多的关系。下面是我的架构: 下面是我的hibernate映射XML:------------------------------- -------------员工属性------------------- 是否可以引用非主键作为外键