2013-11-10 124 views
1

起初我嘗試創建名稱爲USERS或USER的表,但是Google說我是MySQL中的保留字,並且我嘗試使用QWERTY,但表不是以任何方式創建的。休眠時創建MySQL表的問題

實體類

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="QWERTY") 
public abstract class User { 
    @Id 
    @GeneratedValue 
    protected Long id; 
    protected String username; 
    protected String firstname; 
    protected String secondname; 
    protected String middlename; 
    protected String password; 

    @Type(type = "org.hibernate.type.NumericBooleanType") 
    protected Boolean accountIsNotLocked; 

    public User(){ 
    } 

    // getters and setters... 
} 

創建表

import org.hibernate.cfg.AnnotationConfiguration; 
import org.hibernate.tool.hbm2ddl.SchemaExport; 

import by.grsu.epam.domain.User; 

public class TestUser { 

    public static void main(String[] args) { 
     AnnotationConfiguration config = new AnnotationConfiguration(); 
     config.addAnnotatedClass(User.class); 
     config.configure(); 

     new SchemaExport(config).create(true, true); 
    } 

} 

控制檯輸出

INFO : org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 
INFO : org.hibernate.Version - HHH000412: Hibernate Core {4.2.7.Final} 
INFO : org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found 
INFO : org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist 
INFO : org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: /hibernate.cfg.xml 
INFO : org.hibernate.cfg.Configuration - HHH000040: Configuration resource: /hibernate.cfg.xml 
INFO : org.hibernate.cfg.Configuration - HHH000041: Configured SessionFactory: null 
INFO : org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect 
INFO : org.hibernate.tool.hbm2ddl.SchemaExport - HHH000227: Running hbm2ddl schema export 
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000402: Using Hibernate built-in connection pool (not for production use!) 
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 2 
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000006: Autocommit mode: false 
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/MySQL] 
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000046: Connection properties: {user=root, password=****} 

    drop table QWERTY if exists 
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: drop table QWERTY if exists 
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - 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 'if exists' at line 1 

    create table QWERTY (
     id bigint generated by default as identity (start with 1), 
     accountIsNotLocked integer, 
     firstname varchar(255), 
     middlename varchar(255), 
     password varchar(255), 
     secondname varchar(255), 
     username varchar(255), 
     primary key (id) 
    ) 
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: create table QWERTY (id bigint generated by default as identity (start with 1), accountIsNotLocked integer, firstname varchar(255), middlename varchar(255), password varchar(255), secondname varchar(255), username varchar(255), primary key (id)) 
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - 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 'generated by default as identity (start with 1), 
     accountIsNotLocked inte' at line 2 
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/MySQL] 
INFO : org.hibernate.tool.hbm2ddl.SchemaExport - HHH000230: Schema export complete 

回答

3

的記錄顯示你沒有設置正確的話(Using dialect: org.hibernate.dialect.HSQLDialect)。所以,你必須設置正確的方言:org.hibernate.dialect.MySqlDialect。然後Hibernate會爲MySQL生成正確的語句。

+0

你節省了我的時間,直到SpringBoot出現問題的人! – sunleo

0

Mysql的可是沒有吶tive布爾型數據類型 - tinyint(1)用作布爾值。 註釋accountIsNotLocked如下

@Type(type = "org.hibernate.type.NumericBooleanType") 
protected boolean accountIsNotLocked; 
+0

仍然不起作用 –

+0

與之前相同的錯誤? – Sanj

+0

我有編輯實體類和控制檯輸出問題。 –