Person.java和PersonId.java是myeclipse6.0自动生成的俩文件就是getter和setter方法们(Person.java啥用哦)
自己编写的PersonOperator类
package com.abing.hp.hibernate;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class PersonOperator {
Session session = null;
public void PersonOperator()
{
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
this.session = factory.openSession();
}
public void insert(Person p)
{
Transaction tran = this.session.beginTransaction();
this.session.save(p);
tran.commit();
this.session.close();
}
}
自己编写的测试类testHibernate.java
package com.abing.hp.hibernate;public class TestHibernate {
public static void main(String[] args) {
PersonId pid = new PersonId();
Person p = new Person();
pid.setId("hello");
pid.setName("world");
pid.setPassword("123");
pid.setEmail("[email protected]");
p.setId(pid);
PersonOperator po = new PersonOperator();
po.insert(p); }}
Person.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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.abing.hp.hibernate.Person" table="person" catalog="test">
        <composite-id name="id" class="com.abing.hp.hibernate.PersonId">
            <key-property name="id" type="string">
                <column name="id" length="32" />
            </key-property>
            <key-property name="name" type="string">
                <column name="name" length="20" />
            </key-property>
            <key-property name="password" type="string">
                <column name="password" length="30" />
            </key-property>
            <key-property name="email" type="string">
                <column name="email" length="50" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>
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"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration> <session-factory>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/abing/hp/hibernate/Person.hbm.xml" /> </session-factory></hibernate-configuration>
异常
Exception in thread "main" java.lang.NullPointerException
at com.abing.hp.hibernate.PersonOperator.insert(PersonOperator.java:18)
at com.abing.hp.hibernate.TestHibernate.main(TestHibernate.java:15)
麻烦各位!

解决方案 »

  1.   

    我看了下你的代码
    p.setId(pid); 
    PersonOperator   po   =   new   PersonOperator(); 
    po.insert(p); 
    你在this.session.save(p); 这里只把pid保存了  不知道是不是这的问题```你这样写 Person   p   =   new   Person(); 
    p.setId("hello"); 
    p.setName("world"); 
    p.setPassword("123"); 
    p.setEmail("[email protected]"); 
    PersonOperator   po   =   new   PersonOperator(); 
    po.insert(p); 你试一下  看能不能行```
      

  2.   

    composite-id   
    id,name,password,email都得有值,你只给p.setId(pid); 其它的没有给值.
      

  3.   

    回diy8187
    这个类只有id属性