Hello,
I'm using JPA with Hibernate 3.5.6 as the persistence provider. I've mapped some entities using JPA annotations and I'm using database schema generated by Hibernate, but I'm encountering incorrect SQL insert statements in runtime when trying to persist on of the entities.
Here are the entitites:
Code:
@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 homeTeamPlayers;
@ManyToMany(targetEntity=PlayerEntity.class)
private Collection awayTeamPlayers;
private String location;
@Temporal( value = TemporalType.DATE )
private Date dateHeldOn;
protected MatchEntity() {
}
@Override
public Club getAwayTeam() {
return awayTeam;
}
//... other getters ...
}
The Player entity looks like this:
Code:
@Entity
@Table( name = "Player" )
public class PlayerEntity implements 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 int number;
protected PlayerEntity() {
}
@Override
public Date getBirthDate() {
return birthDate;
}
// ...other getters...
}
The schema generated by Hibernate looks like this:
Code:
# create table Game (id bigint generated by default as identity (start with 1), dateHeldOn 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), birthDate date, firstName varchar(255), number integer not null, pos integer, surname varchar(255), primary key (id))
When I try to persist the MatchEntity, one of the generated insert statements is incorrect. It tries to do two insert statements instead of one. The first insert is for awayTeamPlayers_id and the other is for homeTeamPlayers_id.
Code:
Hibernate: insert into Game_Player (Game_id, awayTeamPlayers_id) values (?, ?)
binding '1' to parameter: 1
binding '2' to parameter: 2
SQL Error: 0, SQLState: null
failed batch
Does anyone have an idea what could be wrong? I don't think that the interface types are causing this. I have specified the target entities and I also tried to get rid of the interface types and the problem just didn't go away.
I'll be grateful for any help.
Vladimir