A
A
Artem2016-08-20 20:21:55
Java
Artem, 2016-08-20 20:21:55

How to save data from two tables related @OneToOne in Spring mvc?

Help advice. When sending data from a jps page, it is not possible to save the Client object, which includes the Passport object, to the MySQL database.
The page gives an error

HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

Client class
@Entity
@Table(name = "client")
public class Client {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Basic
    @Column(name = "surname")
    private String surname;

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

    @Basic
    @Column(name = "patronymic")
    private String patronymic;

    @Basic
    @Column(name = "phoneMobile")
    private String phoneMobile;    

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "passport_id")
    private Passport passport;

    public Client() {    }
 
    public int getId() {    return id;    }

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

    public String getSurname() {      return surname;   }

    public void setSurname(String surname) {      this.surname = surname;   }

    public String getName() {     return name;   }

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

    public String getPatronymic() {    return patronymic;   }

    public void setPatronymic(String patronymic) {     this.patronymic = patronymic;   }

    public String getPhoneMobile() {    return phoneMobile;   }

    public void setPhoneMobile(String phoneMobile) {     this.phoneMobile = phoneMobile;   }  

    public Passport getPassport() {        return this.passport;    }

    public void setPassport(Passport passport) {       this.passport = passport;   }
}

Passport class
@Entity
@Table(name = "passport")
public class Passport {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Basic
    @Column(name = "series")
    private String series;

    @Basic
    @Column(name = "number")
    private String number;

    @Basic
    @Column(name = "received")
    private String received;  

    @OneToOne(mappedBy = "passport")
    private Client client;

    public Passport() {
    }
   
    public Client getClient() {      return client;   }

    public void setClient(Client client) {       this.client = client;   }


    public int getId() {      return id;   }

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

    public String getSeries() {      return series;  }

    public void setSeries(String series) {     this.series = series;   }


    public String getNumber() {     return number;  }

    public void setNumber(String number) {     this.number = number;  }


    public String getReceived() {      return received;  }

    public void setReceived(String received) {    this.received = received;   }
}

Controller
@Controller
public class ClientController {
    @Autowired
    private ClientService clientService;
       
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addClient(@ModelAttribute Client client) {
        clientService.addClient(client);
        return "redirect:/client";
    }
}

Dao class
@Repository
public class ClientDaoImpl implements ClientDao{

    @Autowired
    PassportService passportService;
    @Autowired
    private SessionFactory sessionFactory;

    public void addClient(Client client){
       Passport passport = passportService.getPassportById(client.getPassport().getId());
        client.setPassport(passport);
        sessionFactory.getCurrentSession().save(client);
    }

/* Этот метод тоже не работает
public void addClient(Client client){       
        sessionFactory.getCurrentSession().save(client);
    }
*/
}

jsp submit page code
<%@ page language="java" contentType="text/html"	pageEncoding="utf8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html lang="ru">
<body>
<form action="${pageContext.servletContext.contextPath}/add" method="post">
    
    <table  >
        <tr>           
            <th><input type="text" name="surname" placeholder="Фамилия" ></th>
        </tr>
        <tr>           
            <th><input type="text" name="name" placeholder="Имя" ></th>
        </tr>
        <tr>           
            <th><input type="text" name="patronymic" placeholder="Отчество" ></th>
        </tr>
        <tr>            
            <th><input type="text" name="phoneMobile" placeholder="Телефон" ></th>
        </tr>
        <tr>           
            <th><input type="text" name="series" id="series"  placeholder="Серия" ></th>
        </tr>
        <tr>           
            <th><input type="text" name="number" id="number"  placeholder="Номер" ></th>
        </tr>
        <tr>            
            <th><input type="text" name="received" id="received"  placeholder="Кем выдан" ></th>
        </tr>

    </table>
  
    <input type="submit"  value="Создать">
    </div>
</form>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2016-08-20
@webserverby

The issue is removed, I found a solution. I changed the field names on the jsp page by adding the prefix passport to the data from the second table.

<form action="${pageContext.servletContext.contextPath}/add" method="post">  
    <table >            
            <th><input type="text" name="surname"  placeholder="Фамилия" ></th>    
            <th><input type="text" name="name"   placeholder="Имя" ></th>          
            <th><input type="text" name="patronymic"   placeholder="Отчество" ></th>      
            <th><input type="text" name="phoneMobile"  placeholder="Телефон" ></th>
             
            <th><input type="text" name="passport.series"  placeholder="Серия" ></th>        
            <th><input type="text" name="passport.number" placeholder="Номер" ></th>         
            <th><input type="text" name="passport.received"   placeholder="Выдан" ></th>       
    </table>
    <input type="submit"  value="Создать">
    </div>
</form>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question