0

我有以下项目及其返回“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;
    }  
}   

保险.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>  

所以我得到的输出是:

休眠:从保险 insurance0_ 中选择 insurance0_.InsuranceName 作为 Insurance1_0_,insurance0_.InvestementAmount 作为 Investem2_0_0_,insurance0_.InvestementDate 作为 Investem3_0_0_,其中 insurance0_.InsuranceName=?
如果为空 null

请提出解决方案。

谢谢斯内哈
_

4

2 回答 2

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 发生的位置。

于 2012-03-28T03:42:42.530 回答
0

我建议进行一些更改

变更类型:

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

并在保险类(POJO)

private Long insuranceName; 

尝试这个。

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

于 2012-03-28T05:02:28.673 回答