问题描述
我正在使用Spring构建简单的CRUD示例。 我有两个实体(Project)和(ProjectExpenditure),它们具有M:1关系。 我的问题是“当我通过POST提交ProjectExpenditure时,将保存null(对于type =“ text”)和0(对于type =“ number”),而不是发送真实数据。
实体1-项目
@Entity
@Getter @Setter
@Table(name = "projects")
public class Project {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int budget;
// Business Logic: Adding Expenditure will minus Project budget
public void minusBudget(int expenditure) {
int leftBudget = this.budget -= expenditure;
if(leftBudget <= 0) {
throw new NotEnoughBudgetException ("Budget is not enough");
}
this.budget = leftBudget;
}
}
实体2-ProjectExpenditure
@Entity
@Getter @Setter
@requiredArgsConstructor
@Table(name = "project_expenditure")
public class ProjectExpenditure{
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn(name = "project_id")
private Project project;
private int cost;
private String description;
// Add Expenditure: Adding Expenditure will minus Project budget
public ProjectExpenditure createExpenditure (Project project,int cost,String description) {
ProjectExpenditure expenditure = new ProjectExpenditure();
expenditure.setProject(project);
expenditure.setCost(cost);
expenditure.setDescription(description);
project.minusBudget(cost);
return expenditure;
}
}
存储库
@RepositoryRestResource
public interface ProjectRepository extends CrudRepository<Project,Long> { }
@RepositoryRestResource
public interface ProjectExpenditureRepository extends CrudRepository<ProjectExpenditure,Long> { }
服务
@Service
@Transactional(readOnly = true)
@requiredArgsConstructor
public class ProjectExpenditureService {
private final ProjectRepository pRepository;
private final ProjectExpenditureRepository peRepository;
/*
* Add Expenditure
*/
@Transactional
public Long addExpenditure (Long projectId,String description) {
// Create ProjectExpenditure
ProjectExpenditure projectExpenditure = new ProjectExpenditure();
pRepository.findById(projectId).ifPresent(p -> projectExpenditure.createExpenditure(p,cost,description));
// Save ProjectExpenditure
peRepository.save(projectExpenditure);
return projectExpenditure.getId();
}
}
控制器
@Controller
@requiredArgsConstructor
public class ProjectExpenditureController {
private final ProjectRepository pRepository;
private final ProjectExpenditureRepository peRepository;
private final ProjectExpenditureService service;
// Add Expenditure
@GetMapping(value = "expenditureadd/{id}")
public String addExpenditure(@PathVariable("id") Long projectId,Model model) {
ProjectExpenditure expenditure = new ProjectExpenditure();
pRepository.findById(projectId).ifPresent(p -> expenditure.setProject(p));
model.addAttribute("expenditure",expenditure);
return "expenditure/addexpenditure";
}
@PostMapping(value = "/expenditureadd/{id}")
public String expendtirueSubmit(@PathVariable("id") Long project_Id,@RequestParam("project.id") Long projectId,@RequestParam("cost") int cost,@RequestParam("description") String description,Model model) {
service.addExpenditure(projectId,description);
return "redirect:/expendituretlist/{id}";
}
}
查看-增加支出
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<title>Project Management</title>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css"
th:href="@{/css/bootstrap.min.css}" />
</head>
<body style="margin: 15px;">
<div class="container">
<div th:replace="fragments/title :: title" />
<div class="form-group">
<form class="form-horizontal" th:action="@{/expenditureadd/__${id}__}" th:object="${expenditure}" method="post">
<input type="hidden" id="projectId" th:field="*{project.id}" /> <br>
<label for="expenditure" class="control-label col-sm-2">Expenditure</label>
<input type="number" id="cost" th:field="*{cost}" class="form-control" />
<br>
<label for="description" class="control-label col-sm-2">Description</label>
<input type="text" id="description" th:field="*{description}" class="form-control"/> <br>
<input type ="submit" class="btn btn-success" value="Submit"/>
</form>
</div>
<div th:replace="fragments/footer :: footer" />
</div>
</body>
</html>
提交新的支出后,数据库更新如下:
id |费用|说明| project_id
1 | 0 |空|空
映射是否存在问题?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)