我在Spring中使用了JPA的Hibernate实现。
Class Country{
@OneToMany(mappedBy="Country", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
List<State> stateList;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "Current_State_ID")
State currnetState;
}
Class State{
@ManyToOne
@JoinColumn(name="Country_ID")
private Country country;
}
State stateObj = new State();
country.getStateList().add(stateObj);
country.setCurrnetState(stateObj);
countryRepository.saveAndFlush(country);
countryRepository是一个JPA Repository Implemenntation。 这会在状态表中创建2个条目,这会弄乱我的逻辑。有人可以指出我在做什么。
答案 0 :(得分:0)
我不知道为什么,但是将我的代码更改为以下内容可以帮助我。
List<State> stateList = new ArrayList<State>();
stateList.add(state);
country.setStateList(stateList);
创建新列表实例并将其设置为国家/地区。