0

I have a ManyToMany relation between two entity

@javax.persistence.Entity
@Table(name = "t_aircraft_model")
public class AircraftModel extends DbObject {

    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name = "t_aircraft_model_entity", joinColumns = { @JoinColumn(name = "aircraft_model_uid", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "entity_id_LDAP", nullable = false) })
    private List<com.airbushelicopter.ahead.db.pojo.Entity> entities ;

But sqlServer doesn't allow me to publish the intermediate table : t_aircraft_model_entity

new publication

I thought about 2 solutions

  • Both column of the table the t_aircraft_model_entity become the primary key (ok in my case a aircraft can't be linked multiple time to the same entity)
  • I add a 3rd column (id) which will be the primary key
  • Or ?

But I have no idea how I can do this with hibernate and annotation.

thanks !

jpprade
  • 3,497
  • 3
  • 45
  • 58

1 Answers1

1

First things first. You will need 3 tables to make a many to many relation, of course, you will need to make sure that both of your other tables have a PK

On the code side, you can do like this:

Your Airplace Model:

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinTable(name = "t_aircraft_entity_relation",joinColumns = {
        @JoinColumn(name = "aircraftid", nullable = false, updatable = false)},
        inverseJoinColumns = { @JoinColumn(name = "entityid",nullable = false,updatable= false)         
})
private Set<com.airbushelicopter.ahead.db.pojo.Entity> entities ;

On your Entity Model:

@ManyToMany(fetch = FetchType.EAGER,mappedBy="entities")    
private Set<AircraftModel> aircrafts;

And you will have to create a relation table, like in my example:

CREATE TABLE t_aircraft_entity_relation
(
  aircraftid integer NOT NULL,
  entityid integer NOT NULL,
  CONSTRAINT "PK_AIRCRAFT_ENTITY" PRIMARY KEY (aircraftid, entityid),
  CONSTRAINT "FK_AIRCRAFT_ENTITY" FOREIGN KEY (aircraftid)
      REFERENCES t_aircraft_model (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FK_ENTITY_AIRCRAFT" FOREIGN KEY (entityid)
      REFERENCES t_entity_model (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

PS: This piece of SQL is based on Postgresql, so you will have to do a little bit of change.