hibernate遇到问题(mysql数据库):
alter table user drop foreign key FK36EBCBF0AE61AB
drop table if exists group
drop table if exists user
create table group (ID integer not null auto_increment, name varchar(255), primary key (ID))
14:31:33,950 ERROR SchemaExport:386 - Unsuccessful: create table group (ID integer not null auto_increment, name varchar(255), primary key (ID))
14:31:33,954 ERROR SchemaExport:387 - 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 'group (ID integer not null auto_increment, name varchar(255), primary key (ID))' at line 1
create table user (id bigint not null, name varchar(255), primary key (id))
alter table user add index FK36EBCBF0AE61AB (id), add constraint FK36EBCBF0AE61AB foreign key (id) references group (ID)
14:31:34,081 ERROR SchemaExport:386 - Unsuccessful: alter table user add index FK36EBCBF0AE61AB (id), add constraint FK36EBCBF0AE61AB foreign key (id) references group (ID)
14:31:34,082 ERROR SchemaExport:387 - 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 'group (ID)' at line 1
下面是全部代码:
持久类:
Group.java:
package com.test;
public class Group {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
User.java:
package com.test;
public class User {
private int id;
private String name;
private Group group;
public User(){}
public User(int id,String name){
this.id=id;
this.name=name;
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
Group.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test">
<class name="com.test.Group" table="group">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test">
<class name="com.test.User" table="user">
<id name="id" column="id">
<generator class="foreign">
<param name="property">group</param>
</generator>
</id>
<property name="name" column="name"/>
<one-to-one name="group" class="com.test.Group" constrained="true"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/secondHibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">kismet</property>
<!-- 在控制台显示SQL语句 -->
<property name="show_sql">true</property>
<!-- 配置数据库连接池C3P0 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">2</property>
<!-- 表示数据库连接对象经过多长时间没有使用就过期(s) -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 预编译的statement(preparedStatement)的数量 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 每隔多长时间检查连接池里的空闲连接(s) -->
<property name="hibernate.c3p0.idle_test_period">600</property>
<!-- 连接池的连接用完后,每次新建连接的数量 -->
<property name="hibernate.c3p0.acquire_increment">3</property>
<!-- 每个线程只有一个色素色素session实例 -->
<property name="hibernate.current_session_context_class" >thread</property>
<mapping resource="com/test/Group.hbm.xml"/>
<mapping resource="com/test/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Java执行:
package DAO;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportSchema {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
se.create(true, true);
}
}
package DAO;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sf;
static {
try {
Configuration cfg = new Configuration();
cfg.configure();
sf = cfg.buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
System.out.println("SessionFactory创建失败");
e.printStackTrace();
throw new ExceptionInInitializerError();
}
}
public static SessionFactory getSessionFactory(){
return sf;
}
}
------解决思路----------------------
group是MySQL的关键词,换一个试试