gpt4 book ai didi

java - 如何在 Spring MVC 中以单一形式编辑两个模型类

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:05 26 4
gpt4 key购买 nike

我需要编辑一个处理两个实体类的表单,file.javaclient.java 具有客户端的主键,clientId 作为 file.java 中的 Client_clientId .我可能在 url 中做错了什么并且正在修复。

这是file.java

@Entity
public class file {
@Id
private int fileNumber;
private String fileName;
private String description;
private String purpose;
@Transient
private String assignedAdvocate;
private String comment;
private int Client_clientID;
@DateTimeFormat(pattern = "dd-MM-yyyy")
private Date timestamp;

这是client.java

@Entity
public class client {

@Id
@GeneratedValue
private int clientId;
@NotNull
private String clientName;
private String clientAddress;
private String contactPerson;
private int phone;
@Email
private String email;
private String bank;
private String branch;
private String accountNumber;

这是我处理编辑的 Controller 类。

ManageFiles.java



@Controller
@RequestMapping("/advocatoree/manageFiles")
public class ManageFiles {

@PersistenceContext
EntityManager em;

@RequestMapping(value = "/searchFile")
public ModelAndView inputFileNumber(ModelAndView model){

model.setViewName("searchFile");
return model;
}

@Transactional
@RequestMapping(value = "/fileStatus", method = RequestMethod.GET)
public ModelAndView viewFileStatus(@RequestParam("file_number")int id, ModelAndView model){

model.setViewName("fileStatus");

file file = em.find(com.techflakes.advocatoree.model.file.class, id);
model.addObject("file", file);

client client = (com.techflakes.advocatoree.model.client)em.createQuery("SELECT c FROM client c WHERE c.clientId =:id")
.setParameter("id", file.getClient_clientID()).getSingleResult();
model.addObject("client", client);

List<deposit> deposits = (List<deposit>)em.createQuery("SELECT d FROM deposit d WHERE d.File_fileNumber =:id")
.setParameter("id", id).getResultList();


List<expense> expenses = (List)em.createQuery("SELECT e FROM expense e WHERE e.File_fileNumber =:id")
.setParameter("id", id).getResultList();

model.addObject("depositList", deposits);
model.addObject("expenseList", expenses);

List<bill> fileBills = em.createQuery("SELECT b FROM bill b WHERE b.file_fileNumber =:id")
.setParameter("id", id).getResultList();

model.addObject("fileBills", fileBills);

List<fileStatus> fileStatusList = (List)em.createQuery("SELECT f FROM fileStatus f WHERE f.file_fileNumber =:id").setParameter("id", id).getResultList();
for(fileStatus fs: fileStatusList){
String s =(String)em.createQuery("SELECT e.name FROM employee e WHERE e.employeeID =:id")
.setParameter("id", fs.getEmployee_employeeID()).getSingleResult();
fs.setEmployeeName(s);
}
model.addObject("fileStatusList", fileStatusList);

List<employee> employeeList = em.createQuery("SELECT e FROM employee e WHERE e.employeeID !=0").getResultList();

model.addObject("employeeList", employeeList);

List<Integer> employeefiles = em.createQuery("SELECT e.employee_employeeID FROM employeefile e WHERE e.file_fileNumber =:id")
.setParameter("id", id).getResultList();

ArrayList<String> employeeNames = new ArrayList<String>();
for(Integer i: employeefiles){
employee e = em.find(employee.class, i);
employeeNames.add(e.getName());
}

model.addObject("employeeNames", employeeNames.toString());

model.addObject("employeeFiles", employeefiles);
return model;
}


@Transactional
@RequestMapping(value = "/updateFileClient/{id}", method = RequestMethod.POST)
public ModelAndView modifyFileClientInfo(ModelAndView model, @PathVariable("id")int id){
model.setViewName("modifyFileClientInfo");
file file = em.find(com.techflakes.advocatoree.model.file.class, id);
// client client = em.find(com.techflakes.advocatoree.model.client.class, id);
client client = (com.techflakes.advocatoree.model.client)em.createQuery("SELECT c FROM client c WHERE c.clientId =:id")
.setParameter("id", file.getClient_clientID()).getSingleResult();
int zero=0;
List<employee> employeeList = em.createQuery("SELECT e FROM employee e WHERE e.employeeID >:zero")
.setParameter("zero", zero).getResultList();
model.addObject("employeeList", employeeList);
model.addObject("files", file);
model.addObject("clients", client);
return model;
}

@Transactional
@RequestMapping(value = "/updatedFileInfo/{id}", method = RequestMethod.POST)
public String updateFileInfo(@ModelAttribute("")file f, client c,
@PathVariable("id")int id){

file file = em.find(com.techflakes.advocatoree.model.file.class, id);
client client = em.find(com.techflakes.advocatoree.model.client.class, id);
// client client = (com.techflakes.advocatoree.model.client)em.createQuery("SELECT c FROM client c WHERE c.clientId =:id")
// .setParameter("id", file.getClient_clientID()).getSingleResult();
file.setFileName(f.getFileName());
file.setDescription(f.getDescription());
file.setPurpose(f.getPurpose());
file.setAssignedAdvocate(f.getAssignedAdvocate());
file.setComment(f.getComment());
file.setTimestamp(f.getTimestamp());
file.setClient_ClientID(c.getClientId());
client.setClientName(c.getClientName());
client.setClientAddress(c.getClientAddress());
client.setContactPerson(c.getContactPerson());
client.setPhone(c.getPhone());
client.setEmail(c.getEmail());
client.setBank(c.getBank());
client.setBranch(c.getBranch());
client.setAccountNumber(c.getAccountNumber());
em.persist(client);
em.flush();
em.refresh(client);
em.persist(file);
return "redirect:/advocatoree/manageFiles/searchFile";
}

}

这是filestatus.jsp中的编辑按钮

<form action="/advocatoree/manageFiles/updateFileClient/${file.fileNumber}" method="post">
<button type="submit" class="btn btn-purple">Edit Information</button>
</form>

这是 modifyFileClientInfo.jsp 中的 url 操作

<form method="post" action="/advocatoree/manageFiles/updatedFileInfo/${clients.clientId}">

一旦我编辑并提交了数据,它们就应该保存在数据库中,并且还应该在我的页面中显示编辑后的信息。我看到的是,当我单击编辑按钮时,它会获取 fileNumber,在我编辑并提交数据后,编辑后的信息将保存在该文件编号中,该文件编号等于前一个文件的 clientId。如果15是65号文件的clientId,则数据持久化到15号文件。

请。帮助!

最佳答案

查看您的方法,我发现您同时保存了文件和客户端 ID,但在坚持时您只保存了客户端 ID。但是请注意,如果不保留文件 ID,您是如何完全没问题的。也许强制保留客户端 ID 存在问题。尝试没有它。它应该有效。

同样,从逻辑上讲,您的方法应该有效。我猜你没有成功地正确保存这些值。在持久化之前尝试打印已保存的 id 值,并检查您是否正确存储了它们。

关于java - 如何在 Spring MVC 中以单一形式编辑两个模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29038089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com