最后是我的测试类
public class MyTest { /**
* @param args
*/
private Session session;
private Logger log;
public static void main(String[] args) { MyTest myTest = new MyTest();
try{
myTest.setUp();
myTest.testCreateDomains();
myTest.testAddRemoveRelation();
//myTest.listBooks();
myTest.tearDown();
}catch(Exception e){
e.printStackTrace();
}
}
private void testCreateDomains(){
Transaction tx = session.beginTransaction();
Book book = new Book();
book.setAuthor("Sebastian");
book.setTitle("Hibernate in Winter");
book.setBorrowallowed(new Short((short)1));
session.save(book);
tx.commit();
tx = session.beginTransaction();
Customer customer = new Customer();
customer.setFirstname("Carsten");
customer.setLastname("Liebzeit");
customer.setAge(new Integer(25));
session.save(customer);
tx.commit();
}
private void testAddRemoveRelation(){
log.info("beging to adding...");
Transaction tx = session.beginTransaction();
Book book = new Book();
book.setAuthor("Sebastian");
book.setTitle("Hibernate in the Summer");
book.setBorrowallowed(new Short((short)1));
session.save(book);
Book book2 = new Book();
book2.setAuthor("Carsten");
book2.setTitle("wildes Kurditain");
book2.setBorrowallowed(new Short((short)1));
session.save(book2);
Customer customer = new Customer();
customer.setLastname("Meier");
customer.setFirstname("John");
customer.setAge(new Integer(26));
session.save(customer);
//customer borrows book
customer.getBookSet().add(book);
customer.getBookSet().add(book2);
session.flush();
session.refresh(customer);
session.refresh(book);
session.refresh(book2);
Set books = customer.getBookSet();
log.info("list books of customer");
for(Iterator it = books.iterator();it.hasNext();){
Book element = (Book)it.next();
log.info(element);
}
//first book is returned
book.setCustomer(null);
customer.getBookSet().remove(book);
System.out.println("00000");
session.flush();
session.refresh(customer);
log.info("list books of customer");
books = customer.getBookSet();
for(Iterator it = books.iterator();it.hasNext();){
Book element = (Book)it.next();
System.out.println(element.getAuthor());
log.info(element);
}
tx.commit();
tx = session.beginTransaction();
session.delete(customer);
session.delete(book);
session.delete(book2);
tx.commit();
}
public void setUp(){
session = HibernateSessionFactory.currentSession();
log = Logger.getLogger(this.getClass());
}
public void tearDown(){
HibernateSessionFactory.closeSession();
}}
当执行改类时,第一个方法能成功增加记录,但执行第二个方法时就报错了java.lang.NullPointerException
at com.myapp.test.MyTest.testAddRemoveRelation(MyTest.java:74)
at com.myapp.test.MyTest.main(MyTest.java:27)
不知道为什么?
各位大侠帮小弟看看,谢谢.
public class MyTest { /**
* @param args
*/
private Session session;
private Logger log;
public static void main(String[] args) { MyTest myTest = new MyTest();
try{
myTest.setUp();
myTest.testCreateDomains();
myTest.testAddRemoveRelation();
//myTest.listBooks();
myTest.tearDown();
}catch(Exception e){
e.printStackTrace();
}
}
private void testCreateDomains(){
Transaction tx = session.beginTransaction();
Book book = new Book();
book.setAuthor("Sebastian");
book.setTitle("Hibernate in Winter");
book.setBorrowallowed(new Short((short)1));
session.save(book);
tx.commit();
tx = session.beginTransaction();
Customer customer = new Customer();
customer.setFirstname("Carsten");
customer.setLastname("Liebzeit");
customer.setAge(new Integer(25));
session.save(customer);
tx.commit();
}
private void testAddRemoveRelation(){
log.info("beging to adding...");
Transaction tx = session.beginTransaction();
Book book = new Book();
book.setAuthor("Sebastian");
book.setTitle("Hibernate in the Summer");
book.setBorrowallowed(new Short((short)1));
session.save(book);
Book book2 = new Book();
book2.setAuthor("Carsten");
book2.setTitle("wildes Kurditain");
book2.setBorrowallowed(new Short((short)1));
session.save(book2);
Customer customer = new Customer();
customer.setLastname("Meier");
customer.setFirstname("John");
customer.setAge(new Integer(26));
session.save(customer);
//customer borrows book
customer.getBookSet().add(book);
customer.getBookSet().add(book2);
session.flush();
session.refresh(customer);
session.refresh(book);
session.refresh(book2);
Set books = customer.getBookSet();
log.info("list books of customer");
for(Iterator it = books.iterator();it.hasNext();){
Book element = (Book)it.next();
log.info(element);
}
//first book is returned
book.setCustomer(null);
customer.getBookSet().remove(book);
System.out.println("00000");
session.flush();
session.refresh(customer);
log.info("list books of customer");
books = customer.getBookSet();
for(Iterator it = books.iterator();it.hasNext();){
Book element = (Book)it.next();
System.out.println(element.getAuthor());
log.info(element);
}
tx.commit();
tx = session.beginTransaction();
session.delete(customer);
session.delete(book);
session.delete(book2);
tx.commit();
}
public void setUp(){
session = HibernateSessionFactory.currentSession();
log = Logger.getLogger(this.getClass());
}
public void tearDown(){
HibernateSessionFactory.closeSession();
}}
当执行改类时,第一个方法能成功增加记录,但执行第二个方法时就报错了java.lang.NullPointerException
at com.myapp.test.MyTest.testAddRemoveRelation(MyTest.java:74)
at com.myapp.test.MyTest.main(MyTest.java:27)
不知道为什么?
各位大侠帮小弟看看,谢谢.
MyTest类中的这行customer.getBookSet().add(book);
我看很多例子这样可以的,而且我屏蔽掉这两行,再往下执行到customer.getBookSet().remove(book);的时候就不报错.实在不明白
这一行在什么地方,你贴出来
at com.myapp.test.MyTest.testAddRemoveRelation(MyTest.java:74)我猜是这一行吧,customer.getBookSet().add(book);
你的Customer类里的面的 bookSet,
有没有初始化啊?
有没有给他赋一个空的HashSet啊?
应该是private Set bookSet=new HashSet();
估计你写成了private Set bookSet;
如果不是这样等我没有说,
只能怪你把错误在哪行,更详细的错误不告诉我,我只能猜是这些问题
customer.getBookSet().remove(book);
可以用,是因为你在getBookSet()之前,已经把他查出来了,已经不是null了
不尽量多贴出来,怕大家不知道问题所在啊.
还有我已经把at com.myapp.test.MyTest.testAddRemoveRelation(MyTest.java:74)这行贴出来了 "忘了说了,提示at com.myapp.test.MyTest.testAddRemoveRelation(MyTest.java:74)指的是MyTest类中的这行customer.getBookSet().add(book);"
只是你没注意看而已.
不过还是要谢谢你!