0

A FailedDeliveryOrder class has a field with the type of DeliveryOrder class, while the DeliveryOrder class has a field with the type of List of ProductDes class. The DeliveryOrder field will be embedded in the same table as FailedDeliveryOrder.

How can I specify a custom name for the table of the List of ProductDes ?

FailedDeliveryOrder.java

@Table(name = "FailedDeliveryOrder")
@Entity
public class FailedDeliveryOrder {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Embedded
    private DeliveryOrder deliveryOrder;    

    public FailedDeliveryOrder() {
    }

    public FailedDeliveryOrder(DeliveryOrder deliveryOrder) {
        this.deliveryOrder = deliveryOrder;
    }
    //setter and getter
}

DeliveryOrder.java

public class DeliveryOrder {    
    @Entity
    public static class ProductDes {
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private Long id;
        public Product() {}
        // setter and getter
    }

    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinColumn(
        name = "DeliveryOrder_ID",
        nullable = false
    )
    @OrderColumn(
        name = "ProductDes_Position",
        nullable = false
    )
    private List<ProductDes> product_descriptions = new ArrayList<Product>();

    public DeliveryOrder() {
    }

    public DeliveryOrder(List<ProductDes> products) {
        this.setProduct_descriptions(products);
    }   

    public List<ProductDes> getProduct_descriptions() {
        return product_descriptions;
    }

    public void setProduct_descriptions(List<ProductDes> product_descriptions) {
        this.product_descriptions = product_descriptions;
    }
}

Instead of autogenerated name. enter image description here

  • 2
    be more specific in what you are trying to achieve – Big Zed Nov 23 '21 at 09:24
  • Updated to include more information. – Constantine Lee Nov 23 '21 at 09:39
  • Please could you verify you want to change name of table in Entity class ? – Jahongir Sabirov Nov 23 '21 at 10:27
  • I would like to change the name of the auto-generated `DELIVERYORDER$PRODUCTDES` to a custom one. – Constantine Lee Nov 23 '21 at 12:07
  • In the `Class ProductDes`you should put a `@Entity` followed by the tag `@Table(name="your_table_name')`and it should get the name you want to give to it. – Big Zed Nov 23 '21 at 13:15
  • What if the `ProductDes` class will be use as an independent object without reference to Delivery Order in the future, wouldn't there be conflict ? – Constantine Lee Nov 23 '21 at 13:21
  • Why have you mapped the FailedDeliveryOrder.deliveryOrder reference as an embedded - did you mean to make it a bidirectional OneToMany/ManyToOne relationship? I'm not sure what you are trying to do with the ProductDes class/entity or how it relates to the two classes involved. Try setting FailedDeliveryOrder.deliveryOrder up as a ManyToOne with the DeliveryOrder_ID join column, then change your DeliveryOrder to map to a list of FailedDeliveryOrder using a OneToMany mapped by the FailedDeliveryOrder.deliveryOrder with an OrderColumn to specify a column in FailedDeliveryOrder for ordering – Chris Nov 25 '21 at 17:56

0 Answers0