[英]Problems with creating MySQL table with hibernate
At first I try to create table with names USERS or USER, but google said me that it is a reserved words in MySQL and I try with QWERTY, the table is not created anyway.起初我尝试创建名称为 USERS 或 USER 的表,但谷歌告诉我这是 MySQL 中的保留字,我尝试使用 QWERTY,无论如何都没有创建该表。
Entity Class
实体类
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...
}
Creating table
创建表
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);
}
}
Console output
控制台输出
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
The logging shows that you are not setting the correct dialect ( Using dialect: org.hibernate.dialect.HSQLDialect
).日志显示您没有设置正确的方言(
Using dialect: org.hibernate.dialect.HSQLDialect
)。 So, you have to set the correct dialect: org.hibernate.dialect.MySqlDialect
.因此,您必须设置正确的方言:
org.hibernate.dialect.MySqlDialect
。 Then Hibernate will generate the correct statements for MySQL.然后 Hibernate 会为 MySQL 生成正确的语句。
Mysql doesnt have native boolean datatype - tinyint(1) is used as boolean. Mysql 没有本机布尔数据类型 - tinyint(1) 用作布尔值。 Annotate accountIsNotLocked as below
注释 accountIsNotLocked 如下
@Type(type = "org.hibernate.type.NumericBooleanType")
protected boolean accountIsNotLocked;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.