0

I have these 2 entities , which are related to each other using OnetoMany Mapping.

Entity:

@Table(name = "table_A")
public class UserSum{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, unique = true)
    private Long id;
    private String order_id;
    private Date order_date;
    @Column(name="email_address")
    private String emailAddress;

    @OneToMany(mappedBy="userOrderID",fetch=FetchType.EAGER, cascade = CascadeType.ALL)
    private Collection<OrderSummary> orderSummary;
        // Getter and Setters

    }


     @Entity
     @Table(name = "table_B")
     public class AlphaSumm {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id", nullable = false, unique = true)
        private Long id;
        @Column(name="user_orders_id",nullable = true)
        private Long userSumID; 
        @Column(name="total_price")
        private String  totalPrice;
            // Getter and setters

        }

Requirement: i want to save the UserSum and AphaSum.

Problems:

i am trying something like this

private void save(Order order){
    UserSum usersum = new UserSum();
    usersum.setOrder_id(order.getOrder_id());
    usersum.setOrderSummary(getAllOrdersSummary(order,userOrders));
    userOrdersRepository.save(userOrders);
}

private OrderSummary processOrderSummary(Item item,UserOrders userOrders){
    OrderSummary orderSummary= new OrderSummary();

    // Some code here  
    orderSummary.setUserOrderID(userOrders.getId());  
    return orderSummary;
    }

Note: UserOrder is the entity

but i get userOrderID null in db.

My understanding is : I am explicitly setting id here orderSummary.setUserOrderID(userOrders.getId());

but its null here because JPA/Hibernate will create the id once it saves the data so how can i map them. One solution i have thought of is something like this

userOrders = userOrdersRepository.save(userOrders);  // to get the id and then perform rest of the operation.
userOrders.setOrderSummary(getAllOrdersSummary(order,userOrders));
userOrdersRepository.save(userOrders);

but am pretty sure this is not the right way to go. Please provide me your inputs and if something is missing from my side then please let me know.

Updated

private List<OrderSummary> getAllOrdersSummary(Order order,UserOrders userOrders ){
    List<OrderSummary> orderSummary=new ArrayList<OrderSummary>();
    //some logic
    orderSummary.add(processOrderSummary(item,userOrders));
    return orderSummary;
}

private OrderSum processOrderSum(Item item,UserSum usersum){
    OrderSum orderSum= new OrderSum();
    orderSum.setOrderItems(item.getName());
    orderSum.setTotalPrice(NumberFormating.currencyFormat(item.getPrice()));
    orderSum.setQuantity(NumberFormating.numberFormat(item.getQty()));
    orderSum.setUserOrderID(usersum.getId()); 

    return orderSum
lesnar
  • 2,400
  • 7
  • 41
  • 72
  • can you show the code of `getAllOrdersSummary(order,userOrders)` please. And your Order class/entity – Patrick Nov 30 '15 at 15:38
  • @Patrick: please see updated code. Order class is just a model, it is picking data from xml (nothing special about order) – lesnar Nov 30 '15 at 15:43
  • `@Column(name="user_orders_id",nullable = true) private Long userOrderID;` in OrderSummary is the Key of UserOrders right? – Patrick Dec 01 '15 at 07:47
  • mappedBy has to point to the other side of BIDIRECTIONAL relation. It doesn't in your classes, so either remove it or put a ManyToOne at the other side. A "Long" is not a relation in O-O. – Neil Stockton Dec 01 '15 at 08:45

1 Answers1

0

I think you need to change the OrderSummary Entity like this:

@Table(name = "table_B")
public class OrderSummary {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, unique = true)
    private Long id;

    @ManyToOne(fetch = FetchType.Lazy)
    @JoinColumn(name="id")
    private UserOrders userOrders;  

    @Column(name="total_price")
    private String  totalPrice;

}

Just give a try.

And please have a look here: pa-joincolumn-vs-mappedby

Community
  • 1
  • 1
Patrick
  • 12,336
  • 15
  • 73
  • 115