이 콘텐츠는 선택한 언어로 제공되지 않습니다.

6.6. Persisting an Order with DAO


  1. The sample code below demonstrates how to persist an order with DAO. 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:
    <order>
        <ordernumber>1</ordernumber>
        <customer>123456</customer>
        <order-items>
            <order-item>
                <product>11</product>
                <quantity>2</quantity>
            </order-item>
            <order-item>
                <product>22</product>
                <quantity>7</quantity>
            </order-item>
        </order-items>
    </order>
    
    Copy to Clipboard Toggle word wrap
  2. Use a custom DAO such as the example below to persist the Order entity:
    @Dao
    public class OrderDao {
     
        private final EntityManager em;
     
        public OrderDao(EntityManager em) {
            this.em = em;
        }
     
        @Insert
        public void insertOrder(Order order) {
            em.persist(order);
        }
    }
    
    Copy to Clipboard Toggle word wrap
    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.
  3. Use a custom DAO as shown in the following example to lookup the Product entities:
    @Dao
    public class ProductDao {
     
        private final EntityManager em;
     
        public ProductDao(EntityManager em) {
            this.em = em;
        }
     
        @Lookup(name = "id")
        public Product findProductById(@Param("id")int id) {
            return em.find(Product.class, id);
        }
    }
    
    Copy to Clipboard Toggle word wrap
    When looking at this class, notice the @Lookup and @Param annotation. The @Lookup annotation declares that the ProductDao.findProductById() 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 their position.
  4. When you have configured your order as shown above, the resulting Smooks configuration will look like this:
    <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                          xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.4.xsd"
                          xmlns:dao="http://www.milyn.org/xsd/smooks/persistence-1.2.xsd">
     
        <jb:bean BeanId="order" class="example.entity.Order" createOnElement="order">
            <jb:value property="ordernumber" data="ordernumber"/>
            <jb:value property="customerId" data="customer"/>
            <jb:wiring setterMethod="addOrderLine" BeanIdRef="orderLine"/>
        </jb:bean>
     
        <jb:bean BeanId="orderLine" class="example.entity.OrderLine" createOnElement="order-item">
            <jb:value property="quantity" data="quantity"/>
            <jb:wiring property="order" BeanIdRef="order"/>
            <jb:wiring property="product" BeanIdRef="product"/>
        </jb:bean>
     
        <dao:locator BeanId="product" dao="product" lookup="id" lookupOnElement="order-item" onNoResult="EXCEPTION">
            <dao:params>
                <dao:value name="id" data="product" decoder="Integer"/>
            </dao:params>
        </dao:locator>
     
        <dao:inserter BeanId="order" dao="order" insertOnElement="order"/>
     
    </smooks-resource-list>
    
    Copy to Clipboard Toggle word wrap
  5. Use the following code to execute Smooks:
    Smooks smooks=new Smooks("./smooks-configs/smooks-dao-config.xml");
    ExecutionContext executionContext=smooks.createExecutionContext();
     
    // The register is used to map the DAO's to a DAO name. 
    // The DAO name is used in the configuration.
    // The MapRegister is a simple Map like implementation of the DaoRegister.
    DaoRegister<object>register = MapRegister.builder()
            .put("product",new ProductDao(em))
            .put("order",new OrderDao(em))
            .build();
     
    PersistenceUtil.setDAORegister(executionContext,mapRegister);
     
    // Transaction management from within Smooks isn't supported yet,
    // so we need to do it outside the filter execution
    EntityTransaction tx=em.getTransaction();
    tx.begin();
     
    smooks.filter(new StreamSource(messageIn),null,executionContext);
     
    tx.commit();
    
    Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat