我正在尝试在实体中保存具有多对多关联的属性:
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
任何的想法?