Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
AW: [eclipselink-users] ManyToMany did not persist in both directions

Hi,

this works as expected:

you are establishing a bidirectional relationship between Schueler and Lehrer. Lehrer is the owning side. In JPA, you have to maintain a bidirectional relationship on both sides.

See §3.2.4 of the JPA Spec 2.0:

"Bidirectional relationships between managed entities will be persisted based on references held by the
owning side of the relationship. It is the developer's responsibility to keep the in-memory references
held on the owning side and those held on the inverse side consistent with each other when they change."

-Adrian


Adrian Görler
SAP AG

Pflichtangaben/Mandatory Disclosure Statements: http://www.sap.com/company/legal/impressum.epx


-----Ursprüngliche Nachricht-----
Von: eclipselink-users-bounces@xxxxxxxxxxx [mailto:eclipselink-users-bounces@xxxxxxxxxxx] Im Auftrag von farisola@xxxxxx
Gesendet: Sonntag, 24. Januar 2010 15:05
An: eclipselink-users@xxxxxxxxxxx
Betreff: [eclipselink-users] ManyToMany did not persist in both directions

Hello, 
using annotation @ManyToMany did not persist 
in both directions:

I have to n:m related tables, LEHRER and SCHUELER

That results in 3 database tables:

1. LEHERER
2. SCHUELER
3. LEHERER_SCHUELER

At the end you can find the two corresponding entity classes
and the two test cases.

Both entites contain the neccessary @ManyToMany annotations, see below.
The entity LEHERER contains the @JoinTable annotation, the entity SCHUELER not.

The problem:
>>>
If I persist a new LEHRER entity that contains some SCHUELER,
then all three tables will have the corresponding entries.

But if I persist a new SCHUELER entity that contains some LEHRER ,
then ONLY the tables SCHUELER and LEHRER will have the corresponding entries,
but the table LEHERER_SCHUELER will be ignored.
<<<

I think the persistence should work in both directions independent of the place,
where the @JoinTable annotation is placed.

All is running on a Oracle 10g Database.

Did someone have an idea, what is wrong?

// +-----------------------------------------------------------------------+
@Entity
@Table(name="LEHRER")
public class Lehrer extends ZAdminEntity implements Serializable {
  private static final long serialVersionUID = 1L;
  private long pk;
  private String lehrername;
  private List<Schueler> schuelers;

  public Lehrer() { }

  @Id
  @SequenceGenerator(name="LEHRER_PK_GENERATOR", sequenceName="SEQ_LEHRER")
  @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="LEHRER_PK_GENERATOR")
  @Column(unique=true, nullable=false, precision=22)
  public long getPk() {
    return this.pk;
  }

  public void setPk(long pk) {
    this.pk = pk;
  }

  @Column(nullable=false)
  public String getLehrername() {
    return this.lehrername;
  }

  public void setLehrername(String lehrername) {
    this.lehrername = lehrername;
  }

  //bi-directional many-to-many association to Schueler
  @ManyToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
  @JoinTable(
    name="LEHRER_SCHUELER"
      , joinColumns={@JoinColumn(name="LEHRER_PK", nullable=false)}
      , inverseJoinColumns={@JoinColumn(name="SCHUELER_PK", nullable=false)}
    )
  public List<Schueler> getSchuelers() {
    return this.schuelers;
  }

  public void setSchuelers(List<Schueler> schuelers) {
    this.schuelers = schuelers;
  }
}

// +-----------------------------------------------------------------------+
@Entity
@Table(name="SCHUELER")
public class Schueler extends ZAdminEntity implements Serializable {
  private static final long serialVersionUID = 1L;
  private long pk;
  private String schuelername;
  private List<Lehrer> lehrers;

  public Schueler() {}

  @Id
  @SequenceGenerator(name="SCHUELER_PK_GENERATOR", sequenceName="SEQ_SCHUELER")
  @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SCHUELER_PK_GENERATOR")
  @Column(unique=true, nullable=false, precision=22)
  public long getPk() {
    return this.pk;
  }

  public void setPk(long pk) {
    this.pk = pk;
  }

  @Column(nullable=false)
  public String getSchuelername() {
    return this.schuelername;
  }

  public void setSchuelername(String schuelername) {
    this.schuelername = schuelername;
  }

  //bi-directional many-to-many association to Lehrer
  @ManyToMany(mappedBy="schuelers", cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
  public List<Lehrer> getLehrers() {
    return this.lehrers;
  }

  public void setLehrers(List<Lehrer> lehrers) {
    this.lehrers = lehrers;
  }

}
// +-----------------------------------------------------------------------+
  @Test
  /**
   * Is working!
   */
  public void lehrerschueler() {
    EntityManagerFactory emf = getEMF();
    EntityManager em = emf.createEntityManager();
    Lehrer lehrer = this.getNewLehrer();
    Schueler schueler = this.getNewSchueler();
    ArrayList  <Schueler> list = new ArrayList <Schueler> ();
    list.add(schueler);
    lehrer.setSchuelers(list);
    em.getTransaction().begin();
    try {
      // persist the lehrer
      em.persist(lehrer);
      em.getTransaction().commit();
    } catch (Exception e) {
      e.printStackTrace();
      Assert.fail(e.getMessage());
    }
  }
// +-----------------------------------------------------------------------+

  @Test
  /**
   * IS NOT working!
   */
  public void schuelerlehrer() {
    EntityManagerFactory emf = getEMF();
    EntityManager em = emf.createEntityManager();
    Schueler schueler = this.getNewSchueler();
    Lehrer lehrer = this.getNewLehrer();
    ArrayList <Lehrer> list = new ArrayList <Lehrer> ();
    list.add(lehrer);
    schueler.setLehrers(list);
    em.getTransaction().begin();
    try {
      // persist the schueler
      em.persist(schueler);
      em.getTransaction().commit();
    } catch (Exception e) {
      e.printStackTrace();
      Assert.fail(e.getMessage());
    }
  }
// +-----------------------------------------------------------------------+

greetings Farisola

-- 
Haiti-Nothilfe! Helfen Sie per SMS: Sende UIHAITI an die Nummer 81190.
Von 5 Euro je SMS (zzgl. SMS-Gebühr) gehen 4,83 Euro an UNICEF.
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top