gpt4 book ai didi

java - saveorupdate 方法出现重复输入错误

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:49 25 4
gpt4 key购买 nike

我尝试在 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;
}

关于java - saveorupdate 方法出现重复输入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26311208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com