Spring Boot Data Rest POST返回204但仅返回SELECTS

时间:2016-03-10 08:08:44

标签: spring hibernate spring-boot spring-data-rest

所以在切换到Boot之前这个工作正常。基本上我能够将text / uri-list发布到@OneToMany资源就好了。我将项目切换为使用Boot,并在此过程中停止工作。我可以在@ManyToOne结尾处输入一个text / uri-list,但这不是我想做的。

当我提交POST时,我得到204响应,但我只能在我的控制台上看到SQL选择而不是插入任何内容。 编辑:我使用的是Postman,但是这里有一个curl命令可以执行/返回相同的

curl -v -X POST -H "Content-Type: text/uri-list" -d "http://localhost:8080/games/2" http://localhost:8080/developers/1/gameList

IDEA上的记录器:

Hibernate: select developer0_.developer_id as develope1_1_0_, developer0_.name as name2_1_0_ from developer developer0_ where developer0_.developer_id=?
Hibernate: select game0_.game_id as game_id1_6_0_, game0_.developer_id as develope5_6_0_, game0_.esrb_rating as esrb_rat2_6_0_, game0_.name as name3_6_0_, game0_.release_date as release_4_6_0_, developer1_.developer_id as develope1_1_1_, developer1_.name as name2_1_1_ from game game0_ left outer join developer developer1_ on game0_.developer_id=developer1_.developer_id where game0_.game_id=?

以下是我的相关课程:

@Entity
public class Developer {
    @Id
    @GeneratedValue
    @Column(name = "developerId")
    private Long id;

    private String name;

    @OneToMany(mappedBy = "developer", cascade = CascadeType.ALL)
    private List<Game> gameList;

其他人:

@Entity
public class Game {
    @Id
    @GeneratedValue
    @Column(name = "gameId")
    private Long id;

    private String name;

    private Date releaseDate;

    private ESRBRating esrbRating;

    @ManyToMany(mappedBy = "gameList", cascade = CascadeType.ALL)
    private List<User> userList;

    @ManyToOne
    @JoinColumn(name = "developerId")
    private Developer developer;

如果我遗漏了任何其他相关信息,请告诉我,我会提供。

2 个答案:

答案 0 :(得分:2)

如果你想保持双向,你似乎有两个选择:

  • 删除mappedBy = "developer"并让JPA使用加入表来管理一对多关系。

开发者:

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(
        name="DeveloperGame",
        joinColumns = @JoinColumn( name="dev_id"),
        inverseJoinColumns = @JoinColumn( name="game_id")
)
private List<Game> gameList;

游戏:

@ManyToOne
@JoinTable(
        name="DeveloperGame",
        joinColumns = @JoinColumn( name="game_id"),
        inverseJoinColumns = @JoinColumn( name="dev_id")
)
private Developer developer;
  • 如果您不想使用加入表,请移除mappedBy = "developer"并添加@JoinColumn(使您在关系的两边都有一个具有相同列名的连接列

开发者:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "devId")
private List<Game> gameList;

游戏:

@ManyToOne
@JoinColumn(name = "devId")
private Developer developer;

我确实想知道这是设计还是Spring Data REST中的错误。

答案 1 :(得分:1)

Hibernate收集插入,更新和删除,直到刷新entitymanager。这通常在交易结束时完成。 所以可能是您的事务管理无法正常工作。

将org.springframework.transaction的日志记录设置为debug,而不是在打开和关闭事务时应该看到的。