CREATE TABLE `admin` (
`CID` int(8) NOT NULL default '0',
`UserName` varchar(20) collate latin1_general_ci NOT NULL,
`Password` varchar(32) collate latin1_general_ci NOT NULL,
`UserClass` varchar(32) collate latin1_general_ci NOT NULL,
`LastDate` datetime default NULL,
`LastIP` varchar(15) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`CID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
配置文件
...
<class name="Admin" table="admin">
<id name="cid" column="CID" type="java.lang.Integer">
<generator class="hilo"/>
</id>
...
hibernate.cfg.xml
...
<session-factory>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">jdbc:mysql://localhost:3306/news</property>
<property name="connection.username">root</property>
<property name="connection.password">5487635104</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/news/struts/bean/Admin.hbm.xml"></mapping>
<mapping resource="com/news/struts/bean/Adminclass.hbm.xml" />
<mapping resource="com/news/struts/bean/Newclass.hbm.xml" />
<mapping resource="com/news/struts/bean/News.hbm.xml" />
<mapping resource="com/news/struts/bean/Review.hbm.xml" />
</session-factory>
...
//Test.java
package com.news.hibernate;
import org.apache.log4j.PropertyConfigurator;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.news.struts.bean.Admin;
public class Test {
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
Transaction tra = sess.beginTransaction();
Admin admin = new Admin();
admin.setLastip("100.100.100.100");
admin.setPassword("123456");
admin.setUsername("cq");
admin.setUserclass("0");
sess.save(admin);
tra.commit();
sess.close();
}
}
错误信息:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not get or update next value
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(TransactionHelper.java:85)
at org.hibernate.id.TableGenerator.generate(TableGenerator.java:94)
at org.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:61)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:85)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at com.news.hibernate.Test.main(Test.java:29)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'news.hibernate_unique_key' doesn't exist
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2822)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1536)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1626)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3031)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:943)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1049)
at org.hibernate.id.TableGenerator.doWorkInCurrentTransaction(TableGenerator.java:133)
at org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(TransactionHelper.java:64)
... 11 more
知道的给小弟指点一下哈!
6 个解决方案
#1
因为Hibernate Tools生成*.hbn.xml工具中包含有catalog="***"(*代表数据库名称)这样的属性,将该属性删除就可以了。
#2
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Sat Jul 15 16:31:42 CST 2006 -->
<hibernate-mapping package="com.news.struts.bean">
<class name="Admin" table="admin">
<id name="cid" column="CID" type="java.lang.Integer">
<generator class="hilo"/>
</id>
<property name="username" column="UserName" type="java.lang.String" not-null="true" />
<property name="password" column="Password" type="java.lang.String" not-null="true" />
<property name="userclass" column="UserClass" type="java.lang.String" not-null="true" />
<property name="lastdate" column="LastDate" type="java.util.Date" />
<property name="lastip" column="LastIP" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>
这是全部内容,没看到楼上的所说的catalog="***"啊!
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Sat Jul 15 16:31:42 CST 2006 -->
<hibernate-mapping package="com.news.struts.bean">
<class name="Admin" table="admin">
<id name="cid" column="CID" type="java.lang.Integer">
<generator class="hilo"/>
</id>
<property name="username" column="UserName" type="java.lang.String" not-null="true" />
<property name="password" column="Password" type="java.lang.String" not-null="true" />
<property name="userclass" column="UserClass" type="java.lang.String" not-null="true" />
<property name="lastdate" column="LastDate" type="java.util.Date" />
<property name="lastip" column="LastIP" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>
这是全部内容,没看到楼上的所说的catalog="***"啊!
#3
自己顶起
#4
帮帮忙啊!
在线等!
在线等!
#5
应该是你主键的生成方式不对.用自增的ID就可以了
#6
你的"hilo"这里出问题了吧,看一下主键生成方法,hilo好象是hibernate维护吧,所以需要一张hibernate_unique_key表。
#1
因为Hibernate Tools生成*.hbn.xml工具中包含有catalog="***"(*代表数据库名称)这样的属性,将该属性删除就可以了。
#2
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Sat Jul 15 16:31:42 CST 2006 -->
<hibernate-mapping package="com.news.struts.bean">
<class name="Admin" table="admin">
<id name="cid" column="CID" type="java.lang.Integer">
<generator class="hilo"/>
</id>
<property name="username" column="UserName" type="java.lang.String" not-null="true" />
<property name="password" column="Password" type="java.lang.String" not-null="true" />
<property name="userclass" column="UserClass" type="java.lang.String" not-null="true" />
<property name="lastdate" column="LastDate" type="java.util.Date" />
<property name="lastip" column="LastIP" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>
这是全部内容,没看到楼上的所说的catalog="***"啊!
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Sat Jul 15 16:31:42 CST 2006 -->
<hibernate-mapping package="com.news.struts.bean">
<class name="Admin" table="admin">
<id name="cid" column="CID" type="java.lang.Integer">
<generator class="hilo"/>
</id>
<property name="username" column="UserName" type="java.lang.String" not-null="true" />
<property name="password" column="Password" type="java.lang.String" not-null="true" />
<property name="userclass" column="UserClass" type="java.lang.String" not-null="true" />
<property name="lastdate" column="LastDate" type="java.util.Date" />
<property name="lastip" column="LastIP" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>
这是全部内容,没看到楼上的所说的catalog="***"啊!
#3
自己顶起
#4
帮帮忙啊!
在线等!
在线等!
#5
应该是你主键的生成方式不对.用自增的ID就可以了
#6
你的"hilo"这里出问题了吧,看一下主键生成方法,hilo好象是hibernate维护吧,所以需要一张hibernate_unique_key表。