我有两个实体:Student
和StudentDetails
。 Student
只能有一个或没有StudentDetails
。当我提交表单以保存Student
时,也会保存StudentDetails
。如果我发送StudentDetails
实体的数据就可以了。但是,即使我没有为StudentDetails
实体发送任何数据,这种情况也在发生。保存三个Student
后的图像:student
表:student_details
表:
我希望行1,3不会保存在student_details
表中。在student
表中,这两个student_details_id
将是null
。
我如何实现@OneToOne关系,其中父/所有者实体可以具有可选的子实体(如果存在,则为一个,如果不存在,则为零)?
这是我的代码:
学生.java
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "student_name")
private String studentName;
@Column(name = "student_roll")
private String studentRoll;
@Column(name = "student_class")
private String studentClass;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "student_details_id")
private StudentDetails studentDetails;
// Constructors, Getters and Setters
}
StudentDetails.java
@Entity
@Table(name = "student_details")
public class StudentDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "student_contact")
private String studentContact;
@Column(name = "student_email")
private String studentEmail;
@Column(name = "student_address")
private String studentAddress;
@OneToOne(mappedBy = "studentDetails", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
@JsonIgnore
private Student student;
// Constructors, Getters and Setters
}
StudentController.java
@Controller
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/add")
public String add(Model theModel) {
Student theStudent = new Student();
theModel.addAttribute("theStudent", theStudent);
return "student/student_add_form";
}
@PostMapping("/create")
public String create(@ModelAttribute("theStudent") Student theStudent) {
theStudent.setId((long) 0);
studentRepository.save(theStudent);
return "redirect:/students/index";
}
}
student_add_form.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css">
<title>Student List</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-9">
<h1 th:text="'Add New Student'"></h1>
</div>
<div class="col-3">
<a th:href="@{/students/index}" class="btn btn-success">View Student List</a>
</div>
</div>
<div class="row">
<form action="#" th:action="@{/students/create}" th:object="${theStudent}" th:method="POST" class="col-12">
<div class="row">
<div class="form-group col-3">
<label for="studentName">Name:</label> <input type="text" class="form-control"
id="studentName" name="studentName" placeholder="Enter Student's Name" th:field="*{studentName}">
</div>
<div class="form-group col-3">
<label for="studentClass">Class:</label> <input type="text" class="form-control"
id="studentClass" name="studentClass" placeholder="Enter Student's Class" th:field="*{studentClass}">
</div>
<div class="form-group col-3">
<label for="studentRoll">Roll:</label> <input type="text" class="form-control"
id="studentRoll" name="studentRoll" placeholder="Enter Student's Roll" th:field="*{studentRoll}">
</div>
<div class="form-group col-3">
<label for="studentContact">Contact:</label> <input type="text" class="form-control"
id="studentContact" name="studentContact" placeholder="Enter Student's Contact"
th:field="*{studentDetails.studentContact}">
</div>
</div>
<div class="row">
<div class="form-group col-3">
<label for="studentEmail">Email:</label> <input type="text" class="form-control"
id="studentEmail" name="studentEmail" placeholder="Enter Student's Email"
th:field="*{studentDetails.studentEmail}">
</div>
<div class="form-group col-6">
<label for="studentAddress">Address:</label> <input type="text" class="form-control"
id="studentAddress" name="studentAddress" placeholder="Enter Student's Address"
th:field="*{studentDetails.studentAddress}">
</div>
<div class="form-group col-3">
<label></label> <input type="submit" class="form-control btn btn-success" id="saveStudent"
name="saveStudent" value="Save Student">
</div>
</div>
</form>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.js"></script>
<script>
</script>
</body>
</html>
最佳答案
您声明您没有发送StudentDetails的任何详细信息,但是您的表单中显然包含此数据的字段,例如:
<div class="form-group col-3">
<label for="studentEmail">Email:</label> <input type="text" class="form-control"
id="studentEmail" name="studentEmail" placeholder="Enter Student's Email"
th:field="*{studentDetails.studentEmail}">
</div>
即使表单字段保留为空白,也会为这些值提交空字符串,因此Spring将在
StudentDetails
模型属性上绑定新的Student
实例,并相应地将绑定字段设置为空字符串。为了防止这种情况,请告诉Spring将空字符串修剪为null:如果在绑定时,请求中没有与
StudentDetails
有关的非null属性,则StudentDetails
模型属性上的Student
实例将不能在Spring之前设置。您可以全局执行此操作,也可以基于每个控制器执行此操作。参见,例如:
Can spring mvc trim all strings obtained from forms?
我不确定Spring是否一直如此,即自动实例化非简单的嵌套属性,但是我已经在Boot 2.1.8(MVC 5.1.9)中进行了测试,这就是正在发生的情况。
关于java - 即使表单字段留空,Spring也会提交空字符串并创建新实体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58075062/