简体   繁体   中英

hibernate one to many with a error, the foreign key was wrong

I have two tables, orders and order_items. The relationship between them is One-to-Many. Order_items.fk_orders is the foreign key. Orders.ID is the primary key.

I first save the orders and then the order_items, but the order_items.fk_orders is different from the orders.ID and the order_items.fk_orders values is long(eg '3213148',but the normal value is '425618'),the problem only occurs sometimes.

Orders.hbm.xml as below:
....
 <id name="id" column="ID" type="java.lang.Long">
            <generator class="native">
            </generator>
 </id>

<set name="orderProduct" lazy="false" cascade="all"
             sort="unsorted">

            <key column="FK_ORDERS"></key>

            <one-to-many
                    class="com.arvato.ecommerce.model.base.OrderItems"/>

</set>
....



OrderItem.hbm.xml as below:
....
 <id name="id" column="ID" type="java.lang.Long">
            <generator class="native">
            </generator>
 </id>
<many-to-one name="orders"
                     class="com.arvato.ecommerce.model.base.Orders" cascade="none"
                     outer-join="auto" update="true" insert="true" lazy="false"
                     column="FK_ORDERS"/>
....




The code as below:
//save order
Orders orders = new Orders();
....
....

orders.setOrderItems(null);//(I think it is strange I don't know why set the null value to OrderItems?)


session = getSession();
session.save(object);
session.flush()


//save order items
....
Collection orderItems = orders.getOrderItems();
if (orderItems != null) {
                OrderItem orderItem;
                for (Iterator itemIterator = orderItems.iterator(); itemIterator.hasNext(); orderdao.insertOrderProduct(orderItem)) {
                    orderItem = (OrderItem) itemIterator.next();
                    orderItem.setOrders(orders);

                }
            }

....



public int insertOrderProduct(OrderItem orderItem)
            throws DaoException {
            Session session =null;
        try {

            session = getSession();
            session.save(orderItem);

            session.flush();


            return 1;
        } catch (Exception e) {
            e.printStackTrace();

            return 0;
        }
    }

Your mapping seems correct, that's the way you are using it that is wrong. Since you use cascade="all" , all the OrderItems listed in an instance of Orders will be automatically persisted when you save the Orders instance !

The below code should be enough :

Orders orders = new Orders();
orders.addItem(new OrderItem());
...

session = getSession();
session.save(object);
session.flush()

where the addItem method looks like :

public void addItem(OrderItem item) {
    if (items == null) {
        items = new ArrayList<OrderItem>();
    }
    item.setOrders(this);
    items.add(item);
}

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