我有一个带有List
注释的@OneToMany
作为Java中的Entity变量。问题是,如果我在列表中添加新的RequestPeople
或RequestProject
,然后执行foo.update()
来更新存储在数据库中的现有Request
,那么Java会尝试创建新的由于REQUESTS_PEOPLE和REQUESTS_PROJECTS表中的UNIQUE_KEY
约束而导致行失败。
这是课程:
家长实体 - 请求
@Entity
@Table(name = "REQUESTS")
public class Request extends Model
{
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@OneToMany(mappedBy = "request", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<RequestPeople> peopleList;
@OneToMany(mappedBy = "request", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<RequestProject> projectsList;
// constructors, getters, setters
}
儿童 - 请求人
@Entity
@Table(name = "REQUESTS_PEOPLE",
uniqueConstraints = @UniqueConstraint(columnNames = {"request_id", "person_id"}))
public class RequestPeople extends Model
{
@Id
@GeneratedValue
private long id;
@ManyToOne(targetEntity = Request.class, fetch = FetchType.EAGER)
@JoinColumn(name = "request_id", referencedColumnName = "id", nullable = false)
private Request request;
@ManyToOne(targetEntity = Person.class)
@JoinColumn(name = "person_id", referencedColumnName = "id", nullable = false)
private Person person;
// constructors, getters, setters
}
Child - RequestProject
@Entity
@Table(name = "REQUESTS_PROJECTS",
uniqueConstraints = @UniqueConstraint(columnNames = {"request_id", "project_id"}))
public class RequestProject extends Model
{
@Id
@GeneratedValue
private long id;
@ManyToOne(targetEntity = Request.class, fetch = FetchType.EAGER)
@JoinColumn(name = "request_id", referencedColumnName = "id", nullable = false)
private Request request;
@ManyToOne(targetEntity = Project.class)
@JoinColumn(name = "project_id", referencedColumnName = "id", nullable = false)
private Project project;
// constructors, getters, setters
}
第三实体(人和项目)只是简单的实体。以下是这些ID:
@Id
@GeneratedValue
@Column(name = "id")
private long id;
上述实体在DB(MySQL)中创建3个表。在某些时候,我创建了new ArrayList<RequestPeople>()
和new ArrayList<RequestProject>()
,并使用实体填充这些内容。当我将这些列表添加到tempRequest
Request.class
类型并执行tempRequest.save()
时,Java会将tempRequest
存储到REQUESTS
表中,对于它创建的列表其他表中的多个条目。 这很好用
问题是当我尝试在peopleList
(或项目)中添加新人时。如果我从数据库返回上一个实体,在列表中添加新人并执行tempRequest.update()
,则会抛出PersistenceException
,表明它违反了子表中的UNIQUE_KEY
约束。
javax.persistence.PersistenceException: ERROR executing DML bindLog[] error[Duplicate entry '1-30' for key 'uq_acccess_requests_people_1']
at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.execute(DmlBeanPersister.java:90)
at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.insert(DmlBeanPersister.java:57)
at com.avaje.ebeaninternal.server.persist.DefaultPersistExecute.executeInsertBean(DefaultPersistExecute.java:59)
at com.avaje.ebeaninternal.server.core.PersistRequestBean.executeNow(PersistRequestBean.java:451)
at com.avaje.ebeaninternal.server.core.PersistRequestBean.executeOrQueue(PersistRequestBean.java:485)
at com.avaje.ebeaninternal.server.persist.DefaultPersister.insert(DefaultPersister.java:261)
.
.
.
知道如何解决此错误吗?
PS 我们正在使用Ebean ORM来持久化对象
以下是我从数据库中检索条目的方法:
Request tempRequest = Ebean.find(Request.class).where().eq("id", id).findUnique();