本文介绍了由于语法错误,未在数据库中创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// 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整数不为null,ADDRESS varchar(60),AGE varchar(255),DATE_OF_BIRTH日期时间不为null,EMAIL varchar(40),FIRST_NAME varchar(30),LAST_NAME varchar(255),MOBILE varchar(255)不为null,PASSWORD varchar(30),USERNAME varchar(40),UUID varchar(5)不为null,V_ID varchar(255),主键(USER-NO))

/* HHH000388: Unsuccessful: create table 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), primary key (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   */

推荐答案

在列名称中使用连字符似乎是Hibernate的已知问题.

Using hyphens in column names appears to be a known problem with Hibernate.

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

In JPA 1.0 the workaround is to escape the column name with backticks:

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

在JPA 2.0和更高版本中,您可以简单地在列名两边加上引号,例如

In JPA 2.0 and later, you can simply place quotes around the column name, e.g.

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

参考:

使用JPA创建具有保留单词名称的字段

这篇关于由于语法错误,未在数据库中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 00:12