How to wrap sublist with jaxb

后端 未结 3 956
孤独总比滥情好
孤独总比滥情好 2021-02-19 09:31

When trying to map a customer-> order 1:n relationship with jaxb 2.2.6 I\'d like to get the list of orders wrapped in a separate node \"orders\".

currently the result is

相关标签:
3条回答
  • 2021-02-19 09:44

    Instead of

    /**
     * getter for orders
     * @return orders
     */
    @XmlElement(name="orders", type=OrderJaxbDao.class)
    public List<Order> getOrders() { return orders; };
    

    You need

    /**
     * getter for orders
     * @return orders
     */
    @XmlElement(name="order", type=OrderJaxbDao.class)
    @XmlElementWrapper(name="orders")
    public List<Order> getOrders() { return orders; };
    

    The @XmlElementWrapper name is the name of the wrapping element (orders) and the @XmlElement name is the name of the per-entry element inside the wrapper (order).

    0 讨论(0)
  • 2021-02-19 09:44

    This seems to do the trick:

      @XmlElementWrapper(name="orders")
      @XmlElement(name="order", type=OrderJaxbDao.class)
      public List<Order> getOrders() { return orders; };
    

    improved CustomerJaxbDao

    /**
     * Copyright (C) 2011-2012 BITPlan GmbH
     * 
     * http://www.bitplan.com
     * 
     * generated: 2012-10-16 12:20 with smartGENERATOR
     */
    package com.bitplan.testentity.jaxb;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlElement;
    import com.bitplan.testentity.Customer;
    
    import java.util.List;
    import com.bitplan.testentity.Order;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import java.util.ArrayList;
    
    /**
     * JaxB Data access object for Customer
     * Entity: Customer for container testentity
     * Table: Customer
     */
    @XmlRootElement(name="Customer")
    public class CustomerJaxbDao implements Customer {
      /**
       * name
       */
      private String name;
    
      /**
       * id
       */
      private long id;
    
      /**
       * orders
       */
      private List<Order> orders=new ArrayList<Order>();
    
      /**
       * getter for name
       * @return name
       */
      @XmlElement(name="name")  
      public String getName() { return name; };
    
      /**
       * setter for name
       * @param pname - new value for name
       */
      public void setName(String pname) { name=pname; };
    
      /**
       * getter for id
       * @return id
       */
      @XmlElement(name="id")  
      public long getId() { return id; };
    
      /**
       * setter for id
       * @param pid - new value for id
       */
      public void setId(long pid) { id=pid; };
    
      /**
       * getter for orders
       * @return orders
       */
      @XmlElementWrapper(name="orders")
      @XmlElement(name="order", type=OrderJaxbDao.class)
      public List<Order> getOrders() { return orders; };
    
      /**
       * setter for orders
       * @param porders - new value for orders
       */
      public void setOrders(List<Order> porders) { orders=porders; };
    
    
    } // CustomerJaxbDao
    
    0 讨论(0)
  • 2021-02-19 09:59

    It sounds like you want to wrap the Order in your CustomerJaxbDao class.

    @XmlElementWrapper( name="orders" )
    @XmlElement(name="order", type=OrderJaxbDao.class)
    public List<Order> getOrders() { return orders; };
    

    Might do what you want.

    0 讨论(0)
提交回复
热议问题