我试图在 hibernate 状态下使用saveorupdate方法,结果导致
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Mobiles' for key 'type_name'
这是我的代码
public ItemType addItemType(String typeName) {
Session session = factory.openSession();
Transaction tx = null;
ItemType itemType = null;
try{
tx = session.beginTransaction();
itemType = new ItemType();
itemType.setTypeName(typeName);
itemType.setDescription("");
itemType.setCreatedBy("shoppingcart");
itemType.setCreatedDate(new Date());
itemType.setUpdatedBy("shoppingcart");
itemType.setUpdatedDate(new Date());
session.saveOrUpdate(itemType);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
return itemType;
}
这是ItemType类
@Entity
@Table(name = "item_types")
public class ItemType implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_type_id")
private String typeId;
@Column(name = "type_name")
private String typeName;
@Column(name = "description")
private String description;
@Column(name = "created_date")
private Date createdDate;
@Column(name = "created_user")
private String createdBy;
@Column(name = "updated_date")
private Date updatedDate;
@Column(name = "updated_user")
private String updatedBy;
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTypeId() {
return typeId;
}
public void setTypeId(String typeId) {
this.typeId = typeId;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(Date updatedDate) {
this.updatedDate = updatedDate;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
}
首先,我做了一个插入项,它工作正常,当我尝试更新时,我猜它试图进行插入并导致上述错误。请帮忙。
更新
这是db中的表结构
DROP TABLE IF EXISTS `shopping_cart`.`item_types`;
CREATE TABLE `shopping_cart`.`item_types` (
`item_type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type_name` varchar(45) NOT NULL,
`description` varchar(450) DEFAULT NULL,
`created_user` varchar(45) NOT NULL,
`created_date` datetime NOT NULL,
`updated_user` varchar(45) NOT NULL,
`updated_date` datetime NOT NULL,
PRIMARY KEY (`item_type_id`),
UNIQUE KEY `type_name` (`type_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
最佳答案
您的代码始终会保存一个新的ItemType,并且由于您的数据库对“type_name”具有唯一的约束,因此您在第二次尝试运行此方法时会遇到该异常(对于 type_name 而言)。
因此,您不能使用hbmddl,因为否则您的Hibernate ItemType模型必须使用:
@Column(name = "type_name", unique = "true")
private String typeName;
保存之前,需要通过typeName选择ItemType:
public ItemType addItemType(String typeName) {
Session session = factory.openSession();
Transaction tx = null;
ItemType itemType = null;
try{
tx = session.beginTransaction();
itemType = session.createQuery("select it from ItemType where typeName = :typeName")
.setParameter("typeName", typeName)
.uniqueResult();
if(itemType == null) {
itemType = new ItemType();
itemType.setTypeName(typeName);
itemType.setDescription("");
itemType.setCreatedBy("shoppingcart")
itemType.setCreatedDate(new Date());
}
itemType.setUpdatedBy("shoppingcart");
itemType.setUpdatedDate(new Date());
session.saveOrUpdate(itemType);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
return itemType;
}