I have three entities: User, Team, and TeamInvite. Each User has one Team. Each User can invite other Users to their Team by creating a TeamInvite. When a TeamInvite is accepted, each User’s *Team* is updated. TeamInvites do not affect Users, just their Teams.
@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();
}
}
When TeamInvite.fulfill() gets called, I get the following error:
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)
...
I copied my annotation structure from the Yabe demo (Posts have a Set of Tags). Anybody know what I’m doing wrong?
I guess the inverse side of
Team.teamrelationship is namedUser.teamas well. If so, you have a collision between columns names in the join table, because their default form ispropertyName + "_id".So, you need to change one of the property names, or override default column names with
@JoinTable.