我在mysql db中生成了值策略IDENTITY和auto_increment。据我所知,持久对象必须在flush()调用后生成值。但我也一样。怎么了?感谢。
@Entity
@Table(name = "orderr")
public class Order implements Serializable, ConvertibleToDTO<OrderDTO>{
@NotNull @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id;
@NotNull @Size(min = 1, max = 20) private String status;
@NotNull @Size(min = 1, max = 100) private String address;
@NotNull private Date orderDate;
private Manager manager;
@NotNull private Date deliveryDate;
@NotNull private Integer totalCost;
@NotNull private Customer customer;
@NotNull private DeliveryType deliveryType;
@NotNull private PaymentMethod paymentMethod;
private Set<Product> products = new HashSet<Product>();
@Id @Column(name = "id") public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Column(name = "address") public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
@Column(name = "order_date") public Date getOrderDate() { return orderDate; }
public void setOrderDate(Date orderDate) { this.orderDate = orderDate; }
@Column(name = "delivery_date") public Date getDeliveryDate() { return deliveryDate; }
public void setDeliveryDate(Date deliveryDate) { this.deliveryDate = deliveryDate; }
@ManyToOne(cascade = {CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "delivery_type")
public DeliveryType getDeliveryType() { return deliveryType; }
public void setDeliveryType(DeliveryType deliveryType) { this.deliveryType = deliveryType; }
@Column(name = "status") public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
@Column(name = "total_cost") public Integer getTotalCost() { return totalCost; }
public void setTotalCost(Integer totalCost) { this.totalCost = totalCost; }
@ManyToOne(cascade = {CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "customer_id")
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
@ManyToOne(cascade = {CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "manager_id")
public Manager getManager() { return manager; }
public void setManager(Manager manager) { this.manager = manager; }
@ManyToMany(cascade = {CascadeType.MERGE,CascadeType.DETACH}, fetch = FetchType.EAGER, targetEntity = Product.class)
@JoinTable(name = "order_product", joinColumns = {@JoinColumn(name = "order_id")},
inverseJoinColumns = {@JoinColumn(name = "product_id")})
public Set<Product> getProducts() {return products; }
public void setProducts(Set<Product> products) { this.products = products; }
@ManyToOne(cascade = {CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "payment_method_id")
public PaymentMethod getPaymentMethod() { return paymentMethod; }
public void setPaymentMethod(PaymentMethod paymentMethod) { this.paymentMethod = paymentMethod; }
}
@Entity
@Table(name = "product")
public class Product implements Serializable, ConvertibleToDTO<ProductDTO>{
@NotNull @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id;
@Size(min = 1, max = 50) private String name;
@Size(max = 255) private String description;
@IntegerSize(min = 1, max = 11) private Integer showcasePrice;
private Set<Order> orders = new HashSet<Order>();
@Id @Column(name = "id") public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Column(name = "description") public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
@Column(name = "name") public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Column(name = "showcase_price") public Integer getShowcasePrice() { return showcasePrice; }
public void setShowcasePrice(Integer showcasePrice) { this.showcasePrice = showcasePrice; }
@ManyToMany(mappedBy = "products", cascade = {CascadeType.MERGE, CascadeType.DETACH}, fetch = FetchType.EAGER, targetEntity = Order.class)
public Set<Order> getOrders() { return orders; }
public void setOrders(Set<Order> orders) { this.orders = orders; }
}
public Integer create(OrderDTO orderDTO) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Integer result = null;
try {
session.beginTransaction();
session.clear();
Order entity = orderDTO.createEntityFromDTO();
Set<Product> savedProducts = entity.getProducts();
entity.setProducts(null);
session.persist(entity);
session.flush();
//!!!!!!!!!!!!!!!!!!!!!
result = entity.getId(); //RETURN THE SAME VALUE
//!!!!!!!!!!!!!!!!!!!!!
entity.setProducts(savedProducts);
session.update(entity);
session.getTransaction().commit();
} catch (Exception e) {
LoggerFactory.getLogger(Application.class).error(e.getMessage(), e);
session.getTransaction().rollback();
} finally { return result; }
}
}