I've encountered a strange error with JPA that is not persistence provider specific. I'm using JPA 2.0 and I'm using a generated schema.
In short: The generated schema includes a join table with three columns, but the generated insert statements treat this table as if it had only two columns.
Here are the mappings:
@Entity
@Table( name = "Game" )
public class MatchEntity implements Match, Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Long id;
@ManyToOne( targetEntity = ClubEntity.class )
private Club homeTeam;
@ManyToOne( targetEntity = ClubEntity.class )
private Club awayTeam;
@ManyToMany( targetEntity = PlayerEntity.class )
private Collection<Player> homeTeamPlayers;
@ManyToMany( targetEntity = PlayerEntity.class )
private Collection<Player> awayTeamPlayers;
private String location;
@Temporal( value = TemporalType.DATE )
@Column( name = "Match_Date" )
private Date date;
/* constructor, getters and setters follow */
}
@Entity
@Table( name = "Club" )
public class ClubEntity implements Club, Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Long id;
private String name;
@OneToMany( fetch = FetchType.EAGER, targetEntity = PlayerEntity.class,
mappedBy = "club" )
private Collection<Player> players = new ArrayList<Player>();
private String fieldName;
private Boolean archived;
/* constructor, getters and setters follow */
}
@Entity
@Table( name = "PLAYER" )
public class PlayerEntity implem开发者_JAVA技巧ents Player, Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Long id;
private String firstName;
private String surname;
@Temporal( value = TemporalType.DATE )
private Date birthDate;
@Column( name = "pos" )
@Enumerated( EnumType.ORDINAL )
private Position position;
private Integer number;
private Boolean archived;
@ManyToOne( targetEntity = ClubEntity.class, fetch = FetchType.EAGER )
private Club club;
/* constructor, getters and setters follow */
}
From these mappings, the following schema gets created:
create table Club (id bigint generated by default as identity (start with 1), archived bit, fieldName varchar(255) not null, name varchar(255) not null, primary key (id))
create table Game (id bigint generated by default as identity (start with 1), Match_Date date, location varchar(255), awayTeam_id bigint, homeTeam_id bigint, primary key (id))
create table Game_PLAYER (Game_id bigint not null, homeTeamPlayers_id bigint not null, awayTeamPlayers_id bigint not null)
create table PLAYER (id bigint generated by default as identity (start with 1), archived bit, birthDate date, firstName varchar(255) not null, number integer, pos integer, surname varchar(255) not null, club_id bigint, primary key (id))
alter table Game add constraint FK21C0123B2A3B9E foreign key (homeTeam_id) references Club
alter table Game add constraint FK21C012F5972EAF foreign key (awayTeam_id) references Club
alter table Game_PLAYER add constraint FK267CF3AE6AE1D889 foreign key (Game_id) references Game
alter table Game_PLAYER add constraint FK267CF3AED51EDECF foreign key (homeTeamPlayers_id) references PLAYER
alter table Game_PLAYER add constraint FK267CF3AE6CBE869E foreign key (awayTeamPlayers_id) references PLAYER
alter table PLAYER add constraint FK8CD18EE13F2C6C64 foreign key (club_id) references Club
This line is important - this is the join table.
create table Game_PLAYER (Game_id bigint not null, homeTeamPlayers_id bigint not null, awayTeamPlayers_id bigint not null)
When I try to persist the Game entity (MatchEntity.java), this happens:
insert into Game_PLAYER (Game_id, awayTeamPlayers_id) values (?, ?)
Hibernate: insert into Game_PLAYER (Game_id, awayTeamPlayers_id) values (?, ?)
binding '2' to parameter: 1
binding '1' to parameter: 2
reusing prepared statement
insert into Game_PLAYER (Game_id, awayTeamPlayers_id) values (?, ?)
Hibernate: insert into Game_PLAYER (Game_id, awayTeamPlayers_id) values (?, ?)
binding '2' to parameter: 1
binding '2' to parameter: 2
done inserting collection: 2 rows inserted
Inserting collection: [football.model.entities.MatchEntity.homeTeamPlayers#2]
Executing batch size: 2
about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Could not execute JDBC batch update [insert into Game_PLAYER (Game_id, awayTeamPlayers_id) values (?, ?)]
JPA tries to insert two rows to the join table, each affecting only two columns of the three.
What I have tried:
- Getting rid of the interfaces in the mappings altogether
- Defining an explicit join table
- Using OpenJPA instead of Hibernate
Neither did resolve the problem.
edit: code for eager fetching:
@Transactional(readOnly = true)
public Collection<Match> findAll() {
em.createQuery("SELECT m FROM MatchEntity m "
+ "JOIN FETCH m.homeTeamPlayers", MatchEntity.class).getResultList();
List<MatchEntity> rList = em.createQuery("SELECT m FROM MatchEntity m "
+ "JOIN FETCH m.awayTeamPlayers", MatchEntity.class).getResultList();
Collection<Match> result = new ArrayList<Match>( rList );
return result;
}
Perhaps you need different join tables for homeTeamPlayers
and awayTeamPlayers
:
@ManyToMany( targetEntity = PlayerEntity.class )
@JoinTable(name = "Game_HomeTeamPlayers")
private Collection<Player> homeTeamPlayers;
@ManyToMany( targetEntity = PlayerEntity.class )
@JoinTable(name = "Game_AwayTeamPlayers")
private Collection<Player> awayTeamPlayers;
精彩评论