【发布时间】:2020-04-25 11:51:24
【问题描述】:
在我的真实项目中,有一个具有大约 15 个属性(列)的实体。我需要存储大约 10 个与实体相关的属性。为了避免在同一个表中创建新列,一种方法是将新属性存储在另一个实体中,并将它们与一对一关系链接起来。 Here 我找到了另一种方法,这意味着将可嵌入对象存储在辅助表中。 问题是具有未初始化属性的可嵌入对象不会持久保存到数据库。我已经在demo project 中重现了这个问题:
@Entity
@Table(name = "STUDENTS")
@SecondaryTable(
name = ADDRESSES,
pkJoinColumns = @PrimaryKeyJoinColumn(name = "STUDENT_ID")
)
public class Student {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
@Embedded // not needed
private Address address;
//constructors, getters and setters
@Embeddable
public class Address {
public static final String ADDRESSES = "ADDRESSES";
@Column(table = ADDRESSES)
private String country;
@Column(table = ADDRESSES)
private String city;
@Column(table = ADDRESSES)
private String street;
@Column(table = ADDRESSES)
private Integer houseNumber;
//constructors, getters and setters
@Service
@Transactional
public class StudentService {
@Autowired
private StudentRepository studentRepository;
@Transactional(readOnly = true)
public List<Student> findAll() {
return studentRepository.findAll();
}
public Student createStudentWithEmptyAddress() {
Student student = new Student("Caleb", "Baker", new Address());
return studentRepository.save(student);
}
public Student createStudentWithNonEmptyAddress() {
Address address = new Address("France", "Paris", "Rue Vieille Du Temple", 88);
Student student = new Student("Cayden", "Hoover", address);
return studentRepository.save(student);
}
public Student findById(Long id) {
return studentRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Student with id: " + id + " was not found"));
}
public Student updateStudentWithEmptyAddress(Long id) {
Student student = findById(id);
student.setAddress(new Address());
return student;
}
public Student updateStudentWithNonEmptyAddress(Long id) {
Student student = findById(id);
Address address = new Address("USA", "New York", "Stanton Street", 17);
student.setAddress(address);
return student;
}
}
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/all")
@ResponseStatus(OK)
public List<Student> findAll() {
return studentService.findAll();
}
@GetMapping("/{id}")
@ResponseStatus(OK)
public Student findOne(@PathVariable Long id) {
return studentService.findById(id);
}
@PostMapping("/emptyAddress")
@ResponseStatus(CREATED)
public Student createStudentWithEmptyAddress() {
return studentService.createStudentWithEmptyAddress();
}
@PostMapping("/nonEmptyAddress")
@ResponseStatus(CREATED)
public Student createStudentWithNonEmptyAddress() {
return studentService.createStudentWithNonEmptyAddress();
}
@PutMapping("/{id}/emptyAddress")
@ResponseStatus(OK)
public Student updateStudentWithEmptyAddress(@PathVariable Long id) {
return studentService.updateStudentWithEmptyAddress(id);
}
@PutMapping("/{id}/nonEmptyAddress")
@ResponseStatus(OK)
public Student updateStudentWithNonEmptyAddress(@PathVariable Long id) {
return studentService.updateStudentWithNonEmptyAddress(id);
}
}
POST localhost:8080/student/nonEmptyAddress 返回预期的输出:
jdbc.sqlonly: insert into students (first_name, last_name, id) values ('Cayden', 'Hoover', 4)
jdbc.sqlonly: insert into addresses (city, country, house_number, street, student_id) values ('Paris', 'France', 88, 'Rue Vieille Du Temple', 4)
{
"id": 4,
"firstName": "Cayden",
"lastName": "Hoover",
"address": {
"country": "France",
"city": "Paris",
"street": "Rue Vieille Du Temple",
"houseNumber": 88
}
}
GET localhost:8080/student/4 返回上面的结果。现在让我们创建一个包含未初始化所有属性的 Address 对象的学生:
POST localhost:8080/student/nonEmptyAddress 返回预期的输出:
jdbc.sqlonly: insert into students (first_name, last_name, id) values ('Caleb', 'Baker', 5)
jdbc.sqlonly: insert into addresses (city, country, house_number, street, student_id) values (NULL, NULL, NULL, NULL, 5)
{
"id": 5,
"firstName": "Caleb",
"lastName": "Baker",
"address": {
"country": null,
"city": null,
"street": null,
"houseNumber": null
}
}
但是,当我调用 GET 端点时,我收到了另一个结果。获取 localhost:8080/student/5 :
jdbc.sqlonly: select student0_.id as id1_1_0_, student0_.first_name as first_na2_1_0_, student0_.last_name as last_nam3_1_0_, student0_1_.city as city1_0_0_, student0_1_.country as country2_0_0_, student0_1_.house_number as house_nu3_0_0_, student0_1_.street as street4_0_0_ from students student0_ left outer join addresses student0_1_ on student0_.id=student0_1_.student_id where student0_.id=5
{
"id": 5,
"firstName": "Caleb",
"lastName": "Baker",
"address": null
}
Content of ADRESSES table:
CITY COUNTRY HOUSE_NUMBER STREET STUDENT_ID
Paris France 88 Rue Vieille Du Temple 4
null null null null 5
尽管 ADRESSES 表中存在第二条记录,但地址为空。 当 Address 对象中至少有一个已初始化的属性时,使用地址更新学生按预期工作。但是,如果您使用所有属性都为空的地址(PUT localhost:8080/student/3/emptyAddress)更新学生,则表 ADDRESSES 甚至不会被更新。
上面的例子有什么问题?我认为使用@Embeddable 和@Secondary 表来达到预期结果不是正确的方法。
【问题讨论】:
标签: java hibernate spring-boot jpa embeddable