我有以下实体:
@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 的行为感到惊讶。id
正如预期的那样,JPA 将序列中的值分配给。但是,在回滚期间,该值不会被“删除”。然后下次我尝试坚持时, for 的值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
我究竟做错了什么?