简体   繁体   English

两种方式都以多对多关系向对象添加数据,

[英]Add data to objects in a many-to-many relationship both ways,

Consider this class: 考虑此类:

@Entity(name = "ORDERS") 
public class Order {
   @Id 
   @Column(name = "ORDER_ID", nullable = false)
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long orderId;

   @Column(name = "CUST_ID")
   private long custId;

   @Column(name = "TOTAL_PRICE", precision = 2)
   private double totPrice;

   @OneToOne(optional=false,cascade=CascadeType.ALL, mappedBy="order",
   targetEntity=Invoice.class)
   private Invoice invoice;

   @ManyToOne(optional=false)
   @JoinColumn(name="CUST_ID",referencedColumnName="CUST_ID")
   private Customer customer;

   @ManyToMany(fetch=FetchType.EAGER)
   @JoinTable(name="ORDER_DETAIL",
           joinColumns=
           @JoinColumn(name="ORDER_ID", referencedColumnName="ORDER_ID"),
     inverseJoinColumns=
           @JoinColumn(name="PROD_ID", referencedColumnName="PROD_ID")
   )
   private List<Product> productList;       
   ...............
   The other attributes and getters and setters goes here

And this one: 还有这个:

@Entity(name = "PRODUCT") 
public class Product {
   @Id
   @Column(name = "PROD_ID", nullable = false)
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long prodId;

   @Column(name = "PROD_NAME", nullable = false,length = 50)
   private String prodName;

   @Column(name = "PROD_DESC", length = 200)
   private String prodDescription;

   @Column(name = "REGULAR_PRICE", precision = 2)
   private String price;

   @Column(name = "LAST_UPDATED_TIME")
   private Date updatedTime;
   @ManyToMany(mappedBy="productList",fetch=FetchType.EAGER)
   private List<Order> orderList;       
   ...............
   The other attributes and getters and setters goes here

} }

Now, if I want to add a product to an order I just do this: 现在,如果要向订单添加产品,请执行以下操作:

    EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("something");
    EntityManager em = emFactory.createEntityManager();
    EntityTransaction trans = em.getTransaction();

    trans.begin();

    Order order = em.find(Order.class, 1);
    Product product = em.find(Product.class, 51);

    order.getProductList().add(product);

    trans.commit
    em.close();
    emFactory.close();

The above example works - I can see it in the database. 上面的示例有效-我可以在数据库中看到它。 If I instead do this: 如果我改为这样做:

 EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("something");
    EntityManager em = emFactory.createEntityManager();
    EntityTransaction trans = em.getTransaction();

    trans.begin();

    Order order = em.find(Order.class, 1);
    Product product = em.find(Product.class, 51);

    product.getOrderList().add(order);

    trans.commit
    em.close();
    emFactory.close();

The database isn't updating in the above example. 在上面的示例中,数据库未更新。 Shouldn't it work both ways? 它不应该双向工作吗? Is there something I should do? 我应该做些什么吗?

Hank 汉克

First off, you should always set both sides of the relationship. 首先,您应该始终设置关系的双方。

The reason it doesn't work when you set from only the Product side is that the relationship is owned by the Order side. 仅从“产品”端进行设置时,它不起作用的原因是该关系归“订单”端所有。

Try add cascade on your @ManyToMany option like this : 尝试像这样在@ManyToMany选项上添加级联:

@ManyToMany(
        mappedBy="productList",
        fetch=FetchType.EAGER,
        cascade = {CascadeType.ALL}
)

See the Hibernate Documentation which is very clear on this issue 请参阅Hibernate文档 ,该文档在此问题上非常清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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