如何防止@Cascade({CascadeType.ALL})保存重复的重复记录

时间:2015-07-06 08:53:21

标签: java mysql hibernate hibernate-mapping cascade

在下面提供的代码中,当我运行HibernateDAOImplTest并重复插入项目表中的项目时。我将两个顾客和物品一起保存,有两个项目(肥皂,洋葱),这是客户之间共同的(RAJ和DESH),我想在项目表中只插入这两个项目(不需要重复)但是我需要在customer_item_mapping表中进行客户项目映射。

我正在使用

org.hibernate.annotations.Cascade;
org.hibernate.annotations.CascadeType;

==========表===========

    CREATE TABLE customer_item_mapping ( mapping_id int(11) unsigned NOT NULL AUTO_INCREMENT, item_id int(100) NOT NULL, customer_id int(11) NOT NULL, PRIMARY KEY (mapping_id))

CREATE TABLE customer ( customer_id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, PRIMARY KEY (customer_id) )

CREATE TABLE item ( item_id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, PRIMARY KEY (item_id) )

==========第一个实体===========

@Entity @Cacheable
@Table(name = "customer_item_mapping")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class CustomerItemMapping implements Serializable {

private static final long serialVersionUID = 3500101963230957017L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "mapping_id", unique = true, nullable = false)
private Integer mappingId;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "item_id", nullable = false)
private Item item;

@Column(name = "customer_id", nullable = false)
private Integer customerId;

}

==========第二实体===========

    @Entity @Cacheable
@Table(name = "customer")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Customer implements Serializable{

private static final long serialVersionUID = 3886876059389214345L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "customer_id", unique = true, nullable = false)
private Integer customerId;

@JoinColumn(name = "name", nullable = false)
private String customerName;

@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "customer_item_mapping", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "item_id"))
@Cascade({CascadeType.ALL})
private Set<Item> itemSet;

}

==========第三实体===========

    @Entity @Cacheable
@Table(name = "item")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Item implements Serializable{

private static final long serialVersionUID = 3886876059389214345L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "item_id", unique = true, nullable = false)
private Integer itemId;

@JoinColumn(name = "name", nullable = false)
private String name;

public Item(String name){
this.name=name;
}
}

========== HibernateDAOImpl ===========

    package com.myapp.txn.impl.genric.dao;
import org.hibernate.Session;
import com.myapp.txn.exception.PersistenceException;
import com.myapp.txn.genric.dao.HibernateDAO;
public class HibernateDAOImpl {
public Integer saveEntity(String entityType, Object obj, Session session) throws PersistenceException{
Integer id=0;
try {
id = (Integer) session.save(entityType, obj);
}catch (Exception e) {
throw new PersistenceException(e);
}
return id;
}
}

========== HibernateDAOImplTest ===========

    package com.myapp.txn.impl.genric.dao;
public class HibernateDAOImplTest {

@Autowired
private HibernateDAOImpl hibernateDAOImpl;

@Test
public void saveEntityTest(){
Set<Item> itemSet=new HashSet<Item>();
Item item=new Item("Onion");
itemSet.add(item);
item=new Item("Soap");
itemSet.add(item);
item=new Item("Paneer");
itemSet.add(item);
Customer customer=new Customer();
customer.setName("RAJ");
customer.setItemSet(itemSet);
hibernateDAOImpl.saveEntity(customer);

itemSet=new HashSet<Item>();
Item item=new Item("Onion");
itemSet.add(item);
item=new Item("Soap");
itemSet.add(item);
item=new Item("Daaru");
itemSet.add(item);
customer=new Customer();
customer.setName("DESH");
customer.setItemSet(itemSet);
hibernateDAOImpl.saveEntity(customer);
}
}

1 个答案:

答案 0 :(得分:0)

您需要使用@ManyToMany映射。 @ManyToOne或@OneToMAny将不适用于您的情况。