检索数据时出现NullPointerException

时间:2016-07-26 07:03:37

标签: java hibernate session transactions

我正在尝试使用带有固定数据的hibernate来检索数据。但是可以在提交事务之前检索数据。提交事务后,我只能得到null个对象。这是我的Main.java代码:

package com.SSH.Action;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.SSH.Model.Address;
import com.SSH.Model.Contact;
import com.SSH.Model.Phone;

public class Main {
    private static SessionFactory factory; 
    public static void main(String args[])
    {
    try{
         factory = new     Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      }

    Set<Phone> phoneSet = new HashSet<Phone>();
    Phone phone= new Phone();
    phone.setAreaCode(80);
    phone.setNumber(123456);
    phoneSet.add(phone);
    Address address = new Address();
    address.setCity("City");
    address.setStreet("Street");
    address.setHomeNo("12");
    Contact contact = new Contact();
    contact.setAddress(address);
    contact.setPhone(phoneSet);

    Main me = new Main();

    me.saveContact(contact);
    me.getContact();

    }

public void saveContact(Contact contact){

    Session session = factory.openSession();
    Transaction tx = null;
    try{
        tx = session.beginTransaction();
        session.save(contact);
        System.out.println(session.get(Contact.class, new Long("1")).getAddress().getCity()); // result 'City'
        tx.commit();
    }catch(HibernateException he){
        if (tx!=null) tx.rollback();
        he.printStackTrace();
    }finally {
        session.close();
    }
 }

 public void getContact(){
    Session session = factory.openSession();
    Transaction tx = null;
    try{
        tx = session.beginTransaction();
        System.out.println(session.get(Contact.class, new Long("1"))); // result 'null'
        tx.commit();
    }catch(HibernateException he){
        if (tx!=null) tx.rollback();
        he.printStackTrace();
    }finally {
        session.close(); 
    }
   }
  }

我可以在提交事务之前获取Contact对象。我应该如何从getContact方法获取该对象。 当我使用这段代码时:

session.get(Contact.class, new Long("1")).list();

抛出这个Hibernate异常:

  

线程中的异常&#34; main&#34; java.lang.NullPointerException at   org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2159)     在org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:692)     在org.hibernate.type.EntityType.resolve(EntityType.java:434)at   org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:151)     在   org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:125)     在   org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:238)     在   org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:209)     在   org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:133)     在   org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)     在   org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)     在   org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)     在   org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4004)     在   org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)     在   org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)     在   org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)     在   org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)     在   org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)     在   org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)     在org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1213)     在   org.hibernate.internal.SessionImpl.access $ 1900年(SessionImpl.java:203)     在   org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2753)     在   org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl.load(SessionImpl.java:2727)     在org.hibernate.internal.SessionImpl.get(SessionImpl.java:1049)at   com.SSH.Action.Main.getContact(Main.java:70)at   com.SSH.Action.Main.main(Main.java:44)

0 个答案:

没有答案