【问题标题】:In hibernate how to map two foreign keys in one table to one primary key in another table在休眠中如何将一个表中的两个外键映射到另一个表中的一个主键
【发布时间】:2016-12-23 13:53:50
【问题描述】:

ER图如下 ER diagram

我创建了如下实体类:

@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

================================================ ===============

但它给了我以下错误:

初始化数据库连接时出错。 org.hibernate.AnnotationException: mappedBy 引用了一个未知的目标实体属性:com.test.entity.StateEntity.currentStateFlows 中的 com.test.orm.entity.StateEntity.currentState

我是hibernate的新手,我做错了什么?

【问题讨论】:

  • 您确定要在此处的StateEntity 上使用targetEntity 吗?集合类型 StateEntity 不公开 nextStatecurrentState 的命名属性,因此您可能会同时收到此错误。

标签: hibernate


【解决方案1】:

就像@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>();

【讨论】:

  • 谢谢,删除 targetEntity 消除了错误
猜你喜欢
  • 1970-01-01
  • 2013-12-09
  • 2015-12-18
  • 1970-01-01
  • 2011-08-10
  • 2018-05-05
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
相关资源
最近更新 更多