2012-01-27 8 views
0

私は3つのエンティティ:ユーザー,チーム、およびTeamInviteを持っています。各ユーザーには、チームが1つあります。各ユーザーユーザーチームに招待できます。チームインベイトを作成します。 TeamInviteが受け入れられると、それぞれユーザーの *チーム*が更新されます。 TeamInvites影響を受けませんユーザー、そのチームTeamInvite.fulfill()が呼び出されるとき再生! JPA @ManyToManyバグ

@Entity 
public class Team extends Model { 
    @OneToOne 
    public User user; 

    @ManyToMany(cascade=CascadeType.ALL) //I've also tried CascadeType.PERSIST 
    public List<User> team = new ArrayList<User>(); 
} 

@Entity 
public class TeamInvite extends Model { 
    @ManyToOne 
    public User inviter; 

    @ManyToOne 
    public User invitee; 

    public void fulfill() { 
     Team team = Team.forUser(inviter); 
     team.team.add(invitee); 
     team.save(); //error gets thrown here 

     team = Team.forUser(invitee); 
     team.team.add(inviter); 
     team.save(); 

     delete(); 
    } 
} 

、私は次のエラーを取得する:

PersistenceException occured : org.hibernate.exception.SQLGrammarException: could not insert collection: [models.Team.team#2] 

play.exceptions.JavaExecutionException: org.hibernate.exception.SQLGrammarException: could not insert collection: [models.Team.team#2] 
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:231) 
    at Invocation.HTTP Request(Play!) 
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert collection: [models.Team.team#2] 
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214) 
    ... 
Caused by: org.hibernate.exception.SQLGrammarException: could not insert collection: [models.Team.team#2] 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1243) 
    at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:81) 
    ... 
Caused by: org.h2.jdbc.JdbcSQLException: Duplicate column name "TEAM_ID"; SQL statement: 
insert into Team_dp_user (Team_id, team_id) values (?, ?) [42121-149] 
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:327) 
    at org.h2.message.DbException.get(DbException.java:167) 
    ... 

私は矢部デモ(投稿にはタグのセットを持っている)からの私の注釈構造をコピーしました。誰か私が間違っていることを知っている?

答えて

3

Team.teamという関係の反対側は、User.teamという名前もあります。その場合、デフォルト・フォームがpropertyName + "_id"であるため、結合表の列名の間に衝突があります。

したがって、プロパティ名の1つを変更するか、デフォルトの列名を@JoinTableで上書きする必要があります。

+0

Team.teamをTeam.members(これはおそらくより良い名前です)に変更すると、実際に問題が解決されました。ありがとう! –

関連する問題