使用Hibernate自动生成数据库的功能只需要导好Hibernate的jar包,然后写好POJO实体类和ORM映射文件,在hibernate.cfg.xml里配好,然后写一个方法执行增删查改的任意一个操作就可以使Hibernate自动为你生成所有的映射好的表,但是我今天碰到一个很BT的现象,就是我的六个POJO类和ORM类都正确的写好了,然后写了一个测试DAO类(这个类只是测试了其中一个POJO类的存取操作),执行后发现对该POJO类的操作正常,可是只生成了5个表,还有一个表(t_message)没生成,也没报错。
我觉得很奇怪,仔细的对比了t_message的映射文件和其他的映射文件,没有发现什么奇怪的地方,然后我又写了一个测试Message类(与t_message对应的)的DAO类,用以测试插入操作,执行发现报错,报错内容如下:
Hibernate: insert into t_message (title, content, sender, receiver, sendDate, read) values (?, ?, ?, ?, ?, ?)
org.hibernate.exception.SQLGrammarException: could not insert: [com.gmha.entity.Message]
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:2186)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666)
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:562)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
at com.gmha.dao.HibernateUtil.save(HibernateUtil.java:58)
at com.gmha.test.TestForMessageDAO.main(TestForMessageDAO.java:18)
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 'write) values ('title', 'content', 'sender', 'receiver', '2010-04-03 17:18:24', ' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2934)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3255)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1566)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1480)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1465)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 17 more
这说明还是这个t_message的映射文件写得有问题,*无奈,我只有一行一行的去掉映射文件里的属性映射,发现由一个名为read的属性去掉后就可以生成表,运行也不报错了,然后我把read改为了isRead,结果还是不能生成数据库表,运行还是报上面的错。
经反复测试,有如下总结:
1. POJO类的属性不能取名为read和write,否则都会出现不能生成相应的数据库表和运行出错的现象。(我以为:这两个可能是Hibernate屏蔽的两个关键字)
2. POJO类的boolean类型的属性名不能以is为前缀,否则Hibernate会报找不到getter方法的异常。(我以为:如果属性为boolean类型,Eclipse生成getter方法时就是以is开头的,比如:boolean isRead;生成的getter方法名为:isRead,这时Hibernate想要找的则是isIsRead方法,所以就找不到了。)
就我的情况,解决办法就是把名为read的属性换成一个不是read/write且不以is为前缀的单词即可。(当然POJO里和ORM都要相应的改)