如何在Hibernate中将OneToMany关系与弱实体进行映

如何在Hibernate中将OneToMany关系与弱实体进行映

本文介绍了如何在Hibernate中将OneToMany关系与弱实体进行映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在休眠状态下使用JPA注释映射一个OneToMany关系,其中涉及一个弱实体.
例如
餐桌订单:

I need to map a OneToMany relationship in hibernate, with the JPA annotations, in which is involved a weak entity.
For example
Table orders:

CREATE TABLE orders(
idorder serial NOT NULL,
note varchar(30),
CONSTRAINT orders_pkey PRIMARY KEY (idorder)
)

表OrderItems:

Table OrderItems:

CREATE TABLE orderitems(
  idorder integer NOT NULL,
  iditem serial NOT NULL,
  qnt integer,
  CONSTRAINT orderitems_pk PRIMARY KEY (idorder, iditem),
  CONSTRAINT fk_orderitems FOREIGN KEY (idorder)
      REFERENCES orders (idorder) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

在我的订单"类中,我已经通过以下方式实现了getOrderItem()方法:

Within my class "Orders" I have realized the method getOrderItem() in this way:

// i need cascadeType.All here
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "order")
public Set<OrderItem> getOrderItems() {
    return items;
}

现在,无法知道将分配给新订单的标识符在插入之前,例如可以在类OrderItem中使用注解,以便自动(以层叠模式)提交正确的idOrder?
我的目标是要得到这样的情况.

now,not being able to know the identifier that will be assigned to a new orderprior to insertion, such as Annotations can I use within the class OrderItem in order to submit automatically (in cascade mode) the correct idOrder?
my goal is to get a situation like this.

Order myOrder = new Order();
//      myOrder.setId(1) not necessary
myOrder.setNote("orderNote");

OrderItem firstItem = new OrderItem();
// firstItem.setIdOrder()       no need to specify idorder
// firstItem.setId(12);
firstItem.setName("firstItem");
firstItem.setQnt(2);

OrderItem secondItem = new OrderItem();
// secondItem.setId(13);
secondItem.setName("secondItem");
secondItem.setQnt(4);

Set<OrderItem> items = new HashSet<OrderItem>();
items.add(firstItem);
items.add(secondItem);

myOrder.setItems(items);

OrderDAO dao = new OrderDAO();
dao.save(myOrder);  // i want inser all items in cascade with the idOder assigned to "myOrder"

推荐答案

好吧,我将尝试添加上述给定场景所需的实体类.

Ok I will try to add Entity classes you need to have above given scenario.

@Entity
@Table(name="orders")
public class Order{

    @Id
    @GeneratedValue
    @Column(name="ID")
    private Long id;

    @Column(name="note ")
    private String note ;

    @OneToMany(mappedBy="orders")
    private Set<OrderItem> orderitems;

    // Getter and Setter methods
}

然后是OrderItem类

And then the OrderItem class

@Entity
@Table(name="OrderItems")
public class OrderItem{

    @Id
    @Column(name="iditem")
    private Long iditem;

    @Column(name="qnt")
    private long qnt ;

    @ManyToOne
    @JoinColumn(name="idorder")
    private Order order;

    public OrderItem() {

    }

    // Getter and Setter methods
}

我也想得到您的setname映射到哪一列.. iditem不是生成的值,它是分配的ID类型好的,那么关于 inverse 的问题还可能是谁可能成为关系所有者..Inverse = true与 mappedBy 属性用法具有相同的行为,因此请尝试对其进行更改.让我知道这是否可行,或者在尝试时遇到问题.

Also i dint get which column your setname maps to.. and iditem isnt generated value its assigned id typeOk then it also could be issue about inverse that who could be the relationship owener .. Inverse=true is same behaviour as mappedBy attribute usage so try changing it..Let me know if this works or you get a issue while trying this out..

这篇关于如何在Hibernate中将OneToMany关系与弱实体进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:09