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,我做错了吗?
答案 0 :(得分:0)
与@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>();