日期:2014-05-18  浏览次数:20675 次

hibernate开发 空指针问题
在进行many-to-many映射的时候,提示为空指针。
配置为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 "   >
<hibernate-mapping>
<class   name= "com.cqmc.Person "   table   = "PERSON ">
<id   name   = "id "   column   = "PERSON_ID ">
<generator   class= "increment "> </generator>
</id>
<property   name= "age "   column= "AGE "> </property>
<property   name= "firstName "   column= "FIRSTNAME "> </property>
<property   name= "lastName "   column= "LASTNAME "> </property>
<set   name= "events "   table   = "PERSON_EVENT ">
<key   column= "PERSON_ID "/>
<many-to-many   column= "EVENT_ID "   class= "com.cqmc.Event "/>
</set>
</class>
</hibernate-mapping>
CODE:
/**
  *   Created   on   2007-1-9   17:00:00
  *
  *   @author   fanxue.bu
  *
  *   @company   Chongqing   Justin   Mobile   Business   Co.,Ltd.
  *
  */
package   com.cqmc;
import   java.util.*;
public   class   Person   {
private   Set   events   =   new   HashSet();
private   long   id;
private   Integer   age;
private   String   firstName;
private   String   lastName;
public   Person(){
}
public   void   setEvents(Set   events){
this.events   =events;
}
public   Set   getEvents(){
return   this.events;
}
private   void   setId(long   id){
this.id   =   id   ;
}
public   long   getId(){
return   this.id   ;
}
public   void   setAge(Integer   age){
this.age   =   age;  
}
public   Integer   getAge(){
return   this.age;
}
public   void   setFirstName(String   firstName){
this.firstName   =   firstName;
}
public   String   getFirstName(){
return   this.firstName;
}
public   void   setlastName(String   lastName){
this.lastName   =   lastName;
}
public   String   getlastName(){
return   this.lastName;
}

}
主程序代码:
package   com.cqmc;

import   org.hibernate.*;
import   java.util.Date;
import   util.HibernateUtil;
import   java.util.*;
import   java.text.*;
import   org.hibernate.cfg.*;
public   class   EventManager   {
public   static   void   main(String[]   args)throws   Exception{
EventManager   mgr   =   new   EventManager();
//mgr.deleteDate();
try{
Long   personId   =mgr.createAndStorePerso