Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring集成JPA模型中插入期间获取异常_Java_Hibernate_Spring Mvc_Jpa_Spring Orm - Fatal编程技术网

Java 在spring集成JPA模型中插入期间获取异常

Java 在spring集成JPA模型中插入期间获取异常,java,hibernate,spring-mvc,jpa,spring-orm,Java,Hibernate,Spring Mvc,Jpa,Spring Orm,我正在使用Spring与hibernate集成,并在实体对象中使用JPA注释。我创建这个项目是为了使用这个组合测试CRUD操作。我能够从db加载数据,但面临插入问题 我有两张桌子 员工:ID、姓名、年龄、部门 部门:部门ID,部门名称 Employee.java @Entity @Table(name="EMPLOYEE") public class Employee implements Serializable { @Id @Gene

我正在使用Spring与hibernate集成,并在实体对象中使用JPA注释。我创建这个项目是为了使用这个组合测试CRUD操作。我能够从db加载数据,但面临插入问题

我有两张桌子

员工:ID、姓名、年龄、部门

部门:部门ID,部门名称

Employee.java

    @Entity
    @Table(name="EMPLOYEE")
    public class Employee implements Serializable {

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="ID")
        private int id;

        @Column(name="NAME")
        private String name;

        @Column(name="AGE")
        private int age;

        @ManyToOne
        @JoinColumn(name="DEPT", referencedColumnName="DEPT_NAME", updatable=false, insertable=false)
        private Dept department;

//Other getters and setters
爪哇省

@Entity
@Table(name="DEPT")
public class Dept implements Serializable {

    @Id
    @Column(name="DEPT_ID")
    private int deptId;

    @Column(name="DEPT_NAME")
    private String deptName;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="department")
    private List<Employee> employee;
问题2: 另一个问题是,我正在使用entity类中自动生成的Employee ID,但如果我没有在表单中输入ID并单击submit,我将得到以下异常

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'employee' on field 'department': rejected value [VZW]; codes [typeMismatch.employee.department,typeMismatch.department,typeMismatch.com.spring.persistence.Dept,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.department,department]; arguments []; default message [department]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.spring.persistence.Dept' for property 'department'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.spring.persistence.Dept] for property 'department': no matching editors or conversion strategy found]
Field error in object 'employee' on field 'id': rejected value []; codes [typeMismatch.employee.id,typeMismatch.id,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'id'; nested exception is java.lang.IllegalArgumentException]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
我甚至在谷歌上搜索答案,但找不到正确的答案。有人能帮我吗? 谢谢


附言:我没有在这里保存spring配置和web.xml文件。正如我已经提到的,检索操作正在工作,所以我相信配置应该是好的。

我确信问题不在JPA中,它与spring在控制器内的有效对象中转换表单参数的方式有关。 基本上,同一个问题有两个问题:

来自表单的值总是字符串的,所以在控制器中公开的bean应该将其所有属性都作为字符串,以便能够将表单的参数传递到bean属性中。我认为这个过程叫做绑定

这就是为什么我说: 无法将类型为“java.lang.String”的属性值转换为属性“department”所需的类型“com.spring.persistence.Dept”,始终发送字符串。但并非所有属性都是字符串,其中一个属性是Dept,因此无法进行转换

为了解决这个问题,您可以使用只包含字符串的POJO,这是最简单的解决方案,表单中的所有参数都应该在bean中命名为属性。为什么不尝试在控制器中使用bean表单之类的东西而不是实体bean


或者我认为一个更完整的解决方案是使用@modeldattribute和@InitBinder实现一些东西,对此不是很确定,所以请注意这个注释XD。

问题1:中强烈建议您使用Spring JSTL中的标记。它将解决此错误,并可能解决将来的错误


问题2:不要在POJO中使用原语。而且我认为它与问题1有关

我想在这里使用单独的VO类,而不是这个实体类。但是在检索过程中,我看到实体正确地填充了相应类型的值。问题只会在插入时出现。让我尝试为deptName添加一个字符串类型的字段,看看会发生什么。在代码中有**@JoinColumn(name=“DEPT”,referencedColumnName=“DEPT\u name”,updateable=false,insertable=false)**尝试删除insertable=false。还要记住Dept的键是Dept_ID而不是字符串,使用joincolumn注释只会覆盖列的名称。请尝试启用CascadeType。PERSISTOk谢谢,让我试试。给我点时间。嘿,伙计,我也有同样的问题。你找到问题了吗。webinit是答案吗?
    @Controller
    public class EmployeeController {

        @Autowired
        private EmployeeDAO empDao;

        @Autowired
        private DeptDAO deptDao;

    @RequestMapping(value="/loadInsertForm", method=RequestMethod.GET)
        public String loadInsertForm(Model model){
            List<Dept> depList = deptDao.getDept();
            model.addAttribute("deptObjList", depList);
            return "empInsertForm";
        }

        @RequestMapping(value="/insertEmpValues", method=RequestMethod.POST)
        public String insertEmployee(Employee employee){
            empDao.saveEmployee(employee);
            return "loadEmpList";
        }

@RequestMapping(value="/loadEmpList", method=RequestMethod.POST)
    public String displayEmpDetails(Model model){
        List<Employee> emp = empDao.getEmployee();
        model.addAttribute("empList", emp);
        return "empList";
    }
    }
<form:form method="POST" action="insertEmpValues.spr" commandName="employee">
        <table width="30%">
            <tr>
                <td align="right">Employee ID : </td>
                <td><input type="text" name="id" /></td>
            </tr>
            <tr>
                <td align="right">Employee Name : </td>
                <td><input type="text" name="name" /></td>
            </tr>
            <tr>
                <td align="right">Age : </td>
                <td><input type="text" name="age" /></td>
            </tr>
            <tr>
                <td align="right">Department : </td>
                <td>
                    <select name="department">
                        <c:forEach items="${deptObjList}" var="dept">
                            <option id="${dept.deptName}">${dept.deptName}</option>
                        </c:forEach>
                    </select>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <input type="submit" value="Submit" />
                </td>
            </tr>
        </table>
    </form:form>
exception 
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'employee' on field 'department': rejected value [POS]; codes [typeMismatch.employee.department,typeMismatch.department,typeMismatch.com.spring.persistence.Dept,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.department,department]; arguments []; default message [department]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.spring.persistence.Dept' for property 'department'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.spring.persistence.Dept] for property 'department': no matching editors or conversion strategy found]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'employee' on field 'department': rejected value [VZW]; codes [typeMismatch.employee.department,typeMismatch.department,typeMismatch.com.spring.persistence.Dept,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.department,department]; arguments []; default message [department]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.spring.persistence.Dept' for property 'department'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.spring.persistence.Dept] for property 'department': no matching editors or conversion strategy found]
Field error in object 'employee' on field 'id': rejected value []; codes [typeMismatch.employee.id,typeMismatch.id,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'id'; nested exception is java.lang.IllegalArgumentException]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)