0
我使用的是Java8,Hibernate 5.2.1.Final,JPA2,Spring 4.3.1.RELEASE和MySQL。休眠:鍵'重複'的重複條目'173-604'
我有一種情況,我在一行上做了select
,但得到了ConstraintViolationException
。
首先,我有下表。如果我做了選擇通過MySQL工作臺,它有一行數據:
SELECT * FROM www.person;
結果
ID, UID, JOINING_DATE, LASTACCESS_DATE, DISPLAY_NAME, EMAIL_ADDRESS, AVATAR, PROVIDER
'176', 'PHblCtu5odMNQEaOu9TOmGgpsJM2', '1485981515384', '1485981525475', 'One', '[email protected]', null, '1'
然後,我的代碼有以下幾點:
的Java
@Override
public Person findByUid(String uid, Integer provider) {
StringBuilder sb = new StringBuilder();
sb.append(" select e.* ");
sb.append(" from ");
sb.append(" www.person as e ");
sb.append(" where e.uid = :uid ");
sb.append(" and e.provider = :provider ");
Query q = entityManager.createNativeQuery(sb.toString(), Person.class);
q.setParameter("uid", uid);
q.setParameter("provider", provider);
Person person = (Person) q.getResultList().stream().findFirst().orElse(new Person());
return person;
}
其他e uid(PHblCtu5odMNQEaOu9TOmGgpsJM2
)和提供者(1
)匹配上面的行。
但上述q.getResultList()
被調用時,我得到:
錯誤
22:28:25,843 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-14) SQL Error: 1062, SQLState: 23000
22:28:25,844 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-14) Duplicate entry '173-604' for key 'PRIMARY'
22:28:25,845 INFO [org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl] (default task-14) HHH000010: On release of batch it still contained JDBC statements
org.hibernate.exception.ConstraintViolationException: could not execute statement
豆
@Entity
@Table(name = "person")
@XmlRootElement(name = "person")
public class Person extends AbstractDomain<Long> {
// private static final Logger logger = Logger.getLogger(
// ClassName.class.getName());
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(min = 1, max = 45)
@Column(name = "UID")
private String uid;
@Column(name = "JOINING_DATE", nullable = false)
private Long joiningDate;
@Column(name = "LASTACCESS_DATE", nullable = false)
private Long lastAccessDate;
@Size(min = 1, max = 85)
@Column(name = "DISPLAY_NAME", nullable = false)
private String displayName;
@Size(min = 5, max = 55)
@Column(name = "EMAIL_ADDRESS", nullable = false, unique = true)
private String emailAddress;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "person_language", joinColumns = {
@JoinColumn(name = "PER_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
@JoinColumn(name = "LANG_ID", referencedColumnName = "LANGUAGE_CODE", unique = true) })
private Set<Language> languages;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinTable(name = "person_location", joinColumns = {
@JoinColumn(name = "PER_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
@JoinColumn(name = "LOC_ID", referencedColumnName = "ID", unique = true) })
private Set<LocationPerson> locations = null;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "person_job_favourites", joinColumns = {
@JoinColumn(name = "PER_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
@JoinColumn(name = "JOB_ID", referencedColumnName = "ID", unique = true) })
private Set<Job> favouriteJobs;
問題
我不知道你可以從執行select
查詢得到ConstraintViolationException
。如果任何人可以建議我如何解決這個問題,我將不勝感激。
p.s.我可以在上面的Bean上成功執行merge
。
你執行該交易的任何操作? –
另外,可以添加與該特定人員ID相對應的表person_language,person_location,person_job_favorites的行 –
是的,我在'select'之前的同一個事務中執行同一對象('person')的'合併'。該代碼不會在'merge'錯誤,而是在'select'錯誤。 – Richard