2016-12-23 5 views
1

ER図は次のとおりです。で別のテーブル内の1つの主キーに一つのテーブルに2つの外部キーをマップする方法休止状態

@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 

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

が、そのエラーの下に私を与える:データベース接続をintializingで

エラー。 org.hibernate.AnnotationException:mappedByは、未知のターゲットエンティティプロパティを参照:私はやっているところcom.test.entity.StateEntity.currentStateFlows

でcom.test.orm.entity.StateEntity.currentState

私は、休止状態に新しいです違う?

+1

ここで 'StateEntity'にtargetEntityを使用してもよろしいですか?コレクション型 'StateEntity'は' nextState'と 'currentState'の名前付き属性を公開しませんので、両方の場合にこのエラーが発生する可能性があります。 – Naros

答えて

0

上記の@Narosと同様に、targetEntityは「StateFlowEntity.class」である必要があります。まったく指定する必要はありません。あなたが明示的に言及したので

@OneToMany(cascade = CascadeType.ALL, mappedBy = "currentState", 
    targetEntity = StateEntity.class) 

「targetEntity = StateEntity.class」あなたのコードでは、それが「StateEntity」クラスの「currentStateの」フィールドを探しています。それがエラーメッセージの意味です。

これらの変更を加えた後、これらのエンティティをSpringブートで読み込むことができました。

@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>(); 
+0

ありがとうございます.targetEntityを削除するとエラーが解消されます – tina

関連する問題