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

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

最佳答案

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

关于java - 如何防止@Cascade({CascadeType.ALL}) 保存重复的重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31241507/

相关文章:

java - Rest API 调用拒绝 Cookie

java.lang.ClassNotFoundException : sun. jdbc.odbc.JdbcOdbcDriver 异常

java - 图像未存储在 postgres 中

java - Guice: "Could not find suitable constructor"当在 FactoryBuilder 中提供合适的构造函数时

java - 什么是NullPointerException,我该如何解决?

mysql - 根据一列值删除重复行

Mysql警告问题?

PHP 奇怪的错误 "0:"。可能的对象语法问题?

hibernate - 从 1.3.5 升级到 1.3.9 时如何修复 grails 配置错误?

java - 如何使用 Hibernate 作为非阻塞 ORM 与 Vert.x