Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 8. Enriching Output Data
8.1. Out-of-the-Box Enrichment Methods Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
Three methods for enriching your output data are included with the product:
- JDBC Datasources
- Use a JDBC Datasource to access a database and use SQL statements to read from and write to the Database. This capability is provided through the Smooks Routing Cartridge. See the section on routing to a database using SQL.
- Entity persistence
- Use an entity persistence framework (like Ibatis, Hibernate or any JPA compatible framework) to access a database and use its query language or CRUD methods to read from it or write to it.
- DAOs
- Use custom Data Access Objects (DAOs) to access a database and use its CRUD methods to read from it or write to it.
8.2. Hibernation Example Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
The data to be processed is an XML order message. Depending on your needs, the input data could also be CSV, JSON, EDI, Java or any other structured/hierarchical data format. The same principals apply, no matter what the data format is. The same principals follow for any JPA compliant framework:
8.3. Hibernate Entities Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
The following is a snapshot of the hibernate function's entities:
8.4. Processing and Persisting an Order Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
- To process and persist an XML "order" message, you should bind the order data into the Order entities (Order, OrderLine and Product). To do this, create and populate the Order and OrderLine entities using the Java Binding framework.
- Wire each OrderLine instance into the Order instance.
- In each OrderLine instance, you should lookup and wire in the associated order line Product entity.
- Finally, insert (persist) the Order instance as seen below:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - If you want to use the named query
productById
instead of the query string, the DAO locator configuration will look like this:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
8.5. Executing Smooks with a SessionRegister Object Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
The following code executes Smooks. A
SessionRegister
object is used so the Hibernate Session can be accessed from within Smooks.
8.6. Persisting an Order with DAO Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
- To persist an order with DAO, observe the example code below. This example will read an XML file containing order information (this works the same for EDI, CSV, and so on). Using the Javabean cartridge, it will bind the XML data into a set of entity beans. It will locate the product entities and bind them to the order entity bean using the ID of the products within the order items (the product element). Finally, the order bean will be persisted.The order XML message looks like this:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use a custom DAO such as the example below to persist the Order entity:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow When looking at this class you should notice the @Dao and @Insert annotations. The @Dao annotation declares that the OrderDao is a DAO object. The @Insert annotation declares that the insertOrder method should be used to insert Order entities. - Use a custom DAO as shown in the following example to lookup the Product entities:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow When looking at this class, you should notice the @Lookup and @Param annotation. The @Lookup annotation declares that the ProductDao#findByProductId method is used to lookup Product entities. The name parameter in the @Lookup annotation sets the lookup name reference for that method. When the name isn’t declared, the method name will be used. The optional @Param annotation lets you name the parameters. This creates a better abstraction between Smooks and the DAO. If you don’t declare the @Param annotation the parameters are resolved by there position. - When you have configured your order as shown above, the resulting Smooks configuration will look like this:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use the following code to execute Smooks:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow