Product と Order の間には n/n の関係があります。したがって、作成時に新しい列が必要になるため、3 番目のテーブル ProductOrder があります。
public class Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORDER_SEQ")
@Column(unique = true, nullable = false, updatable = false)
private Long idOrder;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Set<ProductOrder> productOrder;
//get and setter
ProductOrder は次のとおりです。
@Entity
@IdClass(ProductOrderId.class)
public class ProductOrder implements Serializable {
private static final long serialVersionUID = 3943799614725570559L;
@Id
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "product_id")
private Product product;
@Id
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "order_id")
private Order order;
private Integer qtdProduct;
private Double unitValueProductOrder;
//get and setter
また、私の ProcutOrderId (念のため)
public class ItemCompraId implements Serializable {
private Long compra;
private Long produto;
//get and set
および私の注文エンティティ:
@Entity
@SequenceGenerator(name = "ORDER_SEQ", sequenceName = "s_compra", initialValue = 1, allocationSize = 1)
public class Order implements Serializable {
private static final long serialVersionUID = 3943799614725570559L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORDER_SEQ")
@Column(unique = true, nullable = false, updatable = false)
private Long idOrder;
private Double orderValue;
private Date creatingDate;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private Set<ProductOrder> productOrder;
基本的に、私はすでにデータベースに永続化されている製品を持っています...注文が注文されようとしているときに、それらのリストを取得しました。したがって、すでに永続化されているオブジェクト (製品) に基づいて、新しいオブジェクト (注文) を永続化したいと考えています。これは、Order を永続化するためにマネージド Bean で呼び出されるメソッドです。
public String doOrder() throws Exception {
try {
Order order = new Order();
compra.setCreatingDate(new Date());
compra.setOrderValue(null);
if (compra.getProductOrder() == null)
compra.setProductOrder(new HashSet<ProductOrder>());
for (Product product : listOfMyCartOfProducts) {
ProductOrder productOrder = new ProductOrder();
productOrder.setQtdProduct(100);
productOrder.unitValueProductOrder(null);
productOrder.setOrder(order);
productOrder.setProduct(product); //I THINK THAT THE PROBLEM IT'S HERE
order.getOrderProduct().add(productOrder);
}
ejbInvoke.persist(order); //tryed .merge and it doesn't work aswell
return "stuff";
何か案は?私は絶望的です..昨日のためにこれが機能する必要があります..何か助けてください??
ところで、私はJSF 2.0、JPA 2.0およびPostgresでHibernateを使用しています。よろしく、