2

I'm using JPA and Hibernate as provider with MySQL DBMS and I remarked that the cascade deleting does not work for my situation :

    @Entity
    public class Entity_1{

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;
   private String nomAttribute;

   @ManyToMany(cascade={CascadeType.PERSIST, CascadeType.REMOVE})
   private java.util.List<Entity_2> et2;

   ...
   }

and the second Entity is

  @Entity
  public class Entity_2{
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private int id;
  private String nomAttribute;
    ...
  }

the result is a three tables

Entity_1 ,Entity_2, Entity_1_Entity_2

I remarked that when I delete a Entity_1 the Entity_2 is too deleted because of the cascade on deleting .

what I want is when I delete Entity_1 the relation between Entity_1 and Entity_2 deleted only not the Entity_2 and I tried Many options in but all in vain

What the option should I use or there are no options for that and I should use Triggers ??

K.Ariche
  • 1,188
  • 3
  • 14
  • 29
  • No you don't need triggers, check out this link: http://stackoverflow.com/questions/9108224/can-someone-please-explain-mappedby-in-hibernate – zmf May 21 '14 at 20:25
  • they are not talking about cascade deleting ,they are talking about bidirectional relation,if not so could you specify where exactly plz – K.Ariche May 21 '14 at 20:34

1 Answers1

1

What about this solution ?

Class Entity1 :

@Entity
public class Entity1 {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "entity1")
    private Collection<Entity1Entity2> collection;

    ...
}

Class Entity2 :

@Entity
public class Entity2 {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "entity2")
    private Collection<Entity1Entity2> collection;

    ...
}

Class Entity1Entity2 (which is the join table) :

@Entity
@IdClass(Entity1Entity2Pk.class)
public class Entity1Entity2 {

    @ManyToOne
    @Id
    private Entity1 entity1;

    @ManyToOne
    @Id
    private Entity2 entity2;

    ...    
}

Class Entity1Entity2Pk (which is needed for Entity1Entity2) :

public class Entity1Entity2Pk {

    private int entity1;

    private int entity2;

}

Update :

I added mappedBy = "entity1" and mappedBy = "entity2". In that way @oneToMany associations won't create extra join tables.

Basemasta
  • 381
  • 1
  • 13
  • supposing that this is a solution ,to implement this solution I have to change all my classes (I have a lot) and that will generate a great number of classes and tables, could you give me some other solutions it it's possible – K.Ariche May 21 '14 at 20:26
  • @user3660257 This will not generate any more tables than using `@ManyToMany` like you have above. In your example, JPA creates a table to represent the linkage of `@ManyToMany`. In Basemasta's example, there is still a linking table, but he's managing it as an entity. I **always** use this strategy instead of `@ManyToMany`. One key reason is that most of the time, record linkages themselves are decorated with additional data, like "when was the linkage made?". In this case, you simply add non-key fields to `Entity1Entity2` – Glenn Lane May 21 '14 at 21:20
  • @GlennLane according to what I saw in a tuto I had understand that oneToMany association also generate a third table for join the two entities isn't it ? [tuto](http://blog.paumard.org/cours/jpa/chap03-entite-relation.html#d0e1324) – K.Ariche May 21 '14 at 21:35
  • Answer updated with mappedBy in @oneToMany associations. So those associations will not generate join tables. – Basemasta May 21 '14 at 22:04