设置方式
ProductModel.priceQuantity
1.0
LoadInterceptor 拦截器为ProductModel默认设置
ProductModel.code
test
代码中明确指定
ProductModel.approvalStatus
CHECK
LoadInterceptor 拦截器为ProductModel默认设置
所以如果要创建实例Model请使用new ProductModel()方法来创建 这样不会触发拦截器给实例Model设置默认值来对查询造成不必要的干扰
这也可以搜索本地化属性。但是如果我们这么做了 必须要手工把Model加入到Model context中。如果没有做这不 那么LocaleProvider 这个值就不会被设置 查询结果会抛出如下错误
java.lang.IllegalStateException: got no locale provider - cannot access default localized getters and setters.
//search for a product where the english name is uniqueName_english and the german name uniqueName_deutsch .ProductModel exampleProduct new ProductModel();exampleProduct.setName( uniqueName_deutsch , Locale.GERMAN);exampleProduct.setName( uniqueName_english , Locale.ENGLISH);modelService.attach(exampleProduct); // - important
额外的代码实例 请参考FlexibleSearchServiceGetModelByExampleTest.java
Saving a Model
有两种基本的方式保存模型
某些情况 保存一个Model和他引用的其他Model。使用modelService s save(...) 方法modelService.save(model);
如果一个Model还引用了其他的Model 如果这些被引用的Model之前没有被保存过那么都会被保存。如果之前已经被保存过的话那么就不会再保存
保存所有的模型modelService.saveAll();
将保存Model context上记录的所有改动。参见Model Context
CollectionsThere is a special behavior when using collections. You cannot simply get a collection-based attribute of a Model, modify the collection s contents and call the ModelService s save(...) method. The getter methods of Models return unmodifiable lists, so you cannot modify the collection. Instead, you have to:
Create a new collection object.Add existing, non-modified values.Add new or modified values.Set the collection to the attribute.Store the attribute by calling the save(...) for the Model.
This is the intended behavior to ensure data consistency: You explicitly have to create a new Collection, set values to it, and save the attribute for the collection. Thus, you know which values have been added and stored, and which values have not.
Removing a Model
删除一个Model就调用modelService删除方法:
modelService.remove(product)Refresh a Model
modelService.refresh(product)
注意 从数据库刷新检索Model的值 会覆盖现有的值 如果没有保存的话 数据会丢失
Model 与 hybris Items 之间的转换
我们可能经常会遇到Model 与hybris Items之间的转换
你应该总是避免直接访问Jalo Items 除非在ServiceLayer没有替代者。Converting a Model to an Item 把一个Model 转换为Item
原文
You should always avoid accessing Jalo items directly, unless there is no replacement in the ServiceLayer.
Sometimes you need to get access to the underlying item of a Model, for example to perform some logic only available in the item class and not yet ported to a service.
有时我们需要得到一个Model的底层Item。比如要在一些项目类执行一些逻辑但是他没有一个服务类。modelService的getSource(...) 方法把一个Model转换为Item
Product productItem modelService.getSource(productModel)
Converting an Item to a Model(把Item转换为Model)
有时 我们需要使用Model 但是这是只有Item.This typically occurs if legacy, Jalo Layer-based code is involved.To make use of this item in your service layer related code, you have to convert the item to a Model. Use the special get method in the modelService that takes an item as parameter and returns a Model, such as:
final Cart cart JaloSession.getCurrentSession().getCart();final CartModel result modelService().get(cart);return result;
Defining Enums for Models(为Models 定义枚举)
Models 可以使用JAVA的枚举 你可以为Models的属性预定义可选择的值。枚举的典型用例
星期月T恤的颜色定义一个枚举值模型:
以下例子为在Items.xml 中定义enum:enumtypes enumtype code ArticleApprovalStatus autocreate true generate true value code check / value code approved / value code unapproved / /enumtype enumtypes
2. 在attribute 标签中定义enum
attribute qualifier approvalStatus type ArticleApprovalStatus modifiers read true write true search true optional false / persistence type property / /attribute
3.出发Model生成。该Model生成枚举的getter和setter方法
/***Generated method- Getter of theProduct.approvalStatus* attribute defined at extensioncatalog.* return the approvalStatuspublic ArticleApprovalStatus getApprovalStatus()if( !isAttributeLoaded(APPROVALSTATUS))this._approvalStatus (ArticleApprovalStatus) loadAttribute(APPROVALSTATUS);throwLoadingError(APPROVALSTATUS);return this._approvalStatus;*Generated method- Getter of theProduct.articleStatus* attribute defined at extensioncatalog.* return the articleStatuspublic MapgetArticleStatus()return getLocalizedValue(this._articleStatus, ARTICLESTATUS, getCurrentLocale());*Generated method- Getter of theProduct.articleStatus* attribute defined at extensioncatalog.* param loc the value localization key* return the articleStatus* throws IllegalArgumentException if localization key cannot be mapped to data languagepublic MapgetArticleStatus(final Locale loc)return getLocalizedValue(this._articleStatus, ARTICLESTATUS, loc);*Generated method- Setter ofArticleApprovalStatus.approvalStatus* attribute defined at extensioncatalog.* param value the approvalStatuspublic void setApprovalStatus(final ArticleApprovalStatus value)this._approvalStatus value;markDirty(APPROVALSTATUS);*Generated method- Setter oflocalized:ArticleStatusMapType.articleStatus* attribute defined at extensioncatalog.* param value the articleStatuspublic void setArticleStatus(final Mapvalue)setLocalizedValue( this._articleStatus,ARTICLESTATUS, getCurrentLocale(), value );*Generated method- Setter oflocalized:ArticleStatusMapType.articleStatus* attribute defined at extensioncatalog.* param value the articleStatus* param loc the value localization key* throws IllegalArgumentException if localization key cannot be mapped to data languagepublic void setArticleStatus(final Mapvalue, final Locale loc)setLocalizedValue( this._articleStatus,ARTICLESTATUS, loc, value );}
转载于:https://www.cnblogs.com/pingjie/p/4319150.html
本文链接: http://ccollectionsb2b.immuno-online.com/view-713511.html