【发布时间】:2013-04-02 14:51:28
【问题描述】:
我有以下实体:
@Entity
@Table(name = "sales", schema = "cust_tables")
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(contextProperty = "customer_schema", type = TenantTableDiscriminatorType.SCHEMA)
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Sale implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "Sale")
@TableGenerator(name = "Sale", schema = "thehub", allocationSize = 5)
@Column(name = "id", unique = true, nullable = false, updatable = false)
@XmlElement
private Long id;
在持久化操作期间,我可能故意允许事务在乐观锁定失败期间回滚。
然而,我对 JPA/EclipseLink 在这种情况下的行为感到惊讶。正如预期的那样,JPA 将序列中的一个值分配给id。但是,在回滚期间,该值不会被“删除”。然后下次我尝试坚持时,id 的值已经存在。 JPA 跳过从序列中提取新数字并尝试持久化实体。
我因 SQL 完整性约束异常而失败:
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130226-e0971b1): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '51' for key 'PRIMARY'
Error Code: 1062
我做错了什么?
【问题讨论】:
标签: java jakarta-ee jpa eclipselink