简体   繁体   中英

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. I have two entities: Game and Participant and undirectional OneToMany relationship between them. Game can has many Participant 's.

In database it looks like:

在此处输入图片说明 Of course i have FK constraint on table event_participant :

CONSTRAINT `FK_par_eve_eve_id__eve_id`
    FOREIGN KEY (`event_id`)
    REFERENCES `event` (`id`));

In Java code i wrote this mapping (simplified):

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

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. I don't understand why. When i removed DB constraint everything is ok and event_id column has the correct value.

All the boilerplate is done by Spring Data util classes. 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. I got the same result using either H2 or 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM