Question

I am encountering a serious issue with Hibernate's hbm2ddl and MySQL: Hibernate systematically creates a unique index for the one of the fields of my OneToMany associations as follows:

screen capture

Notice the UQ box is checked for the day_to_time_slots field which does not make any sense...

Here is the relationship in the Advertisement entity:

@OneToMany
private List<DayToTimeSlot> dayToTimeSlots;

Here is the DayToTimeSlot entity:

@RooJavaBean
@RooToString
@RooEquals
@RooJpaEntity
@Entity
public class DayToTimeSlot {

    @NotNull(groups = { Default.class, Validation.AdvertisementCreation.class })
    @Enumerated
    private Day day;

    @NotNull(groups = { Default.class, Validation.AdvertisementCreation.class })
    @Enumerated
    private TimeSlot timeSlot;
}

Can anyone please tell me how to prevent this behavior?

Was it helpful?

Solution

That makes perfectly sense - it works exactly as it should work. Table advertisement_day_to_time_slots is a join table between Advertisement and DayToTimeSlot entities. Relation between these entities is built with join table because association is unidirectional one-to-many.

This join table do have unique constraint in day_to_time_slot column, because each DayToTimeSlot can be connected only to the one Advertisement entitity. Inverse of OneToMany is ManyToOne. One Advertisement can be connected to many DayToTimeSlot entities, but one DayToTimeSlot is associated only with one Advertisement. If such a limitation is not preferred, @ManyToMany association should be used instead of OneToMany.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top