简体   繁体   中英

How to store entities in Spring Data JPA or hibernate

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM