简体   繁体   English

空对一对多休眠的约束

[英]Null constraint on One to many hibernate

Getting 入门

org.postgresql.util.PSQLException: ERROR: null value in column "tournament_id" violates not-null constraint`

Tournament.java 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;}

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;
    }
}

I'm trying to save both in one go. 我正努力一举两得。 with CrudRepository . CrudRepository Saving just an tournament instance without any groups works fine. 仅保存没有任何组的锦标赛实例就可以了。 I cannot see why groups aren't inserting tournament_id key correctly 我看不到为什么组没有正确插入Tournament_id

I'm using lombok for getters and setters. 我正在使用龙目岛吸气剂和二传手。

DDL: 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)
);

Possible problem 可能的问题

I use Spring @RequestBody which will parse my object into Tournament object, this might not set the mapping correctly? 我使用Spring @RequestBody会将我的对象解析为Tournament对象,这可能无法正确设置映射?

...Was the problem ...是问题所在

Parsing the object using the underlying jackson library did not set up the correct mapping. 使用基础的杰克逊库解析对象未设置正确的映射。 Doing it manually everything was inserted correctly. 手动执行所有操作均正确插入。

@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 :)
}

I guess you will have to save Tournament entity separately and then set that Tournament in the corresponding Group entity and then save that Group entity separately. 我猜您将必须分别保存Tournament实体,然后在相应的Group实体中设置该Tournament ,然后分别保存该Group实体。 (In either case of spring saving group entities or you saving the group entity by mapping corresponding Tournament , the number of insert statement remain the same.) (在弹簧保存组实体的情况下,或者通过映射相应的Tournament保存组实体,insert语句的数量都保持不变。)

What's happening here is you are expecting Spring to map each Tournament to Group by calling setters of Group , which is not happening. 这里发生的事情是您期望Spring通过调用Group的设置者将每个Tournament映射到Group ,但是这没有发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM