我的hibernate配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/mysample
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">
True
</property>
<property name="hibernate.use_outer_join">
True
</property>
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<mapping resource="org/redsaga/quickstart/Tuser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我的JUnit测试用例:
import java.util.List;
import junit.framework.Assert;
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class HibernateTest extends TestCase {
Session session=null;
protected void setUp()
{
try
{
Configuration config=new Configuration().configure();
SessionFactory sessionFactory=config.buildSessionFactory();
session=sessionFactory.openSession();
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
protected void tearDown()
{
try
{
session.close();
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
public void testInsert()
{
Transaction tran=null;
try
{
tran=session.beginTransaction();
Tuser user=new Tuser();
user.setName("Emma");
session.save(user);
session.flush();
tran.commit();
Assert.assertEquals(user.getId().intValue()>0,true);
}
catch(HibernateException e)
{
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran!=null)
{
try
{
tran.rollback();
}
catch(HibernateException e1)
{
e1.printStackTrace();
}
}
}
}
public void testSelect()
{
String hql="from Tuser where name='Emma'";
try
{
List userList=session.createQuery(hql).list();
Tuser user=(Tuser)userList.get(0);
Assert.assertEquals(user.getName(),"Emma");
}
catch(HibernateException e)
{
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}
已将MySQL驱动类包添加到工程类库,测试HibernateTest时出现异常net.sf.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver
检查了一下,是session=sessionFactory.openSession();的问题,难道是我的驱动类包的问题?
请高手指教,不用感激!!
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/mysample
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">
True
</property>
<property name="hibernate.use_outer_join">
True
</property>
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<mapping resource="org/redsaga/quickstart/Tuser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我的JUnit测试用例:
import java.util.List;
import junit.framework.Assert;
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class HibernateTest extends TestCase {
Session session=null;
protected void setUp()
{
try
{
Configuration config=new Configuration().configure();
SessionFactory sessionFactory=config.buildSessionFactory();
session=sessionFactory.openSession();
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
protected void tearDown()
{
try
{
session.close();
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
public void testInsert()
{
Transaction tran=null;
try
{
tran=session.beginTransaction();
Tuser user=new Tuser();
user.setName("Emma");
session.save(user);
session.flush();
tran.commit();
Assert.assertEquals(user.getId().intValue()>0,true);
}
catch(HibernateException e)
{
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran!=null)
{
try
{
tran.rollback();
}
catch(HibernateException e1)
{
e1.printStackTrace();
}
}
}
}
public void testSelect()
{
String hql="from Tuser where name='Emma'";
try
{
List userList=session.createQuery(hql).list();
Tuser user=(Tuser)userList.get(0);
Assert.assertEquals(user.getName(),"Emma");
}
catch(HibernateException e)
{
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}
已将MySQL驱动类包添加到工程类库,测试HibernateTest时出现异常net.sf.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver
检查了一下,是session=sessionFactory.openSession();的问题,难道是我的驱动类包的问题?
请高手指教,不用感激!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货