Hibernate:重复录入' 173-604'关键' PRIMARY'

时间:2017-02-01 21:06:20

标签: java mysql hibernate jpa

我使用的是Java8,Hibernate 5.2.1.Final,JPA2,Spring 4.3.1.RELEASE和MySQL。

我有一种情况,我在一行user_id user_id ip_address ------- ------- ---------- 111 111 127.0.0.1 222 222 127.0.0.1 333 (null) (null) 444 (null) (null) 555 (null) (null) ,但获得了select

首先,我有下表。如果我通过My​​SQL Workbench进行选择,它有一行数据:

ConstraintViolationException

结果

SELECT * FROM www.person;

然后,我的代码有以下内容:

爪哇

 ID,              UID,                   JOINING_DATE, LASTACCESS_DATE, DISPLAY_NAME, EMAIL_ADDRESS, AVATAR, PROVIDER
'176', 'PHblCtu5odMNQEaOu9TOmGgpsJM2', '1485981515384', '1485981525475',   'One',     'one@mail.com', null,      '1'

其中uid(@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; } )和提供者(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; 查询中获得ConstraintViolationException。如果有人可以建议我如何解决这个问题,我将不胜感激。

P.S。我可以在上面的Bean上成功select

1 个答案:

答案 0 :(得分:0)

找到解决方案:

我需要在merge方法中设置每个连接列:

            person.setLanguages(foundPerson.getLanguages());
            person.setLocations(foundPerson.getLocations());
            person.setFavoriuteJobs(foundPerson.getFavoriuteJobs());