Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate:从商店项目中获取顶级类别列表_Java_Database_Hibernate_Hql - Fatal编程技术网

Java Hibernate:从商店项目中获取顶级类别列表

Java Hibernate:从商店项目中获取顶级类别列表,java,database,hibernate,hql,Java,Database,Hibernate,Hql,我的Hibernate应用程序中有以下实体:- 存储、项目、类别具有以下关系 商店与商品 类别与其他类别有多对多关系(即父类别有子类别) 项与具有相同父类别的类别具有多对多关系 我想有一个顶级类别的列表,商店有它的项目 我尝试了这个HQL查询,但它只返回一个顶级类别 String queryString=“从类别c中选择c.parentCategory,其中:存储在元素中(c.items)” List categories=getSession().createQuery(queryString

我的Hibernate应用程序中有以下实体:-

存储、项目、类别
具有以下关系

商店
商品

类别
与其他类别有多对多关系(即父类别有子类别)

与具有相同父类别的类别具有多对多关系

我想有一个顶级类别的列表,商店有它的项目

我尝试了这个HQL查询,但它只返回一个顶级类别

String queryString=“从类别c中选择c.parentCategory,其中:存储在元素中(c.items)”

List categories=getSession().createQuery(queryString).setEntity(“存储”,存储).List()

请帮我解决这个问题


提前谢谢。

我以前也做过类似的事情,只是稍微简化了一下。如果类别只能有一个父类别,则可以将其建模为:

@Entity
public class Category

@OneToMany(mappedBy = "parentCategory", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Category> childCategories;

@ManyToOne
@JoinColumn(name = "parentCategoryId", nullable = true)
private Category parent;

这满足了我的需要,但需要限制类别只能有一个父类别。

您可以在逻辑级别和ORM级别实现它,而不仅仅是在ORM级别

首先,添加关系:商店与类别有一对多的关系 其次,在插入/更新/删除用于添加或删除商店级别类别的项目之前,添加预处理器

public void preProcessForItemInserting(Item item){
    Store store = getStoreDao().get( item.getStoreId() );
    Set<Category> storeCategories = store.getCategories();
    Set<Category> itemCategories = item.getCategories();
    storeCategories.addAll( itemCategories );
    getStoreDao().save( store );
}

//it is a little bit more complex than inserting logic
public void preProcessForItemUpdating(Item item){...}

//it is a little bit more complex than inserting logic
public void preProcessForItemDeleting(Item item){...}
公共项目插入(项目){
Store Store=getStoreDao().get(item.getStoreId());
Set storeCategories=store.getCategories();
Set itemcegories=item.getCategories();
storeCategories.addAll(itemcegories);
getStoreDao().save(存储);
}
//这比插入逻辑要复杂一点
公共无效预处理ForItemUpdate(项){…}
//这比插入逻辑要复杂一点
公共无效预处理ForItemDeleteing(项){…}

总之,这是面向数据设计和面向应用设计的经典问题。我们可以根据需求在两个方面调整我们的应用程序,而不仅仅是数据建模或应用程序优化。

谢谢@Alex,但我的问题更进一步。我希望列表只包含商店项目的顶级类别,也就是说,返回的父类别应至少有一个子类别,其中包含特定商店的项目。你有解决这个问题的办法吗?
public void preProcessForItemInserting(Item item){
    Store store = getStoreDao().get( item.getStoreId() );
    Set<Category> storeCategories = store.getCategories();
    Set<Category> itemCategories = item.getCategories();
    storeCategories.addAll( itemCategories );
    getStoreDao().save( store );
}

//it is a little bit more complex than inserting logic
public void preProcessForItemUpdating(Item item){...}

//it is a little bit more complex than inserting logic
public void preProcessForItemDeleting(Item item){...}