Hibernate中的映射错误

时间:2016-09-14 05:48:42

标签: hibernate spring-mvc thymeleaf

我已经在Spring Boot中准备了一个应用程序

我在两个表之间的hibernate中实现了oneToOne映射:

person and PersonDetail。

现在,当我运行该程序时,我得到以下错误

绑定结果和模型对象都不适用于' person'。

错误在personform.html中,字段地址和年龄。

//////////////////////////////控制器//////////////// ///////////////

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;




import com.model.Person;
//import com.model.PersonDetail;
import com.service.PersonService;

@Controller
public class PersonController {

    Person person = new Person();

    private PersonService personService;


     @Autowired
        public void setPersonService(PersonService personService) {
            this.personService = personService;
        }


        @RequestMapping(value = "/persons", method = RequestMethod.GET)
        public String list(Model model){
            model.addAttribute("persons", personService.listAllPersons());
            System.out.println("Returning Persons");
            return "persons";
        }




        @RequestMapping("person/{id}")
        public String showPerson(@PathVariable Integer id, Model model){
            model.addAttribute("person", personService.getPersonById(id));          
            return "personshow";
        }



        @RequestMapping("person/edit/{id}")
        public String edit(@PathVariable Integer id, Model model){
            model.addAttribute("person", personService.getPersonById(id));
            return "personform";
        }


        @RequestMapping("person/new")
        public String newPerson(Model model){
            model.addAttribute("person", new Person());
            return "personform";
        }


        @RequestMapping(value = "person", method = RequestMethod.POST)
        public String savePerson(Person person){
            personService.savePerson(person);
            return "redirect:/persons/";
        }


        @RequestMapping("person/delete/{id}")
        public String delete(@PathVariable Integer id){
            personService.deletePerson(id);
            /*return "redirect:/products";*/
            //return "persons";
            return "redirect:/persons/";

        }


}

////////////////////////与人模型//////////////////// ///////////

package com.model;



import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;




@Entity
//@Table(name = "person")
public class Person{


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id",unique=true,nullable=false)
        private Integer id;     
    @Column(name = "name", unique = true, nullable = false, length = 10)
        private String name;        
        @OneToOne(cascade= CascadeType.ALL)
        private PersonDetail personDetail;      

    /*
        public Person(){}
        public Person(Integer id,String name)
        {
            this.id=id;
            this.name=name;
        }*/

        public Integer getId() {
            return this.id;
        }
        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return this.name;
        }
        public void setName(String name) {
            this.name = name;
        }


        public PersonDetail getPersonDetail() {
            return personDetail;
        }
        public void setPersonDetail(PersonDetail personDetail) {
            this.personDetail = personDetail;

        }


}

//////////////////////////// PersonDetail-模型//////////////// //////

package com.model;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;






@Entity

public class PersonDetail{

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)       
    private Integer id; 

    @Column(name = "address", nullable = false, length = 20)
    private String address; 

    @Column(name = "age", nullable = false,length=10)
    private Integer age;     

    @OneToOne
    @PrimaryKeyJoinColumn
    private Person person;  
    /*
    public PersonDetail()
    {

    }   
    public PersonDetail(Integer id,String address,Integer age)
    {
        this.id=id;
        this.address=address;
        this.age=age;
    }*/

    public Integer getId() {
        return this.id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getAddress() {
        return this.address;
    }


    public void setAddress(String address) {
        this.address = address;
    }


    public Integer getAge() {
        return this.age;

    }


    public void setAge(Integer age) {
        this.age = age;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;

    }


}

///////////////////////////// PersonRepository ///////////////// /////

package com.repository;

import javax.transaction.Transactional;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.model.Person;
@Repository
@Transactional
public interface PersonRepository extends CrudRepository<Person, Integer>{
}

////////////////////// PersonDetailReppository /////////////////////

package com.repository;

import javax.transaction.Transactional;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.model.PersonDetail;
@Repository
@Transactional
public interface PersonDetailRepository extends CrudRepository<PersonDetail, Integer>{

}

/////////////////////////////////的index.html /////////// //////////

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">

    <title>Spring Boot Example</title>
    <!-- <th:block th:include="fragments/headerinc :: head"></th:block> -->
</head>
<body>
<div class="container">
    <div th:fragment="header">
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="navbar-header">
                    <a class="navbar-brand" href="#" th:href="@{/}">Home</a>
                    <ul class="nav navbar-nav">
                        <li><a href="#" th:href="@{/persons}">Persons</a></li>
                        <li><a href="#" th:href="@{/person/new}">Create Person</a></li>
                    </ul>

                </div>
            </div></nav></div>
<!-- <div class="container"> -->
   <!--  <th:block th:include="fragments/header :: header"></th:block> -->
</div>
</body>
</html>

/////////////////////////人///////////////////// ///////

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">

    <title>Persons</title>

   <th:block th:include="fragments/headerinc :: head"></th:block> 
