Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the design and implementation of Hybris Enterprise Commerce Platform service layer

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about the design and implementation of the Hybris Enterprise Commerce Platform service layer, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Let's introduce the persistence layer of SAP Hybris, the Service layer.

When we open the details page of a Product in Hybris, the Hybris backend performs the following three steps of logic:

1. The Service layer takes the data from the database and returns it to the Facade layer in the form of Model (also known as DAO objects).

2. The Facade layer calls Converter, and with the help of Populator, the DTO is generated based on Model.

3. The Controller of the Product details page returns its corresponding JSP view path to the Hybris framework, and draws the final UI through JSP technology.

Steps 2 and 3 have been introduced in the first two articles in this series, and this article will cover step 1 in detail.

The Service layer uses the form defined by XML to manage the type system of Hybris, which not only establishes the association with the database table, but also isolates the type system from the specific database implementation. ModelService and Flexible Search in the Service layer provide convenient functions for adding, deleting, modifying and querying. Let's find out one by one.

Hybris type system

In the ProductFacade of the DTO layer, the method to get ProductModel in the Service layer is called as follows.

A few simple lines of code, similar to the Service of other common Java Web projects. So does ProductModel need to be created by developers themselves?

It should be noted that POJO classes such as ProductModel (Plain Old Java Object) do not need to be created by developers themselves, but are generated through Hybris's own type system.

The types in Hybris are divided into two categories. The first type is ItemType (also known as ComposedType), which contains all the types related to Hybris business. Product is this type. The second category is DataType that supports the collection and relational properties of ItemType, including CollectionTypes, MapTypes, EnumerationTypes, RelationTypes and AtomicTypes. For example, multiple media files corresponding to a product can be defined with CollectionTypes, and then associated with RelationTypes and Product types.

ABAP consultants can compare the former (ItemType) to the global data types that contain business logic defined in ABAP Data Dictionary, while the latter is an aggregation of these business data types in ABAP using STANDARD, SORTED, HASHED TABLE, and so on. For Java developers, CollectionTypes and MapTypes are actually Hybris's higher-level abstractions of List, Map and other types in JDK.

Items.xml

Similar to the XML definition type and database configuration used by the Hibernate framework, the specific definition of the Hybris type system is stored in the items.xml file of each extension. For example, the Product type exists in the core-items.xml file under the ".. / hybris/bin/platform/ext/core/resources" folder. This file also defines many of the core business types of Hybris.

Let's look at a practical example, that is, the definition of the Product type in items.xml. SAP Hybris's help documentation contains the detailed meaning of each field in items.xml. Here only the red highlighted fields in the following image are introduced.

Extends: GenericItem . Indicates that the type Product is an extension to another type, GenericItem.

GenericItem is the root type, equivalent to the java.lang.Object of the Java type system. The Hybris type system avoids repetitive modeling of fields through inheritance.

Assuming that we have modeled the Product with the items.xml shown above, there is now a new requirement to define the CustomerProduct type. In business terms, CustomerProduct simply adds a field to the Product type to maintain the Customer ID. ABAP consultants will create a new CustomerProduct structure, add the Product type to the CustomerProduct through Include, inherit all the fields maintained on the former through include, and then simply define a new field CUSTOMER_ID on the CustomerProduct.

For the Hybris type system, the idea is similar: use the CustomerProduct type to extends Product the type, and then simply define a CustomerID field.

Autocreate = true: when executing the Hybris command line ant initialize for Hybris system initialization, the corresponding type is created in the database table according to the definition of items.xml.

Generate = true: the POJO class corresponding to this type is generated when ant compiles.

The Product type of the above figure is taken as an example, because the generate property is set to true, so after compilation, we can find an automatically generated POJO class in the following folder with the naming specification Model.java:

Hybrisinplatformootstrapgensrcdehybrisplatformcoremodelproduct

Just like many of ABAP's automatically generated resources are usually placed in packages such as $GEN, the gensrc in the same file directory as the Pojo class indicates that the file is automatically generated.

Open ProductModel.java to see its contents and learn more about how the properties defined in items.xml are mapped to this automatically generated POJO class: each type property defined in items.xml automatically generates a set of set and get methods in the POJO class.

Take the name attribute as an example, the setName and getName automatically generated in ProductModel.java:

Table = Products: the table name corresponding to the database, which is unique in the entire Hybris type system.

Attribute autocreate= "true" qualifier= "code" type= "java.lang.String" generate= "true": a new member appears in the POLO class with the name code and type String, with the set and get methods, which are created in the database table when ant initialize.

ModelService

After defining the type, you need to develop the corresponding add, delete, modify and query function. Hybris provides the de.hybris.platform.servicelayer.model.ModelService class as a helper class. You only need to pass the POJO class to the corresponding method to achieve the function of adding, deleting, modifying and querying. This is similar to the use of helper classes in Hibernate. The query operation corresponds to the get method, the corresponding save method is created and updated, and the delete operation is the remove method. There are also saveAll and removeAll methods, which can be added or deleted in batches simply by passing in a collection of business types.

The following figure is an example of getting the ModelService instance through getModelService and performing the save operation.

Flexible Search

For complex queries, Hybris also provides its own query statement Flexsible Search. For example, the associated Category type query used in ProductDao:

ABAP consultants and Java developers who have used ADBC and JDBC will be familiar with the above code.

Here is a picture taken from Jerry's blog, a comparison between ADBC and JDBC:

Https://blogs.sap.com/2017/05/08/adbc-and-jdbc/

Hybris supports mainstream databases, including MySQL,Oracle,SQL Server and SAP HANA databases. The introduction of the concept of Flexible Search is similar to ABAP Open SQL. By writing Flexible Search code that does not depend on any specific database provider, the Hybris application layer is decoupled from the specific implementation of the underlying database. In the above statement, the value of ProductModel._TYPECODE in the POJO class is "Product", which is automatically generated at compile time. Therefore, the query statement can be translated into the following text:

The "Category" after the question mark is the parameter name of the query statement to be passed, in this case the parameter "CategoryModel" of the method.

The above is how the design and implementation of the Hybris Enterprise Commerce Platform service layer is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report