2016-04-14 91 views
0

Hiy!使用Hybris ModelService获取特定类型的集合

我想ModelService

所有对象(在测试类型行)

所以,我可以通过收集迭代和更新单行(对象)的新的价值

我看到getModelService.create(TestModel.class)getModelService.save() 属性但他们不会创建一个新的对象/行而不是更新现有的对象吗?right 我不想创建一个新的对象,而是选择一个现有的匹配我的标准并更新其中一个属性

是否有人帮帮我 与List<TestModel> testModels = getModelService.get(TestModel.class)将返回我测试类型/表的所有行(集合)?

不幸的是我不能测试它,需要帮助

其实我在validateInterceptor ...这截获模型的基础上,改变属性值我要更新另一个模型的属性值...

谢谢

+0

请看看[问]有关如何最好地修改您的帖子,所以它是明确的和可以理解的信息。如果可能的话也创建一个[mcve]。 –

回答

0

首先 - 我认为这不是一个好主意,在任何拦截器中创建/更新模型,特别是在'验证'模型中。

关于你的问题:

  1. ModelService在大多数的情况下,可与单一的模式,并 专为创建/更新/删除操作。
  2. 要检索某种类型的所有型号,必须使用FlexibleSearchService
  3. 然后要更新每个检索到的TestType模型,可以使用ModelService的save方法。

的查询中检索所有TestType车型将是这样的: SELECT PK FROM {TestType}

0

你可以简单地使用由示例方法模型的服务,以节省他们所有的灵活搜索服务的搜索和。下面是一个例子使用Groovy脚本,所有的产品:

import java.util.List 
import de.hybris.platform.core.model.product.ProductModel 
import de.hybris.platform.servicelayer.search.FlexibleSearchService 
import de.hybris.platform.servicelayer.model.ModelService 

FlexibleSearchService fsq = spring.getBean("flexibleSearchService") 
ModelService ms = spring.getBean("modelService") 

ProductModel prd = ms.create(ProductModel.class) 
List<ProductModel> products = fsq.getModelsByExample(prd) 

//Do Whatever you want with the objects in the List 

ms.saveAll(products) 
0

ModelService.create(new TestModel.class)将创建指定类型的单个实例,并将其连接到modelservice的上下文。 但它只会被保存到持久性存储,当你调用modelService.save(newInstance)

ModelService.get()返回一个模型对象,但需要一个Jalo对象作为输入,(Jalo是的hybris遗留下来的持久层),这样就不会为你工作。

要检索对象,您可以使用FlexibleSearchService编写自己的查询,也可以查看DefaultGenericDao,该对象具有一堆简单的find()类型的方法。

通常情况下,您会注入像例如:

private GenericDao<TestModel> dao; 

[...] 

public void myMethod() 
{ 
    List<TestModel> allTestModels = dao.find(); 

    [...] 
} 

有很多更多的方法可以用来创建WHERE类型的语句来限制您的结果。

关于ValidateInterceptor: 看一看维基页面拦截器的生命周期: https://wiki.hybris.com/display/release5/Interceptors

这不是修改类型为“所有”的对象,同时该类型的拦截器是个好主意。 因此,如果您在为Test项目类型声明的拦截器中,则不要尝试修改其中的项目。 如果您碰巧遇到不同的拦截器并且想要修改不同类型的项目: 例如,你有Type1其中有一个Type2对象的列表和在拦截器中的Type1你想要修改所有的Type2对象。 对于这些场景,您必须将您修改的实例Type2添加到拦截器上下文中,以便这些更改将被保留。 这会是这样的:

void onValidate(Test1 model, InterceptorContext ctx) throws InterceptorException 
{ 
    ... 
    List<Type2> type2s = dao.find(); 
    for (Type2 type2 : type2s) 
    { 
     // do something with it 
     // then make sure to persist that change 
     ctx.registerElementFor(type2, PersistenceOperation.SAVE); 
     [...] 
    } 
}