我可以问一下选择锁定吗? 可以读取spring数据JPA中的操作,比如repository.findOne(),findById抛出OptimisticLocking。 ?
我使用spring-data jpa + hibernate + mysql
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.sample.persistence.domain.Driver#540444464]; nested exception is javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.sample.persistence.domain.Driver#540444464]"
感谢
[代码] 持久性
@Entity
@Table(name = "driver")
public class Driver extends Base {
private static final long serialVersionUID = 1L;
@Column(name = "name", length = 50, nullable = false)
private String name;
@Column(name = "phone", length = 20, nullable = false)
private String phone;
@Column(name = "photograph")
private String photograph;
}
@MappedSuperclass
public abstract class Base implements Serializable {
private static final long serialVersionUID = -7369920601847524273L;
public Base() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Integer id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_last_modified")
protected Date modificationDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_created")
protected Date creationDate;
@Version
@Column(name = "version")
protected Integer version;
}
存储库
@Repository
public interface DriverRepository extends JpaRepository<Driver, Integer>, DriverRepositoryCustom, JpaSpecificationExecutor<Driver> {
}
DAO代码
public Driver findById(Integer key) {
Driver driver = driverRepository.findOne(key);
if (driver == null) {
return null;
}