当前位置: 首页 > 知识库问答 >
问题:

由于语法错误,没有在DB中创建表

刘安志
2023-03-14
// 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(用户-否整数不为空,地址varchar(60),年龄varchar(255),出生日期date_of_birding datetime不为空,电子邮件varchar(40),第一名varchar(30),最后一名varchar(255),移动varchar(255)不为空,密码varchar(30),用户名varchar(40),UUID varchar(5)不为空,V_ID varchar(255),主键(用户-否))

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   */    

共有1个答案

柯冯浩
2023-03-14

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

在JPA1.0中,解决方法是使用反勾符转义列名:

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

在JPA2.0和更高版本中,您可以简单地在列名周围加上引号,例如。

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

参考:

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

 类似资料:
  • 我的logcat消息: (1)near“INTEGER”:CREATE_SQL android.database.sqlite.sqliteexception中的语法错误异常:编译时:create BOMUL_PLACE(_id INTEGER NOT NULL主键auticrement,NAME TEXT,LAT DOUBLE,LNG DOUBLE,CHECK INTEGER DEFAULT 0

  • 我无法按照flask教程创建数据库:https://www.youtube.com/watch?v=cywidiiuxqc&list=pl-osie80tets4ujlw5mm6ojgkjfeuxcyh&index=4 我没有找到解决这个问题的办法。请帮帮忙。 当我尝试在从项目导入数据库后运行时,我得到 SqlAlchemy.exc.OperationalError:(Sqlite3.Operat

  • 在过去的几个星期里,我使用下面的gCloud命令成功地创建了dataproc集群: gcloud dataproc--区域us-east1群集创建测试1--子网默认值--区域us-east1-c--主机类型n1-standard-4--主机启动磁盘大小250--工作机数量10--工作机类型n1-standard-4--工作机启动磁盘大小200--工作机本地SSD数量1--映像版本1.2--作用域h

  • 使用此代码在Mariadb中创建表时 我得到了这个错误。 #1064年的今天,您的SQL语法出现错误;查看与您的MariaDB服务器版本对应的手册,以了解第2行“Grade TINYINT UNSIGNED,Subject VARCHAR(20),yearteached YEAR)”附近使用的正确语法 我不知道语法有什么问题。谢谢你。

  • 我从Spring Boot进行了升级 它创建序列,但不创建任何表。查看跟踪,它显示了创建表中的语法错误 SQL:SQL 语句中的语法错误(一个示例): 事实上,检查qith H2控制台或SQl linter它将“IDENTITY[*]”标记为错误,否则它将在H2中工作。 第二次尝试也是失败的。我把 并将Spring文档 https://docs.spring.io/spring-batch/doc

  • 我写了一个简单的go服务器和客户端,客户端发送两个数字,服务器用总和回复,它工作了。现在,我正在尝试使用grpc API配置设置grpc网关,并将客户端的请求从grpc更改为rest。 我正在使用本教程,在以下部分中,我无法创建gw,但没有错误: 我用了这个: 在这个问题之后,我搜索并找到了这种既不工作(没有错误也没有输出!): 我在grpc测试目录中运行它,目录树如下: 那么,我做错了什么? 编