无法使用JPA

时间:2018-04-09 06:40:49

标签: spring hibernate spring-boot jpa

我正在尝试通过http://localhost:8080/register插入一个用户来注册一个新User.But当我点击注册按钮时它会显示hibernate错误,例如“出现意外错误(类型=内部服务器错误) ,status = 500)。不能执行语句; SQL [n / a];约束[null];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句“。

的welcome.jsp

<!DOCTYPE html >
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet"
        href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script
        src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script
        src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    </head>
    <body>

        <div role="navigation">
            <div class="navbar navbar-inverse">
                <a href="/home" class="navbar-brand">Tecno-Tab</a>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li><a href="/login">Login</a></li>
                        <li><a href="/register">New Registration</a></li>
                        <li><a href="/show-users">All Users</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <c:choose>
        <c:when test="${mode=='MODE_HOME'}">
        <div class="container" id="homediv">
            <div class="jumbotron text-center">
                <h1>Welcome to Tecno-tab</h1>
                <h3>Subscribe my channel to support me</h3>
            </div>
        </div>
        </c:when>
        <c:when test="${mode=='MODE_REGISTER'}">    
        <div class="container text-center">
            <h3>New Registration</h3>
            <hr>
            <form class="form-horizontal" method="POST" action="save-user">
                <input type="hidden" name="id" value="${user.id }" />
                <div class="form-group">
                    <label class="control-label col-md-3">Username</label>
                    <div class="col-md-7">
                        <input type="text" class="form-control" name="username"
                            value="${user.userName}" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-md-3">First Name</label>
                    <div class="col-md-7">
                        <input type="text" class="form-control" name="firstname"
                            value="${user.firstName}" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-md-3">Last Name</label>
                    <div class="col-md-7">
                        <input type="text" class="form-control" name="lastname"
                            value="${user.lastName}" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-md-3">Age </label>
                    <div class="col-md-3">
                        <input type="text" class="form-control" name="age"
                            value="${user.age}" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-md-3">Password</label>
                    <div class="col-md-7">
                        <input type="password" class="form-control" name="password"
                            value="${user.passWord}" />
                    </div>
                </div>
                <div class="form-group ">
                    <input type="submit" class="btn btn-primary" value="Register" />
                </div>
            </form>
        </div>
    </c:when>
        </c:choose>
        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="static/js/jquery.min.js"></script>
        <script src="static/js/bootstrap.min.js"></script>
    </body>
    </html>

ApplicationController.java

package com.ashwin.myapplication.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ashwin.myapplication.model.User;
import com.ashwin.myapplication.service.UserService;

@Controller
public class ApplicationController {

    @Autowired
    private UserService userService;

    @RequestMapping("/home")
    public String Welcome(HttpServletRequest request) {
        request.setAttribute("mode","MODE_HOME");
        return "welcome";
    }

    @RequestMapping("/register")
    public String registeration(HttpServletRequest request) {
        request.setAttribute("mode","MODE_REGISTER");
        return "welcome";

    }

    @PostMapping("/save-user")
    public String saveUser(@ModelAttribute User user,
            BindingResult bindingResult,HttpServletRequest request) {
        userService.saveMyUser(user);
        request.setAttribute("mode","MODE_HOME");
        return "welcome";
    }
}

User.java

package com.ashwin.myapplication.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name="mytable1")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;


    @Column(name="user_name")
    private String userName;


    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

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

    @Column(name="password")
    private String passWord;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassWord() {
        return passWord;
    }
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
    public User(String firstName, String lastName, int age, String password) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.passWord = password;
    }
    public User() {

    }
    @Override
    public String toString() {
        return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", password="
                + passWord + "]";
    }


}

0 个答案:

没有答案