</head>
<body>
<div class="container">
  <!--   <th:block th:include="fragments/header :: header"></th:block> /*/ -->
    <div th:if="${not #lists.isEmpty(persons)}">
        <h2>Person List</h2>
        <table class="table table-striped">
            <tr>
                <th>Id</th>
                <th>Person Id</th>
                <th>Name</th>
                <th>Address</th>
                <th>Age</th>
                <th>View</th>
                <th>Edit</th>
                <th>Delete</th>


            </tr>

            <tr th:each="person : ${persons}">
                <td th:text="${person.id}"><a href="/person/${person.id}">Id</a></td>
                <td th:text="${person.id}">Person Id</td>
                <td th:text="${person.name}">Name</td>
                <td th:text="${person.personDetail.address}">Address</td>
                <td th:text="${person.personDetail.age}">Age</td>

                <td><a th:href="${ '/person/' + person.id}">View</a></td>
                <td><a th:href="${'/person/edit/' + person.id}">Edit</a></td>
                <td><a th:href="${'/person/delete/' + person.id}">Delete</a></td>

            </tr>           

        </table>
    </div>
</div>


</body>
</html>

////////////////////////////////////为personForm ////////// ///////////

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">

    <title>Spring Boot Example</title>

     <th:block th:include="fragments/headerinc :: head"></th:block> 
</head>
<body>
<div class="container">
    <th:block th:include="fragments/header :: header"></th:block> 

    <h2>Person Details</h2>
    <div>
    <!-- th:object="${person}" --> 

        <form class="form-horizontal" th:action="@{/person}"  method="post">
             <input type="hidden" th:field="*{person.id}"/> 

            <!-- <div class="form-group">
                 <label class="col-sm-2 control-label">Person Id:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{id}"/> 
                </div> 
            </div> -->
            <div class="form-group">
                <label class="col-sm-2 control-label">Name:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{person.name}"/>
                </div>
            </div>
             <div class="form-group">
                <label class="col-sm-2 control-label">Address:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{person.address}"/>
                </div>
            </div>
             <div class="form-group">
                <label class="col-sm-2 control-label">Age:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{person.age}"/>
                </div>
            </div>

            <div class="row">
                <button type="submit" class="btn btn-default">Submit</button>
            </div>
        </form>
    </div>
</div>

</body>
</html>

/////////////////////////////////// personshow /////////// ////////////

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">

    <title>Person Details</title>

    <th:block th:include="fragments/headerinc :: head"></th:block> 
</head>
<body>
<div class="container">
    <!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->

    <h2>Person Details</h2>
        <div>
            <form class="form-horizontal" th:action="@{/person}" th:object="${person}" method="get">
                <div class="form-group">
                    <label class="col-sm-2 control-label">Person Id:</label>
                    <div class="col-sm-10">
                        <p class="form-control-static" th:text="${id}">Person Id</p></div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Name:</label>
                    <div class="col-sm-10">
                        <p class="form-control-static" th:text="${person.name}">name</p>
                    </div>
                </div>
                 <div class="form-group">
                    <label class="col-sm-2 control-label">Address:</label>
                    <div class="col-sm-10">
                        <p class="form-control-static" th:text="${person.personDetail.address}">address</p>
                    </div>
                </div>
                 <div class="form-group">
                    <label class="col-sm-2 control-label">Age:</label>
                    <div class="col-sm-10">
                        <p class="form-control-static" th:text="${person.personDetail.age}">age</p>
                    </div>
                </div>

            </form>
    </div>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

由于您已将personDetail与person映射,因此您需要像这样提及:

mainclass.mappedclass.fieldname

因此,在您的情况下,它将是person.personDetail.fieldname

将personform.html更改为:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">

    <title>Spring Boot Example</title>

     <th:block th:include="fragments/headerinc :: head"></th:block> 
</head>
<body>
<div class="container">
    <th:block th:include="fragments/header :: header"></th:block> 

    <h2>Person Details</h2>
    <div>
    <!-- th:object="${person}" --> 

        <form class="form-horizontal" th:action="@{/person}"  method="post">
             <input type="hidden" th:field="*{person.id}"/> 

            <!-- <div class="form-group">
                 <label class="col-sm-2 control-label">Person Id:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{id}"/> 
                </div> 
            </div> -->
            <div class="form-group">
                <label class="col-sm-2 control-label">Name:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{person.name}"/>
                </div>
            </div>
             <div class="form-group">
                <label class="col-sm-2 control-label">Address:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{person.personDetail.address}"/>
                </div>
            </div>
             <div class="form-group">
                <label class="col-sm-2 control-label">Age:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" th:field="*{person.personDetail.age}"/>
                </div>
            </div>

            <div class="row">
                <button type="submit" class="btn btn-default">Submit</button>
            </div>
        </form>
    </div>
</div>

</body>
</html>