如何在Spring MVC中编辑单个表单中的两个模型类

时间:2015-03-13 17:06:50

标签: java spring hibernate jsp spring-mvc

我需要编辑一个表单,该表单处理两个实体类,file.javaclient.java,其主客户端为client,客户端ID为file.java中的Client_clientId。我可能在网址上做错了,我正在修复。

这是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;

这是我正在处理编辑的控制器类。

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

中的网址操作
<form method="post" action="/advocatoree/manageFiles/updatedFileInfo/${clients.clientId}">

一旦我编辑数据并提交它们,它们应该保存在数据库中,并且还应该在我的页面中显示已编辑的信息。我看到的是当我单击编辑按钮时,它采用fileNumber,在我编辑和提交数据之后,编辑的信息将保留在该文件编号中,该文件编号等于上一个文件的clientId。如果15是文件号65的clientId,则数据将保留在文件号15中。

请。救命啊!

1 个答案:

答案 0 :(得分:1)

看着你的方法,我发现你正在保存文件和文件。客户端ID,但持久化您只是持久化客户端ID。但请注意,如果不持久保存文件ID,你完全没问题。也许强制持久化客户端ID存在问题。试试没有它。它应该工作。

再次,从逻辑上讲,您的方法应该有效。我猜你没有成功保存正确的值。在保存之前尝试获取已保存的id值的打印,并检查是否正确存储它们。