簡體   English   中英

用hibernate加入外鍵

[英]join on foreign key with hibernate

我是一個hibernate-beginner,在嘗試使用hibernate加入2個表時遇到問題。 我想要做的是根據商店ID獲取某個商店的產品列表,但我得到的是每個商店下列出的數據庫中所有可用產品的列表。

這是Product.java的代碼:

@Entity
@Table (name = "products")
public class Product implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = -1001086120280322279L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column (name = "product_id")
private int product_id;

@Column(name = "product_name", unique=true)
private String product_name;

@JoinColumn(name = "store", referencedColumnName="store_id")
@ManyToOne(cascade=CascadeType.ALL)
private Store store;

等等..

這是Store.java的代碼:

@Entity
@Table(name = "stores")
public class Store implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 4497252090404342019L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column (name = "store_id")
private int store_id;

@Column(name = "store_name", unique=true)
private String store_name;

@JoinColumn(name="store", referencedColumnName= "store_id")
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<Product> productList;

等等..

這是輸出:(產品A應在Butik A下,產品B在Butik B下)

Butik: Butik A
Produkt: Banana A
Produkt: Morot A
Produkt: Banana B
Produkt: Apple B

Butik: Butik B
Produkt: Banana A
Produkt: Morot A
Produkt: Banana B
Produkt: Spple B

我有2個額外的類,ProductDAO和StoreDAO負責查詢,除了table-name / class-name之外,兩個類中的代碼類似。

public class ProductDAO { 
   public static List<Product> getStoreProductsList() { 
     Session hibernateSession = HibernateUtil.getSession(); 
     hibernateSession.beginTransaction();    
     Query query = hibernateSession.createQuery("from Product");
     hibernateSession.getTransaction().commit();     
     List<Product> storeProducts = query.list(); 
     return storeProducts;   
   }
}

有什么辦法只用hibernate來解決這個問題嗎?

謝謝

完成你的評論后。 看起來你從來沒有在那里設定條件,當然,無論他們屬於哪個商店,你最終都會獲得所有產品。 沒有驚喜。 你在哪里指定標准?

你可以做點什么,

// One liner
List<Product> list = session.createQuery("from Product p where p.store.store_id = "
          +" :storeId").setInteger("storeId", storeId).list();

或者您可以獲取Store ,然后獲取Product列表,如下所示,

// Another one liner
List<Product> list = session.createCriteria(Store.class)
             .add(Restrictions.eq("store_id", storeId)).list().getProductList();

另一種更簡單的方法,因為我們知道store_id是主鍵,(感謝Pakore提醒我)

// And another. Changed to use load() instead of get() here. 
// Assuming non-existance is an error.
List<Product> list = (Store) session.load(Store.class,storeId).getProductList();

將帖子

...添加幾個有用的指針(感謝Pascal

14.3關聯和連接

14.4連接語法的形式

問題是您的查詢,它只是選擇系統中的所有產品,而不管商店。

嘗試使用休眠標准。 為數據庫創建查詢更容易,您可以始終使用Java“side”而不是DB“side”。

int storeid = 1 //whatever you want here.
Criteria criteria = session.createCriteria(Product.class);
criteria.add(Restrictions.eq("store_id",storeid));
List<Product> list = criteria.list();

list將包含屬於Store的產品列表,其storeid等於您提供的storeid

但是對於這個簡單的查詢,如果你使用session.get()session.load()更容易 (它們之間的區別在於這個答案文檔中有關它們的更多信息)

int storeid = 1; //Whatever you want
Store store = (Store) session.get(Store.class,storeid);
List<Product> list = store.getProductList();

如你所見,Hibernate負責為你做連接:)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM