2016-09-14 165 views
0

我在春天引导映射错误在Hibernate中

准备的应用程序,我已经实现了oneToOne映射在Hibernate中两个表之间:

人,PersonDetail。

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

无论绑定的结果,也没有模型可用于“人”的对象。

错误发生在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/"; 

     } 


} 

//////////////////////// Person-Model ///////////////////////////////

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

既然你已经映射personDetail与人,你需要一提的是这样的:

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>