是hibernate上的一个小例子。
PersonModel.hbm.xml
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="person.PersonModel" table="zy_person">
<!--hibernate为我们生成主键id-->
<id name="id" type="long">
<generator class="identity"/>
</id>
<property name="name" length="255" type="string"/>
<property name="address" length="255" type="string"/>
</class>
</hibernate-mapping>TestPersonModel2.java
package person;import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;public class TestPersonModel2 {
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception{
Configuration conf= new Configuration().addClass(PersonModel.class); //在表中插入第一条数据 sessionFactory = conf.buildSessionFactory();
Session s = sessionFactory.openSession(); Transaction t = s.beginTransaction(); PersonModel p1=new PersonModel();
p1.setName("robin");
p1.setAddress("上海"); Transaction tx = null;
try {
tx = s.beginTransaction();
s.save(p1);
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
} finally {
s.close();
}
}
}
PersonModel.hbm.xml
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="person.PersonModel" table="zy_person">
<!--hibernate为我们生成主键id-->
<id name="id" type="long">
<generator class="identity"/>
</id>
<property name="name" length="255" type="string"/>
<property name="address" length="255" type="string"/>
</class>
</hibernate-mapping>TestPersonModel2.java
package person;import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;public class TestPersonModel2 {
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception{
Configuration conf= new Configuration().addClass(PersonModel.class); //在表中插入第一条数据 sessionFactory = conf.buildSessionFactory();
Session s = sessionFactory.openSession(); Transaction t = s.beginTransaction(); PersonModel p1=new PersonModel();
p1.setName("robin");
p1.setAddress("上海"); Transaction tx = null;
try {
tx = s.beginTransaction();
s.save(p1);
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
} finally {
s.close();
}
}
}
这一句为什么不写成SessionFactory sessionFactory = conf.buildSessionFactory();前面的可以省略吗?
SessionFactory sessionFactory = conf.buildSessionFactory();袄。
从抱的错来看不是,insert时的错,好象你的初始话都没有成功,,,在看看你的配置文件
这句是多余的,去掉试试。
/**
* Thrown when an application tries to call an abstract method.
* Normally, this error is caught by the compiler; this error can
* only occur at run time if the definition of some class has
* incompatibly changed since the currently executing method was last
* compiled.
*
* @author unascribed
* @version 1.17, 01/23/03
* @since JDK1.0
*/
public
class AbstractMethodError extends IncompatibleClassChangeError {一般在编译的时候会由编译器抛出.
在运行中抛出此异常当且仅当方法在编译之后某些类发生了不兼容的改变.猜测你的hibernate包和mysql驱动包不兼容