Skip to content

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

@Slebi

Description

@Slebi

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions