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

How to prevent @Cascade({CascadeType.ALL}) from saving duplicate duplicate records

在下面提供的代码中,当我 运行 HibernateDAOImplTest 并且重复的项目被插入项目 table 时。我正在保存两个顾客和物品,有两个物品(肥皂、洋葱)在顾客(RAJ 和 DESH)之间很常见,我只想在物品 table 中插入这两个物品一次(不要需要重复)但我需要 customer_item_mapping table.

中的客户项目映射

我正在使用

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);
}
}

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