我有一个带有JPA和H2的Spring Boot应用程序,有两个实体:
@Entity
@Table(name = "people")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "person_id")
private long id;
private String name;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
private Set<Skill> skills = new HashSet<Skill>();
}
@Entity
@Table(name = "skills")
public class Skill {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "skill_id")
private long id;
private String name;
@ManyToOne
@JoinColumn (name="person_id")
@JsonBackReference
private Person person;
}
我有个人实体的JPA存储库:
@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
}
当我尝试保存以下对象时,它会立即返回包含技能的正确实体,但是当我调用repository.find(<id-of-saved-entity>)
时,技能为空(因此它们没有被保留):
{ "name": "Test", "skills": [ { "name": "Skill1" }, { "name": "Skill2"} ] }
有人可以帮助我,告诉我我的映射和配置有什么问题吗?
答案 0 :(得分:0)
以下是spring数据REST(HATEOAS)存储库的答案。他们说,你必须向包含[parent]资源
的URI的关联资源发送PUT请求curl -i -X PUT -H "Content-Type:text/uri-list" -d "http://localhost:8080/libraries/1" http://localhost:8080/books/1/library
http://www.baeldung.com/spring-data-rest-relationships
我希望链接有所帮助。