由于语法错误表未在 DB 中创建

Posted

技术标签:

【中文标题】由于语法错误表未在 DB 中创建【英文标题】:Due to syntax error table is not being created in DB 【发布时间】:2017-01-11 06:33:55 【问题描述】:
// USER_REGISTRATION_DB(POJO class)   
 @Entity
    @Table(name="USER_REGISTRATION_DB")
    public class UserRegistration_DB 

                 // this is for primary key
                @Id
                @GeneratedValue(strategy = GenerationType.TABLE)
                @Column(name="USER-NO")
                private int userno;


        @Size(min=5, max = 5 ,message="please Enter char followed by NUM")
        @NotNull
        @Column(name="UUID")
        private String uuid;                                      

        @Size(min = 2, max = 30,message="Please Enter More than One Character")
        @Column(name = "FIRST_NAME")                    
        private String first_name;                      

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

        @NotNull(message="Please Enter Correct DOB")
        @DateTimeFormat(pattern="dd/mm/yyyy")
        @Past
        @Column(name = "DATE_OF_BIRTH")
        private Date date_of_birth;                          

        //@NotNull(message="Please Enter your Customer_Id")
            @Column(name="AGE")
            private String age;                           

        //@NotEmpty(message="Please Enter your Email")
        @Size(min = 12, max = 40,message="Please Enter More than Eleven Characters")
        @Email(message="Please Enter Valid Email ID")
        @Column(name = "EMAIL", unique=true)
        private String email;                               

        //@NotNull(message="Please Enter your Password")
        @Size(min = 6, max = 30,message="Please Enter more than 6 Characters")
        @Column(name = "PASSWORD")
        private String password;                                  

        @NotNull(message="Please Enter mobile number")
        @Column(name = "MOBILE")
        private String mobile;                               

        //@NotEmpty(message="Please Enter your Username")
        @Size(min = 3, max = 40,message="Please Enter More than 2 Characters")
        @Column(name="USERNAME")
        private String username;                   

        //@NotNull(message="Please Enter your streetName")
        @Size(min = 2, max = 60,message="Please Enter More than 1 Characters")
        @Column(name="ADDRESS")
        private String address;                    

        //@NotNull(message="Please Enter your Customer_Id")
        @Column(name="V_ID")
        private String vid;    





// this my DAO class implementation

public void  saveNewUser(
            UserRegistration_DB userRegistration_DB)throws IOException
            

            System.out.println("In User registration DB DAO class::::::::::"+userRegistration_DB.getFirst_name()+" "+userRegistration_DB.getUuid());
            Session session = getSession();                         //creating session
            session.beginTransaction();                             //begin transaction for the session.
            Query query = session.createQuery("UPDATE UserRegistration_DB set first_name =:firstname,last_name =:lastname,username =:username,date_of_birth =:dob,Email =:email,password =:password,Mobile =:mobile,Address =:address,Age =:age" +
                    " Uuid =:uuid "); 

            query.setParameter("firstname",userRegistration_DB.getFirst_name());
            query.setParameter("lastname",userRegistration_DB.getLast_name());
            query.setParameter("mobile",userRegistration_DB.getMobile());
            query.setParameter("email",userRegistration_DB.getEmail());
            query.setParameter("dob",userRegistration_DB.getDate_of_birth());
            query.setParameter("age",userRegistration_DB.getAge());
            query.setParameter("password",userRegistration_DB.getPassword());
            query.setParameter("username",userRegistration_DB.getUsername());
            query.setParameter("uuid",userRegistration_DB.getUuid());
            query.setParameter("address",userRegistration_DB.getAddress());
            query.setParameter("vid",userRegistration_DB.getVid());

             int user = query.executeUpdate();

             session.save(userRegistration_DB);
             System.out.println("user values are ::::::::::::::::::"+user);
            session.getTransaction().commit();                            //here transaction complete with commit the data in db. 
            session.close();    
            //session close

    


                // this is controller class
                //UserRegistrationController.java

@RequestMapping(value="/signup",method=RequestMethod.POST)
        public String saveRegAction(@ModelAttribute("signup") @Validated UserRegistration_DB userRegistration_DB,BindingResult bindingResult,Model model,HttpSession session,HttpServletResponse servletResponse) throws Exception
        
            System.out.println("the object is:" + model);
            if(bindingResult.hasErrors())
            
                logger.info("user details==========="+userRegistration_DB.getFirst_name()+""+userRegistration_DB.getLast_name()+""+userRegistration_DB.getMobile()+""+userRegistration_DB.getEmail()+""+
            userRegistration_DB.getDate_of_birth()+""+userRegistration_DB.getAge()+""+userRegistration_DB.getPassword()+""+userRegistration_DB.getUsername()+""+userRegistration_DB.getAddress());
                logger.info("Returning home.jsp page"); 
                model.addAttribute("login", new Login()); 
                model.addAttribute("user", new UserRegistration_DB()); 


                return "signup";
            
            //registerService.saveRegister(register);  
            userRegistrationService.saveNewUser(userRegistration_DB);
            session.setAttribute("userRegistration_DB", userRegistration_DB);
            return "main";

        



// error am getting is
// am Using mysql DB
// User_REGISTRATION_DB is not being created

/* HHH000388: 不成功: 创建表 USER_REGISTRATION_DB (USER-NO integer not null, ADDRESS varchar(60), AGE varchar(255), DATE_OF_BIRTH datetime not null, EMAIL varchar(40), FIRST_NAME varchar(30), LAST_NAME varchar(255), MOBILE varchar(255) not null, PASSWORD varchar(30), USERNAME varchar(40), UUID varchar(5) not null, V_ID varchar(255), 主键 (USER-NO))

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-NO integer not null, ADDRESS varchar(60), AGE varchar(255), DATE_OF_BIRTH datet' at line 1   */    

【问题讨论】:

问题与jsp无关。 【参考方案1】:

在列名中使用连字符似乎是 Hibernate 的一个已知问题。

在 JPA 1.0 中,解决方法是使用反引号转义列名:

// this is for primary key
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "`USER-NO`")
private int userno;

在 JPA 2.0 及更高版本中,您可以简单地将列名放在引号中,例如

@Column(name = "\"USER-NO\"")

参考:

Creating field with reserved word name with JPA

【讨论】:

一般规则:尽可能避免使用此类名称

以上是关于由于语法错误表未在 DB 中创建的主要内容,如果未能解决你的问题,请参考以下文章

数据库更改日志表未在 Liquibase 的 Postgres 环境中创建

mySql 中创建过程的语法错误

MySQL 6.3 - 在语法中创建过程错误

试图在 Mysql 中创建存储过程的语法错误?

在 sqlite 数据库中创建表时出现语法错误

当我尝试在 mysql 中创建触发器时出现语法错误