[英]JPA 2: Referential integrity constraint violation while saving entity with undirectional OneToMany relationship
I'm working on database design and Java implementation of some sports event model. 我正在研究一些体育赛事模型的数据库设计和Java实现。 I have two entities:
Game
and Participant
and undirectional OneToMany relationship between them. 我有两个实体:
Game
和Participant
,它们之间是无方向的OneToMany关系。 Game
can has many Participant
's. Game
可以有很多Participant
。
In database it looks like: 在数据库中,它看起来像:
Of course i have FK constraint on table
event_participant
: 当然我对表
event_participant
有FK约束:
CONSTRAINT `FK_par_eve_eve_id__eve_id`
FOREIGN KEY (`event_id`)
REFERENCES `event` (`id`));
In Java code i wrote this mapping (simplified): 在Java代码中,我编写了此映射(简化):
@Entity
@Table(name = "event")
public class GameEntity extends AbstractPersistable<Long> implements Game {
@Nullable
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "game")
private List<ParticipantEntity> participants;
}
@Entity
@Table(name = "event_participant")
public class ParticipantEntity extends AbstractPersistable<Long> implements GameParticipant {
@ManyToOne
@JoinColumn(name="event_id", referencedColumnName = "id", insertable = true)
private GameEntity game;
}
Test: 测试:
TeamEntity teamHome = teamsDao.findOne(1L);
TeamEntity teamGuest = teamsDao.findOne(2L);
GameEntity newEntity = new GameEntity()
.addParticipant(teamHome, GameParticipant.Alignment.HOME)
.addParticipant(teamGuest, GameParticipant.Alignment.GUEST);
dao.save(newEntity);
public GameEntity addParticipant(TeamEntity team, GameParticipant.Alignment alignment) {
if (participants == null) {
participants = new ArrayList<ParticipantEntity>();
}
participants.add(ParticipantEntity.create(team, alignment));
return this;
}
public static ParticipantEntity create(TeamEntity team, Alignment alignment) {
ParticipantEntity object = new ParticipantEntity();
object.team = team;
object.alignment = alignment;
return object;
}
When i'm trying to save the new GameEntity
object which contains just created Participant
's objects i expect that Hibernate creates one new record in event
table and related records in event_participant
table. 当我尝试保存新的
GameEntity
对象(其中包含刚创建的Participant
对象)时,我希望Hibernate在event
表中创建一条新记录,在event_participant
表中创建相关记录。
But i got an exception: 但我有一个例外:
Referential integrity constraint violation: "FK_PAR_EVE_EVE_ID__EVE_ID: PUBLIC.EVENT_PARTICIPANT FOREIGN KEY(EVENT_ID) REFERENCES PUBLIC.EVENT(ID) (0)"; SQL statement:
insert into event_participant (id, alignment, event_id, participant_id, participant_type) values (null, ?, ?, ?, ?)
This insert query contains NULL value for event_id
field. 此插入查询包含
event_id
字段的NULL值。 I don't understand why. 我不明白为什么。 When i removed DB constraint everything is ok and
event_id
column has the correct value. 当我删除数据库约束时,一切正常,
event_id
列具有正确的值。
All the boilerplate is done by Spring Data util classes. 所有样板都由Spring Data util类完成。 I'm using JPA 2.0, mysql-connector-java 5.1.28, hibernate-entitymanager 4.0.1.Final, spring-data-jpa 1.3.4.RELEASE and H2 database for testing.
我正在使用JPA 2.0,mysql-connector-java 5.1.28,hibernate-entitymanager 4.0.1.Final,spring-data-jpa 1.3.4.RELEASE和H2数据库进行测试。 I got the same result using either H2 or MySQL.
使用H2或MySQL,我得到了相同的结果。
Where i failed? 我哪里失败了?
You didn't set the other side of this bi-directional association: 您没有设置此双向关联的另一面:
public GameEntity addParticipant(TeamEntity team, GameParticipant.Alignment alignment) {
ParticipantEntity pe = ParticipantEntity.create(team, alignment)
participants.add(pe);
pe.setGame(this);
return this;
}
The thing to note is you didn't set the foreign key field for the participant as in pe.setGame(this)
需要注意的是,您没有像
pe.setGame(this)
那样为参与者设置外键字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.