读书人

hibernate连接mysql中的有关问题

发布时间: 2011-12-25 23:21:20 作者: rapoo

hibernate连接mysql中的问题
程序是一个实现向建好的MySQL数据库里加入一条记录,但是出现下面的错误信息,请高手指教一下,谢谢


log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into myusertable (username, password, email, id) values (?, ?, ?, ?)
org.hibernate.exception.GenericJDBCException: could not insert: [hyq03.hibernate.User]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at hyq03.hibernate.Test.main(Test.java:21)
Caused by: java.sql.SQLException: Statement parameter 4 not set.
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1031)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:676)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 16 more
配置信息hibernate.cfg.xml如下:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate configuration DTD 3.0//EN "
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd " >



<hibernate-configuration>
<session-factory>

<property name= "connection.driver_class "> com.mysql.jdbc.Driver </property>
<property name= "connection.url "> jdbc:mysql://localhost:3306/MyProject </property>
<property name= "connection.username "> root </property>
<property name= "connection.password "> root </property>

<property name= "connection.pool_size "> 1 </property>

<property name= "dialect "> org.hibernate.dialect.MySQLDialect </property>

<property name= "current_session_context_class "> thread </property>

<property name= "cache.provider_class "> org.hibernate.cache.NoCacheProvider </property>

<property name= "show_sql "> true </property>

<property name= "hbm2ddl.auto "> create </property>

<mapping resource= "hyq03/hibernate/User.hbm.xml "/>
</session-factory>
</hibernate-configuration>
配置信息user.hbm.xml如下:
<?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 " >

<hibernate-mapping>

<class name= "hyq03.hibernate.User " table= "myusertable ">
<id name= "id ">
<generator class= "identity "/>
</id>
<property name= "username "/>
<property name= "password "/>
<property name= "email "/>
</class>

</hibernate-mapping>
测试程序test.java如下:
package hyq03.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test
{
public static void main(String[] args)
{
try{
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
User user=new User();
//user.setId(2);
user.setUsername( "hibernate ");
user.setPassword( "123 ");
//user.setEmail( "hibernate@hbnt ");
session.save(user);
tx.commit();


session.close();
}
catch(HibernateException e)
{
e.printStackTrace();
}
}

}


[解决办法]
把建表的脚本贴出来看看

读书人网 >Eclipse开发

热点推荐