jpa 默认生成sql语句_JPA生成的SQL语句不正确

我在JPA中遇到了一个不特定于持久性提供程序的奇怪错误。我使用的是JPA2.0,我使用的是生成的模式。

简而言之:生成的模式包含一个包含三列的联接表,但是生成的insert语句将该表视为只有两列。

以下是映射:

@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 )

@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 players = new ArrayList();

private String fieldName;

private Boolean archived;

/* constructor, getters and setters follow */

}

@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 Integer number;

private Boolean archived;

@ManyToOne( targetEntity = ClubEntity.class, fetch = FetchType.EAGER )

private Club club;

/* constructor, getters and setters follow */

}

从这些映射创建以下架构:

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

这一行很重要-这是连接表。

create table Game_PLAYER (Game_id bigint not null, homeTeamPlayers_id bigint not null, awayTeamPlayers_id bigint not null)

当我尝试持久化游戏实体(MatchEntity.java)时,会发生以下情况:

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尝试向join表插入两行,每行只影响三行中的两列。

完全去掉映射中的接口

定义显式联接表

也没有解决问题。

编辑:用于紧急获取的代码:

@Transactional(readOnly = true)

public Collection findAll() {

em.createQuery("SELECT m FROM MatchEntity m "

+ "JOIN FETCH m.homeTeamPlayers", MatchEntity.class).getResultList();

List rList = em.createQuery("SELECT m FROM MatchEntity m "

+ "JOIN FETCH m.awayTeamPlayers", MatchEntity.class).getResultList();

Collection result = new ArrayList( rList );

return result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值