Devskiller / jpa2ddl

JPA Schema Generator Plugin
Apache License 2.0
110 stars 33 forks source link

Multiple Many-to-Many relations between two entities are not resolved correctly #45

Open Slebi opened 3 years ago

Slebi commented 3 years ago

The following relation results (IMHO) in a wrong intermediate table (see below).

Entity A:

@Entity
@Table(name = "measurement")
public class MeasurementEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @OneToMany
    private Set<AmbientSensorReadingEntity> ambientUsedForPreparation;

    @OneToMany
    private Set<AmbientSensorReadingEntity> ambientBefore;

    @OneToMany
    private Set<AmbientSensorReadingEntity> ambientAfter;

   // ...
}

Entity B:

@Entity
@Table(name = "ambient_sensor_reading")
public class AmbientSensorReadingEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    private double temperature;
// ...
}

Results in the following intermediate table, which can not be used by hibernate:

    create table measurement_ambient_sensor_reading (
       MeasurementEntity_id bigint not null,
        ambientUsedForPreparation_id bigint not null,
        ambientBefore_id bigint not null,
        ambientAfter_id bigint not null,
        primary key (MeasurementEntity_id, ambientAfter_id)
    ) engine=InnoDB;

If I change it to:

    create table measurement_ambient_sensor_reading (
    id bigint not null auto_increment,
        MeasurementEntity_id bigint not null,
        ambientUsedForPreparation_id bigint,
        ambientBefore_id bigint,
        ambientAfter_id bigint,
        primary key (id)
    ) engine=InnoDB;

Hibernate can correctly use the database. Do I miss some configuration or I am doing something else wrongly?