测试hibernate小程序,所有jar包、xml配置都搞定,然后先测试一个teacher表。源码如下:
-----------------------------------------
Teacher.java按照最简单的写法如下:@Entity
public class Teacher {
private int id;
private String name; @Id
@GeneratedValue
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;
}}测试类如下:
public class TeacherTest { private Session session= null;
@Before
public void before(){
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
session = sf.getCurrentSession();
session.getTransaction().begin();
}
@Test
public void test01(){
Teacher t = new Teacher();
t.setName("t2");
session.save(t);
}
@After
public void after(){
session.getTransaction().commit();
}
}
-------------------------------------------测试test01()这个方法,成功,检查数据库,确实建表成功,数据也插入。
然后,诡异的现象出现了。。我再建立一个文件,Group.java 里面内容和Teacher.java完全一样,意思就是再建
立一个表user。然后我又写了个测试方法test02(),如下: @Test
public void test02(){
Group group = new Group();
group.setName("china");
session.save(group);
}Junit测试 test02(),结果报错了报错信息如下:org.hibernate.exception.SQLGrammarException: Could not execute
JDBC batch update at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverte
r.java:90)
.
.
.
.
Caused by: java.sql.BatchUpdateException: 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 (name, id)
values ('china', 1)' at line 1
at
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStat
ement.java:2054)
at .
.
.
.
.我百思不得其解,到底哪里错了?两个表结构一样,测试方法一样,为什么第一个可以
执行,第二个就不可以执行了?????????求大神指教!!!拜谢!!!!!
-----------------------------------------
Teacher.java按照最简单的写法如下:@Entity
public class Teacher {
private int id;
private String name; @Id
@GeneratedValue
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;
}}测试类如下:
public class TeacherTest { private Session session= null;
@Before
public void before(){
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
session = sf.getCurrentSession();
session.getTransaction().begin();
}
@Test
public void test01(){
Teacher t = new Teacher();
t.setName("t2");
session.save(t);
}
@After
public void after(){
session.getTransaction().commit();
}
}
-------------------------------------------测试test01()这个方法,成功,检查数据库,确实建表成功,数据也插入。
然后,诡异的现象出现了。。我再建立一个文件,Group.java 里面内容和Teacher.java完全一样,意思就是再建
立一个表user。然后我又写了个测试方法test02(),如下: @Test
public void test02(){
Group group = new Group();
group.setName("china");
session.save(group);
}Junit测试 test02(),结果报错了报错信息如下:org.hibernate.exception.SQLGrammarException: Could not execute
JDBC batch update at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverte
r.java:90)
.
.
.
.
Caused by: java.sql.BatchUpdateException: 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 (name, id)
values ('china', 1)' at line 1
at
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStat
ement.java:2054)
at .
.
.
.
.我百思不得其解,到底哪里错了?两个表结构一样,测试方法一样,为什么第一个可以
执行,第二个就不可以执行了?????????求大神指教!!!拜谢!!!!!
SQL 语法错误
调整下配置文件
把SQL语句打出来看看
就知道错在哪了
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.bjsxt.hibernate.Group]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy6.save(Unknown Source)
at com.bjsxt.hibernate.Grouptest.main(Grouptest.java:16)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 (name) values ('china')' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 22 more
不能用这个 用别名