如何构建JPA协会:人与其他人(朋友)有关系

时间:2013-06-01 14:57:36

标签: jpa playframework playframework-2.1 ebean

我有一个实体有朋友的人,所以与其他人有联系:

@Entity
public class Person extends Model {

    @Id
    @Constraints.Required
    @Formats.NonEmpty
    public Long id;

    @OneToMany
    public List<Person> friends = new ArrayList<>();

如果我记录好朋友,我会看到:BeanList deferred

如果我尝试添加这样的朋友:

public void addFriend(Person person) {
    this.contacts.add(person);
    this.save("friends");
    this.update();

}

我收到此错误:

[error] Caused by: org.h2.jdbc.JdbcSQLException: Column "T1.PERSON_ID" not found; SQL statement:
[error] select t0.id c0
[error]         , t1.id c1, t1.first_name c2, t1.last_name c3, t1.gender c4, t1.self_description c5, t1.profile_image c6, t1.registration_date c7, t1.person_status c8, t1.second_mail c9, t1.user_code c10, t1.user_email c11, t1.work_place_id c12, t1.job_id c13, t1.life_cycle_stage_id c14, t1.role_id c15, t1.mentor_id c16 
[error] from person t0
[error] left outer join person t1 on t1.person_id = t0.id 
[error] where t0.id = ?   
[error] order by t0.id [42122-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.expression.ExpressionColumn.optimize(ExpressionColumn.java:138)
[error]     at org.h2.expression.Comparison.optimize(Comparison.java:159)
[error]     at org.h2.command.dml.Select.setEvaluatableRecursive(Select.java:944)
[error]     at org.h2.command.dml.Select.preparePlan(Select.java:923)
[error]     at org.h2.command.dml.Select.prepare(Select.java:823)
[error]     at org.h2.command.Parser.prepareCommand(Parser.java:218)
[error]     at org.h2.engine.Session.prepareLocal(Session.java:415)
[error]     at org.h2.engine.Session.prepareCommand(Session.java:364)
[error]     at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1109)
[error]     at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:74)
[error]     at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:264)
[error]     at com.jolbox.bonecp.ConnectionHandle.prepareStatement(ConnectionHandle.java:820)
[error]     at com.avaje.ebeaninternal.server.query.CQuery.prepareBindExecuteQuery(CQuery.java:365)
[error]     at com.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:174)
[error]     ... 44 more

我也尝试过多对多的关系。然后我明白了:

[error] play - Duplicate column name "PERSON_ID"; SQL statement:
create table person_person (
person_id                      bigint not null,
person_id                      bigint not null,
constraint pk_person_person primary key (person_id, person_id)) [42121-168] [ERROR:42121, SQLSTATE:42S21]
[info] c.j.b.BoneCP - Shutting down connection pool...
[info] c.j.b.BoneCP - Connection pool has been shutdown.
[error] Test common.person.PersonModelTest.addContact failed: Database 'default' is in inconsistent state![An evolution has not been applied properly. Please check the problem and resolve it manually before marking it as resolved.]
[error]     at play.api.db.evolutions.Evolutions$.checkEvolutionsState(Evolutions.scala:177)
[error]     at play.api.db.evolutions.Evolutions$.applyScript(Evolutions.scala:275)
[error]     at play.api.db.evolutions.EvolutionsPlugin$$anonfun$onStart$1$$anonfun$apply$1.apply$mcV$sp(Evolutions.scala:444)
[error]     at play.api.db.evolutions.EvolutionsPlugin.withLock(Evolutions.scala:483)
[error]     at play.api.db.evolutions.EvolutionsPlugin$$anonfun$onStart$1.apply(Evolutions.scala:439)
[error]     at play.api.db.evolutions.EvolutionsPlugin$$anonfun$onStart$1.apply(Evolutions.scala:437)
[error]     at scala.collection.immutable.List.foreach(List.scala:309)
[error]     at play.api.db.evolutions.EvolutionsPlugin.onStart(Evolutions.scala:437)
[error]     at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:63)
[error]     at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:63)
[error]     at scala.collection.immutable.List.foreach(List.scala:309)
[error]     at play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:63)
[error]     at play.api.Play$$anonfun$start$1.apply(Play.scala:63)
[error]     at play.api.Play$$anonfun$start$1.apply(Play.scala:63)
[error]     at play.utils.Threads$.withContextClassLoader(Threads.scala:18)
[error]     at play.api.Play$.start(Play.scala:62)
[error]     at play.api.Play.start(Play.scala)
[error]     at play.test.Helpers.start(Helpers.java:354)
[error]     at common.CommonTest.setUp(CommonTest.java:20)
[error]     ...

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

你可以试试这样的东西吗?

@ManyToOne(mappedBy="friends") 
@OneToMany 
public List<Person> friends = new ArrayList<>();

答案 1 :(得分:0)

由于here

,我找到了答案chssply76