2016-04-05 19 views
2

hibernateを使用してリレーショナルデータベースにオブジェクトを永続化することができます。 下記のコードをご覧ください。hibernateを使用してテーブルからデータを取得する

package one; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.SecondaryTable; 
import javax.persistence.Table; 

@Entity 
public class Customer { 

    @Id 
    private int customerId; 
    private String customerName; 
    private String customerAddress; 
    private int creditScore; 
    private int rewardPoints; 

    public Customer() 
    { 

    } 

    public Customer(int customerId,String customerName,String customerAddress,int creditScore,int rewardsPoints) 
    { 
     this.customerId=customerId; 
     this.customerAddress=customerAddress; 
     this.creditScore=creditScore; 
     this.customerName=customerName; 
     this.rewardPoints=rewardsPoints; 
    } 

    public int getCustomerId() { 
     return customerId; 
    } 
    public void setCustomerId(int customerId) { 
     this.customerId = customerId; 
    } 
    public String getCustomerName() { 
     return customerName; 
    } 
    public void setCustomerName(String customerName) { 
     this.customerName = customerName; 
    } 


    public String getCustomerAddress() { 
     return customerAddress; 
    } 
    public void setCustomerAddress(String customerAddress) { 
     this.customerAddress = customerAddress; 
    } 


    public int getCreditScore() { 
     return creditScore; 
    } 
    public void setCreditScore(int creditScore) { 
     this.creditScore = creditScore; 
    } 

    public int getRewardPoints() { 
     return rewardPoints; 
    } 
    public void setRewardPoints(int rewardPoints) { 
     this.rewardPoints = rewardPoints; 
    } 

} 

次に、このクラスのオブジェクトを保存するために、次のクラスを使用しました。次のクラスは、Customerクラスのオブジェクトを作成し、そのオブジェクトをデータベースに保存した後、再びそれを取得し、保存された各オブジェクトのCustomerNameプロパティを出力します。

package one; 

import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

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

public class TestCustomer { 
    public static void main(String args[]) 
    { 
     Customer cust = new Customer(13,"Sara","Banglore",9000,60); 


     SessionFactory factory = new Configuration().configure().buildSessionFactory(); 
     Session session = factory.openSession(); 
     session.beginTransaction(); 
     session.save(cust); 
     session.getTransaction().commit(); 
     session.close(); 

     session = factory.openSession(); 
     session.beginTransaction(); 
     List list = session.createQuery("FROM Customer").list(); 
     Iterator iterator = list.iterator(); 

     while(iterator.hasNext()) 
     { 
      Customer custA = (Customer)iterator.next(); 
      System.out.println("First Name\t"+custA.getCustomerName()); 
     } 
     session.getTransaction().commit(); 
     session.close(); 

    } 
} 

私はかなりの回数上記コードを実行しました。コードは正常に動作しています。保存されたすべてのオブジェクトをフェッチすることができます。 しかし、私は

Insert into Customer(CUSTOMERID,CREDITSCORE,CUSTOMERNAME,REWARDPOINTS,CUSTOMERADDRESS) 
VALUES(87,4000,'Saurabh',20,'Kalwa'); 
としてOracleヒキガエルを使用して sql声明を発射したレコードがテーブルに格納されますが、私は上記のコードを実行するとき、私はこのレコードを取得することはできませんよ。 私が描くことができる1つの結論は、休止状態は persisted objectsを返しますが、それでも私はすべてのレコードを得ることができる他の方法はありますか?

答えて

2
  1. oradで挿入した後でレコードを送信したことはありますか?(別のクライアントを開き、selectを実行してsqlクライアントからフェッチできることを確認してください)。
  2. デバッグする場合は、hibernateのSQLロギング機能を有効にしてから、SQLクライアントでhibernateが生成するsqlを実行して、すべてのレコードを正しくフェッチできることを確認します。

とJPAを使用するためのいくつかの提案:

  1. @Entityは、テーブルマッピングの混乱を避けるために、あなたの物理的なテーブルにマッピングする名前の値を持っていることを確認してください。
  2. 混乱を避けるため、すべてのフィールドで@Column(name = "column")を使用して物理テーブルの列にマッピングします。
+0

最初の理由が考えられます。 _ JPA_を使用するための奨励は必須ではありません。命名戦略を使用するのがよい方法です。 –

関連する問題