【发布时间】:2020-01-15 17:36:26
【问题描述】:
我有以下实体:
@Entity
@Table(name = "profile")
public class Profile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToOne(cascade = CascadeType.ALL)
private ProfileContacts profileContacts;
...
}
和
@Entity
@Table(name = "profile_contacts")
public class ProfileContacts {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "description")
private String description;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
}
我正在尝试通过将此带有更新的 JSON 发送到 REST 控制器来更新它:
{
"id": 1,
"description": "an update",
"profileContacts": {
"firstName": "John",
"lastName": "Doe"
}
}
所以最后它会调用
profileRepository.save(profile);
其中profileRepository 是ProfileRepository 类的实例:
public interface ProfileRepository extends JpaRepository<Profile, Long> {
}
这是spring-data-jpa接口。
但每次更新后,它都会更新profile 表,但会将new 行添加到profile_contacts 表(对应于ProfileContactsentity 的表),而不是更新现有的。
如何实现更新?
【问题讨论】:
-
你能发布
profile对象的内容吗? -
增加新行是什么意思?新纪录?
-
@SandeshaJ 我添加了更多信息请看一下
-
@gnanajeyam95 表格的新行。
-
no id or not existing id -> "create", existing id -> "update" (?;) (谈论嵌套的 "profileContacts" (json) 元素)
标签: java hibernate spring-boot jpa spring-data-jpa