我想在addTrasction方法中插入一个具有多个产品的事务,如下所示,事务和产品类之间存在一对多的关系,但是出现此错误非法尝试将集合与两个打开的会话相关联
这是hibernate cofigurtion
File path2Config= new File("./src/com/config/hibernate.cfg.xml");
SessionFactory sessionfactory=new
Configuration().configure(path2Config).buildSessionFactory();
Session session=sessionfactory.openSession();
保存产品方法
public Product save(Product product){
session.beginTransaction();
session.save(product);
session.getTransaction().commit();
return product;
}
保存交易方法
public Transaction save(Transaction transaction){
session.beginTransaction();
session.save(transaction);
session.getTransaction().commit();
return transaction;
}
addTransaction方法
public void addTrasction(Product product){
Transaction transaction=new Transaction();
for(Cart c:list){
product=new Product();
product.setProductName(c.getProductName());
product.setProductPrize(c.getPrize());
product.setQuantity(c.getQuantity());
product.setTransaction(transaction);
product=productDao.save(product);
}
Date transactionDate=new Date();
transaction.setTransactionDate(transactionDate);
transactionDao.save(transaction);
}
交易类
@Entity
@Table(name="transaction")
public class Transaction {
@Id
@GenericGenerator(name="autoGen" ,strategy="increment")
@GeneratedValue(generator="autoGen")
@Column(name="transactionId")
private long transactionId;
@Column(name="transactionDate")
private Date transactionDate;
@Column(name="cost")
private double cost;
@OneToMany(mappedBy="transaction")
private List<Product> products=new ArrayList<Product>();
public Transaction()
{
}
public long getTransactionId() {
return transactionId;
}
public void setTransactionId(long transactionId) {
this.transactionId = transactionId;
}
public Date getTransactionDate() {
return transactionDate;
}
public void setTransactionDate(Date transactionDate) {
this.transactionDate = transactionDate;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
产品类
@Entity
@Table(name="Product")
public class Product {
@Id
@GenericGenerator(name="autoGen" ,strategy="increment")
@GeneratedValue(generator="autoGen")
@Column(name="productId")
private long productId;
@Column(name="productName")
private String productName;
@Column(name="productPrize")
private double productPrize;
@Column(name="quantity")
private int quantity;
@JoinColumn(name = "transactionId")
@ManyToOne(cascade = CascadeType.ALL)
Transaction transaction;
public long getProductId() {
return productId;
}
public void setProductId(long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getProductPrize() {
return productPrize;
}
public void setProductPrize(double productPrize) {
this.productPrize = productPrize;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Transaction getTransaction() {
return transaction;
}
public void setTransaction(Transaction transaction) {
this.transaction = transaction;
}
}
答案 0 :(得分:0)
将addTrasction
方法替换为
public void addTrasction(Product product){
Transaction transaction=new Transaction();
for(Cart c:list){
product=new Product();
product.setProductName(c.getProductName());
product.setProductPrize(c.getPrize());
product.setQuantity(c.getQuantity());
product.setTransaction(transaction);
transaction.getProducts().add(product);//here is the change
}
Date transactionDate=new Date();
transaction.setTransactionDate(transactionDate);
transactionDao.save(transaction);
}
这样,hibernate将在事务表中执行一个插入,并在产品表中使用正确的FK执行多个插入。
答案 1 :(得分:0)
最后我解决了它
将sessionfactory.getCurrentSession更改为sessionfactory.openSession并将addTrasction方法更改为
public void addTrasction(Product product){
Transaction transaction=new Transaction();
Date transactionDate=new Date();
transaction.setTransactionDate(transactionDate);
for(Cart c:list){
product=new Product();
product.setProductName(c.getProductName());
product.setProductPrize(c.getPrize());
product.setQuantity(c.getQuantity());
product.setTransaction(transaction);
product=productDao.save(product);
}
list.clear();
}