在hibernate中,如何将一个表中的两个外键映射到另一个表中的一个主键
ER图如下所示 我创建了实体类,如下所示:在hibernate中,如何将一个表中的两个外键映射到另一个表中的一个主键,hibernate,Hibernate,ER图如下所示 我创建了实体类,如下所示: @Entity @Table(name = "state_flows") public class StateFlowEntity { private int id; private StateMachineEntity stateMachine; private StateEntity currentState; private StateEntity nextState; @ManyToOne @JoinColumn(name = "curr
@Entity
@Table(name = "state_flows")
public class StateFlowEntity {
private int id;
private StateMachineEntity stateMachine;
private StateEntity currentState;
private StateEntity nextState;
@ManyToOne
@JoinColumn(name = "current_state", referencedColumnName = "id",
insertable = false, updatable = false)
public StateEntity getCurrentState() {
return currentState;
}
@ManyToOne
@JoinColumn(name = "next_state", referencedColumnName = "id",
insertable = false, updatable = false)
public StateEntity getNextState() {
return nextState;
}
@ManyToOne
@JoinColumn(name = "machine_id", referencedColumnName = "id",
insertable = false, updatable = false)
public StateMachineEntity getStateMachine() {
return stateMachine;
}
//setters and getters
===================================================================
@Entity
@Table(name = "states")
public class StateEntity {
public enum NodeType {
EVENT
};
private int id;
private String name;
private NodeType nodeType;
private String nodeId;
private int ratio;
private int missingRatio;
private String nodeDetail;
private Set<StateFlowEntity> nextStateFlows = new HashSet<StateFlowEntity>();
private Set<StateFlowEntity> currentStateFlows = new HashSet<StateFlowEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "nextState",
targetEntity = StateEntity.class)
public Set<StateFlowEntity> getNextStateFlows() {
return nextStateFlows;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "currentState",
targetEntity = StateEntity.class)
public Set<StateFlowEntity> getCurrentStateFlows() {
return currentStateFlows;
}
@Column(name = "node_type", nullable = false,
columnDefinition = "ENUM('EVENT') default 'EVENT'")
@Enumerated(EnumType.STRING)
private NodeType getNodeType() {
return nodeType;
}
//setter and getters
@Entity
@Table(name = "state_machines")
public class StateMachineEntity {
private int id;
private String name;
private String description;
private int initialState;
private int combinational;
private Set<StateFlowEntity> machineId = new HashSet<StateFlowEntity>();
private Set<InstanceEntity> instances = new HashSet<InstanceEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "stateMachine")
public Set<StateFlowEntity> getMachineId() {
return machineId;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "stateMachine")
public Set<InstanceEntity> getInstances() {
return instances;
}
//setters and getters
@实体
@表(name=“states”)
公共类国家实体{
公共枚举节点类型{
事件
};
私有int-id;
私有字符串名称;
私有节点类型节点类型;
私有字符串nodeId;
私人投资比率;
私人信息丢失率;
私有字符串nodeDetail;
private Set nextStateFlows=new HashSet();
私有集currentStateFlows=new HashSet();
@OneToMany(cascade=CascadeType.ALL,mappedBy=“nextState”,
targetEntity=StateEntity.class)
公共集getNextStateFlows(){
返回下一个状态流;
}
@OneToMany(cascade=CascadeType.ALL,mappedBy=“currentState”,
targetEntity=StateEntity.class)
公共集getCurrentStateFlows(){
回流;
}
@列(name=“node\u type”,null=false,
columnDefinition=“枚举(“事件”)默认值“事件”)
@枚举(EnumType.STRING)
私有节点类型getNodeType(){
返回节点类型;
}
//二传手和接球手
======================================================================================
@Entity
@Table(name = "states")
public class StateEntity {
public enum NodeType {
EVENT
};
private int id;
private String name;
private NodeType nodeType;
private String nodeId;
private int ratio;
private int missingRatio;
private String nodeDetail;
private Set<StateFlowEntity> nextStateFlows = new HashSet<StateFlowEntity>();
private Set<StateFlowEntity> currentStateFlows = new HashSet<StateFlowEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "nextState",
targetEntity = StateEntity.class)
public Set<StateFlowEntity> getNextStateFlows() {
return nextStateFlows;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "currentState",
targetEntity = StateEntity.class)
public Set<StateFlowEntity> getCurrentStateFlows() {
return currentStateFlows;
}
@Column(name = "node_type", nullable = false,
columnDefinition = "ENUM('EVENT') default 'EVENT'")
@Enumerated(EnumType.STRING)
private NodeType getNodeType() {
return nodeType;
}
//setter and getters
@Entity
@Table(name = "state_machines")
public class StateMachineEntity {
private int id;
private String name;
private String description;
private int initialState;
private int combinational;
private Set<StateFlowEntity> machineId = new HashSet<StateFlowEntity>();
private Set<InstanceEntity> instances = new HashSet<InstanceEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "stateMachine")
public Set<StateFlowEntity> getMachineId() {
return machineId;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "stateMachine")
public Set<InstanceEntity> getInstances() {
return instances;
}
//setters and getters
@实体
@表(name=“state\u machines”)
公共阶级国家机器{
私有int-id;
私有字符串名称;
私有字符串描述;
私有国家;
私有int组合;
私有集machineId=新HashSet();
私有集实例=新HashSet();
@OneToMany(cascade=CascadeType.ALL,mappedBy=“stateMachine”)
公共集getMachineId(){
返回机器ID;
}
@OneToMany(cascade=CascadeType.ALL,mappedBy=“stateMachine”)
公共集getInstances(){
返回实例;
}
//二传手和接球手
=============================================================
@Entity
@Table(name = "states")
public class StateEntity {
public enum NodeType {
EVENT
};
private int id;
private String name;
private NodeType nodeType;
private String nodeId;
private int ratio;
private int missingRatio;
private String nodeDetail;
private Set<StateFlowEntity> nextStateFlows = new HashSet<StateFlowEntity>();
private Set<StateFlowEntity> currentStateFlows = new HashSet<StateFlowEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "nextState",
targetEntity = StateEntity.class)
public Set<StateFlowEntity> getNextStateFlows() {
return nextStateFlows;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "currentState",
targetEntity = StateEntity.class)
public Set<StateFlowEntity> getCurrentStateFlows() {
return currentStateFlows;
}
@Column(name = "node_type", nullable = false,
columnDefinition = "ENUM('EVENT') default 'EVENT'")
@Enumerated(EnumType.STRING)
private NodeType getNodeType() {
return nodeType;
}
//setter and getters
@Entity
@Table(name = "state_machines")
public class StateMachineEntity {
private int id;
private String name;
private String description;
private int initialState;
private int combinational;
private Set<StateFlowEntity> machineId = new HashSet<StateFlowEntity>();
private Set<InstanceEntity> instances = new HashSet<InstanceEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "stateMachine")
public Set<StateFlowEntity> getMachineId() {
return machineId;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "stateMachine")
public Set<InstanceEntity> getInstances() {
return instances;
}
//setters and getters
但它给了我以下的错误:
初始化数据库连接时出错。
org.hibernate.AnnotationException:mappedBy引用com.test.entity.StateEntity.currentStateFlows中的未知目标实体属性:com.test.orm.entity.StateEntity.currentState
我不熟悉hibernate,在那里我做错了?就像@Naros提到的那样,targetEntity应该是“StateFlowEntity.class”,或者根本不需要指定
@OneToMany(cascade = CascadeType.ALL, mappedBy = "currentState",
targetEntity = StateEntity.class)
由于您在代码中明确提到了“targetEntity=StateEntity.class”,它正在“StateEntity”类中查找“currentState”字段。这就是错误消息的含义
在做了这些更改之后,我能够用Spring Boot加载这些实体
@OneToMany(cascade = CascadeType.ALL, mappedBy = "nextState",
targetEntity = StateFlowEntity.class)
private Set<StateFlowEntity> nextStateFlows = new HashSet<StateFlowEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "currentState" )
private Set<StateFlowEntity> currentStateFlows = new HashSet<StateFlowEntity>();
@OneToMany(cascade=CascadeType.ALL,mappedBy=“nextState”,
targetEntity=StateFlowEntity.class)
private Set nextStateFlows=new HashSet();
@OneToMany(cascade=CascadeType.ALL,mappedBy=“currentState”)
私有集currentStateFlows=new HashSet();
是否确实要在此处使用状态实体
上的targetEntity?集合类型状态实体
不公开下一状态
和当前状态
的命名属性,因此这两个属性都可能出现此错误。谢谢,删除targetEntity可消除此错误