调用 JpaRepository.getById() 后的结果实体不包含调用 JpaRepository.saveAndFlush() 后带有 @JoinColumn 的字段的值
The result entity after calling JpaRepository.getById() does not contain values for fields with @JoinColumn after calling JpaRepository.saveAndFlush()
当我保存一个具有引用另一个实体的 @JoinColumn
字段的实体时,它会通过调用 saveAndFlush()
按预期正确保存。现在,我希望能够 return 此实体及其相关实体返回给用户。我假设使用新保存实体的 ID 调用 getById()
也会检索 returned 实体中的 @JoinColumn
值,但是,returned 实体的相关实体包含与 saveAndFlush()
中使用的相关实体完全相同的值。我已经制作了示例代码来演示我在说什么。
我发送请求:
GET http://localhost:8080
并收到回复:
{
"id": 3,
"name": "tests",
"related": {
"id": 3,
"name": null
}
}
我的目标是获得相同的请求return以下响应:
{
"id": 3,
"name": "tests",
"related": {
"id": 3,
"name": "test"
}
}
其中 related
包含 name
以及 saveAndFlush()
中指定的 id
。
Controller.java
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class Controller {
private final ObjectRepository oRepo;
private final RelatedObjectRepository coRepo;
@GetMapping
private Object test() {
// imagine as if this entity is already present in the database before this request is handled, this line is only for context.
RelatedObject co = coRepo.saveAndFlush(new RelatedObject(null, "test"));
// create an instance of RelatedObject to contain the value for the related_id foreign key column.
RelatedObject nco = new RelatedObject(co.getId(), null);
// create Object and save it along with the referenced RelatedObject.
Object o = oRepo.saveAndFlush(new Object(null, "tests", nco));
// I expected all values of 'co' object to be contained in the result here in the @JoinColumn field 'related' of Object, instead, only 'id' is present like the 'nco' object
return oRepo.getById(o.getId());
}
}
Object.java
import lombok.*;
import javax.persistence.*;
@Table(name = "object")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Object {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "name", length = 100)
private String name;
@ManyToOne
@JoinColumn(name = "related_id")
private RelatedObject related;
}
相关Object.java
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
@Table(name = "related_object")
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RelatedObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id", nullable = false)
private Long id;
@Column(name = "name", length = 100)
private String name;
}
ObjectRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface ObjectRepository extends JpaRepository<Object, Long> {
}
相关ObjectRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface RelatedObjectRepository extends JpaRepository<RelatedObject, Long> {
}
SpringPlayGroundApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringPlayGroundApplication {
public static void main(String[] args) {
SpringApplication.run(SpringPlayGroundApplication.class, args);
}
}
好的,我知道我需要什么了。
关联实体需要级联刷新,例如:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@JoinColumn(name = "parent_id")
private Category parent;
当我保存一个具有引用另一个实体的 @JoinColumn
字段的实体时,它会通过调用 saveAndFlush()
按预期正确保存。现在,我希望能够 return 此实体及其相关实体返回给用户。我假设使用新保存实体的 ID 调用 getById()
也会检索 returned 实体中的 @JoinColumn
值,但是,returned 实体的相关实体包含与 saveAndFlush()
中使用的相关实体完全相同的值。我已经制作了示例代码来演示我在说什么。
我发送请求:
GET http://localhost:8080
并收到回复:
{
"id": 3,
"name": "tests",
"related": {
"id": 3,
"name": null
}
}
我的目标是获得相同的请求return以下响应:
{
"id": 3,
"name": "tests",
"related": {
"id": 3,
"name": "test"
}
}
其中 related
包含 name
以及 saveAndFlush()
中指定的 id
。
Controller.java
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class Controller {
private final ObjectRepository oRepo;
private final RelatedObjectRepository coRepo;
@GetMapping
private Object test() {
// imagine as if this entity is already present in the database before this request is handled, this line is only for context.
RelatedObject co = coRepo.saveAndFlush(new RelatedObject(null, "test"));
// create an instance of RelatedObject to contain the value for the related_id foreign key column.
RelatedObject nco = new RelatedObject(co.getId(), null);
// create Object and save it along with the referenced RelatedObject.
Object o = oRepo.saveAndFlush(new Object(null, "tests", nco));
// I expected all values of 'co' object to be contained in the result here in the @JoinColumn field 'related' of Object, instead, only 'id' is present like the 'nco' object
return oRepo.getById(o.getId());
}
}
Object.java
import lombok.*;
import javax.persistence.*;
@Table(name = "object")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Object {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "name", length = 100)
private String name;
@ManyToOne
@JoinColumn(name = "related_id")
private RelatedObject related;
}
相关Object.java
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
@Table(name = "related_object")
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RelatedObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id", nullable = false)
private Long id;
@Column(name = "name", length = 100)
private String name;
}
ObjectRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface ObjectRepository extends JpaRepository<Object, Long> {
}
相关ObjectRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface RelatedObjectRepository extends JpaRepository<RelatedObject, Long> {
}
SpringPlayGroundApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringPlayGroundApplication {
public static void main(String[] args) {
SpringApplication.run(SpringPlayGroundApplication.class, args);
}
}
好的,我知道我需要什么了。
关联实体需要级联刷新,例如:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@JoinColumn(name = "parent_id")
private Category parent;