2012-01-27 77 views
0

我有三個實體:用戶TeamInvite。每個用戶有一個團隊。每個用戶可以通過創建TeamInvite邀請其他用戶到他們的團隊。當接受TeamInvite時,每個用戶的 * Team *都會更新。 TeamInvites不影響用戶,只是他們的玩! 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(); 
    } 
} 

TeamInvite.fulfill()被調用,我得到以下錯誤:

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"

因此,您需要更改其中一個屬性名稱,或使用@JoinTable覆蓋默認列名稱。

+0

將Team.team更改爲Team.members(反正這可能是一個更好的名字)確實解決了問題。謝謝! – 2012-01-27 09:25:21