hibernate中的session和get方法

时间:2012-03-28 03:28:52

标签: hibernate session hbm

我有以下项目并返回“null”..如何解决?

我的java课程:

public class UpdateExample {  
    /**
   * @param args
    */
     public static void main(String[] args) {  
         // TODO Auto-generated method stub 
         Session sess = null;  
         try {  
            SessionFactory fact = new Configuration().configure().buildSessionFactory();  
            sess = fact.openSession();  
             Transaction tr = sess.beginTransaction();  


            Insurance ins = (Insurance)sess.get(Insurance.class, new Long(1));  
           ins.setInsuranceName(2);    
           ins.setInvestementAmount(20000);  
           ins.setInvestementDate(new Date());  
           sess.update(ins);  
           tr.commit();   
           sess.close();  
           System.out.println("Update successfully!");  
        }  
        catch(Exception e){
           System.out.println("If null " + e.getMessage());  
        }  
   }  
 }  


public class Insurance {  

    private long insuranceName;  
    private double investementAmount;  
    private Date investementDate;  

    public long getInsuranceName() {  
        return insuranceName;  
     }  

    public void setInsuranceName(long insuranceName) {  
        this.insuranceName = insuranceName;
     }

    public double getInvestementAmount() {
        return investementAmount;
     }

    public void setInvestementAmount(double investementAmount) {
       this.investementAmount = investementAmount;
     }

    public Date getInvestementDate() {
       return investementDate;
    }

    public void setInvestementDate(Date investementDate) {
       this.investementDate = investementDate;
    }  
}   

insurance.hbm.xml:

 <?xml version="1.0"?>   
 <!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

<hibernate-mapping>  

    <class name="Insurance" table="Insurance">  
        <id name="insuranceName" type="long" column="InsuranceName">  
            <generator class="assigned" />  
        </id>  

        <property name="investementAmount">  
            <column name="InvestementAmount" />  

        </property>  

       <property name="investementDate">  
           <column name="InvestementDate" />  
       </property>  

    </class>  


 </hibernate-mapping>  

所以我得到的输出是:

Hibernate:选择insurance0_.InsuranceName作为Insuranc1_0_,insurance0_.InvestementAmount作为Investem2_0_0_,insurance0_.InvestementDate作为Investem3_0_0_来自Insurance insurance0_其中insurance0_.InsuranceName =?
如果为null null

请建议解决方案。

感谢
斯纳

2 个答案:

答案 0 :(得分:0)

很难说没有堆栈跟踪,但很可能

 Insurance ins = (Insurance)sess.get(Insurance.class, new Long(1));

返回null(数据库中没有这样的记录)因此下一行产生NullPointerException。我建议添加

catch(Exception e){
   System.out.println("If null " + e.getMessage());  
    e.printStackTrace();
}  

到catch子句以查看NPE发生的位置。

答案 1 :(得分:0)

我建议进行一些修改

更改类型:

<id name="insuranceName" type="java.lang.Long" column="InsuranceName">  
      <generator class="assigned" />  
</id>

在保险类(POJO)

private Long insuranceName; 

试试这个。

更新:我尝试了你的映射工作,确保你有id 1和hibernate.cfg.xml的保险数据是正确的。