2010-10-06 73 views
2

我是一名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取查询的护理,该代码是在除了表名/类名这两个类相似。

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解决这个问题?

感谢

+1

你如何检索产品列表?你用什么?加载?得到? Hibernate标准? HQL? SQL? – pakore 2010-10-06 09:21:01

+0

我有两个额外的类,ProductDAO和StoreDAO负责查询,代码在两个类中都是类似的,除了表名。公共类ProductDao这个{ \t公共静态列表 getStoreProductsList(){ \t \t \t 会话\t = hibernateSession HibernateUtil.getSession(); \t \t hibernateSession.beginTransaction(); \t \t \t \t Query query = hibernateSession.createQuery(“from se.kyh.stores.entities.Product”); \t \t \t \t hibernateSession.getTransaction()。commit(); \t \t \t \t List storeProducts = query.list(); \t \t return storeProducts; \t \t } – pitic 2010-10-06 09:26:08

+0

您应该将此评论添加到原始问题中。试着编辑问题。 – 2010-10-06 09:41:06

回答

7

通过您的评论后。看起来你从来没有在那里设定条件,当然,你将最终得到所有的产品,不管他们属于哪个商店。没有惊喜。你在哪里指定标准?

你可以做这样的事情,

// 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 Associations and joins

14.4 Forms of join syntax

+1

+1。我只是添加一些链接到部分[14.3。关联和连接](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-joins)和[14.4。连接语法形式](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql。html#queryhql-joins-forms)的文档。 – 2010-10-06 13:07:02

+0

+1完整的答案与3种方式做它和学分:) – pakore 2010-10-06 13:43:04

+0

感谢你们的慷慨。 – 2010-10-07 04:47:45

4

问题是你的查询,它只是选择系统中的所有产品,无论商店。

尝试使用hibernate标准。为数据库创建查询更容易,并且始终可以从Java的“一侧”而不是数据库“一侧”工作。

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会注明是谁属于Storestoreid等于你提供的storeid的产品清单。

但对于这个简单的查询,它甚至容易如果使用session.get()session.load()(它们之间的区别是在documentation关于他们这个answer或详细信息)

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

正如你看到的,休眠照顾你做的加入:)。

+1

你的第二个代码片段很适合做一个小修改,你忘记了'list'的变量名。 +1。但是,您的查询在第一个片段中将返回商店列表而非产品。所以,我希望你不介意解决这个问题。 :) – 2010-10-06 10:14:52

+0

Criteria API“更容易”是主观IMO。我个人更喜欢HQL。从性能角度来看,HQL也更好。请参阅[Hibernate:Criteria与HQL](http://stackoverflow.com/questions/197474/hibernate-criteria-vs-hql)和[Hibernate查询与条件性能](http://stackoverflow.com/questions/1784631 /休眠查询-VS-标准性能)。 – 2010-10-06 13:17:28

+0

@Adeel固定,该死的迷你重构:D。 @帕斯卡尔,确实是主观的。我认为对于初学者和简单的查询更容易使用'Criteria',它更加面向Java。 HQL更多的是QL导向。感谢您的链接,但我不知道性能差异! – pakore 2010-10-06 13:39:26