How to wrap sublist with jaxb

后端 未结 3 968
孤独总比滥情好
孤独总比滥情好 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条回答
  •  旧时难觅i
    2021-02-19 09:44

    This seems to do the trick:

      @XmlElementWrapper(name="orders")
      @XmlElement(name="order", type=OrderJaxbDao.class)
      public List 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 orders=new ArrayList();
    
      /**
       * 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 getOrders() { return orders; };
    
      /**
       * setter for orders
       * @param porders - new value for orders
       */
      public void setOrders(List porders) { orders=porders; };
    
    
    } // CustomerJaxbDao
    

提交回复
热议问题