2015-07-06 76 views
1

在下面提供的代碼中,當我正在運行HibernateDAOImplTest並將重複的項目插入到項目表中時。我將兩個顧客和物品一起存儲,有兩個項目(肥皂,洋蔥),這是客戶之間常見的(RAJ和DESH),我只想在項目表中插入這兩個項目(不需要重複),但是我需要customer_item_mapping表中的客戶項目映射。如何防止@Cascade({CascadeType.ALL})保存重複記錄

我使用

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

回答

0

您需要使用@ManyToMany映射。 @ManyToOne或@OneToMAny不適用於您的情況。

+0

我想你可以使用[this](http://www.mkyong.com/hibernate/hibernate-many-to-many-relationship-example-annotation/)鏈接 – Avdhut