2012-03-28 5 views
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; 
    } 
} 

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> 

だから私が得る出力は次のようになります。

休止状態:Insuranc1_0_、insurance0_としてinsurance0_.InsuranceNameを選択します。 Investem2_0_0_、保険0_InvestmentDate Investem3_0_0_ Insurance Insurance__保険金額_保険金額=から?
nullの場合

解決策をご提案ください。

おかげ
スネハ

答えて

0

ヌルスタックトレースせずに、正確に言うのは難しいが、おそらく

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

戻ります(データベースでは、このような記録)ので、次の行は、NullPointerExceptionが生成されます。 NPEの発生場所を確認するために、catch節に

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

を追加することをお勧めします。

+0

yeaj ..私はそのキャッチを持っています、それはそこに行くだけです。しかし、なぜ私はテーブルにレコードを持っているときにそのエラーを投げているのですか? – Smitha

+0

ID列のマッピングが間違っている可能性がありますか?たぶんDBでタイプが長くないのでしょうか? DDLとデータベースレコードをダンプできますか? –

0

私はいくつかの変更を提案

変更タイプ:

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

や保険クラス(POJO)

private Long insuranceName; 

でこれを試してみてください。

更新:あなたのマッピングを試しました。あなたがid 1で保険データを持っており、hibernate.cfg.xmlが適切であることを確認しました。