我正在尝试在实体中保存具有多对多关联的属性:
public static void addPost(Conversation conversation, Post post) {
conversation.posts.add(post);
conversation.saveManyToManyAssociations("posts");
}
该实体如下所示:
@Entity
public class Conversation extends Model {
@Id
@Constraints.Required
@Formats.NonEmpty
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer id;
@ManyToMany
public List<Post> posts = new ArrayList<Post>();
我可以在添加到列表后加载数据但不保存帖子对象。我收到这个错误:
[error] Test common.conversation.ConversationModelTest.addPost failed: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY_KEY_2A ON PUBLIC.CONVERSATION_POST(CONVERSATION_ID, POST_ID)"; SQL statement:
[error] insert into conversation_post (conversation_id, post_id) values (?, ?) [23505-168]
[error] at com.avaje.ebeaninternal.server.persist.ExeUpdateSql.execute(ExeUpdateSql.java:76)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersistExecute.executeSqlUpdate(DefaultPersistExecute.java:125)
[error] at com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql.executeNow(PersistRequestUpdateSql.java:44)
[error] at com.avaje.ebeaninternal.server.core.PersistRequest.executeStatement(PersistRequest.java:74)
[error] at com.avaje.ebeaninternal.server.core.PersistRequestUpdateSql.executeOrQueue(PersistRequestUpdateSql.java:49)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.executeSqlUpdate(DefaultPersister.java:147)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.saveAssocManyIntersection(DefaultPersister.java:1051)
[error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.saveManyToManyAssociations(DefaultPersister.java:945)
[error] at com.avaje.ebeaninternal.server.core.DefaultServer.saveManyToManyAssociations(DefaultServer.java:1734)
[error] at com.avaje.ebeaninternal.server.core.DefaultServer.saveManyToManyAssociations(DefaultServer.java:1721)
[error] at com.avaje.ebean.Ebean.saveManyToManyAssociations(Ebean.java:617)
[error] at play.db.ebean.Model.saveManyToManyAssociations(Model.java:107)
[error] at models.Conversation.addPost(Conversation.java:80)
[error] at common.conversation.ConversationModelTest.addPost(ConversationModelTest.java:80)
[error] ...
[error] Caused by: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY_KEY_2A ON PUBLIC.CONVERSATION_POST(CONVERSATION_ID, POST_ID)"; SQL statement:
[error] insert into conversation_post (conversation_id, post_id) values (?, ?) [23505-168]
[error] at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
[error] at org.h2.message.DbException.get(DbException.java:169)
[error] at org.h2.message.DbException.get(DbException.java:146)
[error] at org.h2.index.BaseIndex.getDuplicateKeyException(BaseIndex.java:81)
[error] at org.h2.index.TreeIndex.add(TreeIndex.java:62)
[error] at org.h2.table.RegularTable.addRow(RegularTable.java:121)
[error] at org.h2.command.dml.Insert.insertRows(Insert.java:124)
[error] at org.h2.command.dml.Insert.update(Insert.java:84)
[error] at org.h2.command.CommandContainer.update(CommandContainer.java:75)
[error] at org.h2.command.Command.executeUpdate(Command.java:230)
[error] at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:156)
[error] at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:142)
[error] at com.jolbox.bonecp.PreparedStatementHandle.executeUpdate(PreparedStatementHandle.java:203)
[error] at com.avaje.ebeaninternal.server.persist.ExeUpdateSql.execute(ExeUpdateSql.java:68)
[error] ... 46 more
有什么想法吗?
答案 0 :(得分:2)
你是否真的不想尝试添加已经存在的关系?在典型的情况下,这应该不是问题,因为您可能只会添加全新的帖子,但是对于您尝试添加一些固定集的测试可能吗?
AFAIK saveManyToManyAssociations
尝试按原样保存整套关系,因此您需要检查conversation.posts
是否不包含您要添加的post
,并跳过添加如果是的话。