獲取空約束,許多冬眠
org.postgresql.util.PSQLException: ERROR: null value in column "tournament_id" violates not-null constraint`
Tournament.java
@Data
@Entity
public class Tournament {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@OneToMany(mappedBy = "tournament", cascade = CascadeType.PERSIST)
private List<Group> groups;}
組
@Entity
@Data
@Table(schema = "offan", name = "groups")
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JoinColumn(name = "tournament_id")
private Tournament tournament;
public Group(){}
public Group(Tournament tournament){
this.tournament = tournament;
}
}
我想節省一氣呵成。與CrudRepository
。只保存沒有任何組的錦標賽實例就可以正常工作。我看不出爲什麼組沒有插入tournament_id鍵正確
我正在使用lombok獲取和setter。
DDL:
CREATE TABLE tournaments (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE groups (
id SERIAL PRIMARY KEY,
name VARCHAR(50) ,
tournament_id INTEGER REFERENCES tournaments (id) NOT NULL ,
UNIQUE (name, tournament_id)
);
可能出現的問題
我使用Spring @RequestBody
將解析我的對象到比賽對象,這可能設置不正確映射?
...利用底層傑克遜庫沒有樹立正確的映射解析對象的問題
。手動執行所有操作都已正確插入。
@RequestMapping(value = "/tournaments", method = RequestMethod.POST)
public Tournament createTournament(@RequestBody Tournament tournament){
//Will not work
//Tournament savedEntry = tournamentRepository.save(tournament);
//
//Setting properties manually works...
Tournament t = new Tournament();
t.setName(tournament.getName());
Group group = new Group();
group.setTournament(t);
t.getGroups().add(group)
// Both tournament and group are inserted
t = tournamentRepository.save(t);
return t; //Overflow here because of jackson another thing to fix :)
}
您可以添加DDL腳本嗎? – Rocherlee
@Rocherlee當然! – skyw00lker
你最後的編輯就是我的回答! :P :) –