2 Replies Latest reply on Jul 11, 2006 11:05 AM by Rigel Freden

    ManyToOne and ManyToMany foreign key issues

    Rigel Freden Newbie

      I'm using jboss 4.04ga, java 1.5.0_06, postgres 8.0.3, etc. When trying to create Many* relationships I find the foreign keys are somehow not being stored. The rest of the fields of the entities are persisted fine, but the relationships do not seem to be. Strangely, OneToOne relationships seem to turn out as expected. I suspect that I have misconfigured jboss somehow. in my example code, table test1 has one entry, field = 1, test2 has two entries, just as expected, only null fk_test1 fields. My entities are at the bottom of this post, I create and persist them as so:
      Test1 t1=new Test1();
      ArrayList<Test2> a=new ArrayList<Test2>();
      Test2 t2=new Test2();
      t2.setField(1);
      a.add(t2);
      t2=new Test2();
      t2.setField(2);
      a.add(t2);
      t1.setTest2s(a);
      t1.setField(1);
      entityManager.persist(t1);
      /*
      * Test1.java
      */
      // package name intentionally obscured
      package entity;

      import java.io.Serializable;
      import java.util.Collection;
      import javax.persistence.CascadeType;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.OneToMany;
      import javax.persistence.Transient;

      @Entity
      public class Test1 implements Serializable {

      @Transient
      private static final long serialVersionUID = 563564566867744L;

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

      private int field;

      @OneToMany(mappedBy="test1",cascade={CascadeType.ALL})
      private Collection<Test2> test2s;

      public Collection<Test2> getTest2s() {
      return test2s;
      }

      public void setTest2s(Collection<Test2> test2s) {
      this.test2s = test2s;
      }

      public int getField() {
      return field;
      }

      public void setField(int field) {
      this.field = field;
      }

      public long getId() {
      return id;
      }

      public void setId(long id) {
      this.id = id;
      }
      }

      /*
      * Test2.java
      */

      package entity;

      import java.io.Serializable;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Transient;

      @Entity
      public class Test2 implements Serializable {

      @Transient
      private static final long serialVersionUID = 7894561357L;

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

      private int field;

      @ManyToOne
      @JoinColumn(name="fk_test1")
      private Test1 test1;

      public Test1 getTest1() {
      return test1;
      }

      public void setTest1(Test1 t1) {
      test1=t1;
      }

      public int getField() {
      return field;
      }

      public void setField(int field) {
      this.field = field;
      }

      public long getId() {
      return id;
      }

      public void setId(long id) {
      this.id = id;
      }
      }

        • 1. Re: ManyToOne and ManyToMany foreign key issues
          Carlo de Wolf Master

          Please try the following:

          Test1 t1=new Test1();
          ArrayList<Test2> a=new ArrayList<Test2>();
          Test2 t2=new Test2();
          t2.setField(1);entityManager.persist(t2);a.add(t2);
          t2=new Test2();
          t2.setField(2);entityManager.persist(t2);a.add(t2);
          t1.setTest2s(a);
          t1.setField(1);
          entityManager.persist(t1);


          • 2. Re: ManyToOne and ManyToMany foreign key issues
            Rigel Freden Newbie

            For that I get: javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: entity.Test2

            on the line that reads: entityManager.persist(t1);

            I guess that this is because the Test2 objects are being re-persisted, so I modified it to read:

            Test1 t1=new Test1();
            ArrayList<Test2> a=new ArrayList<Test2>();
            Test2 t2=new Test2();
            t2.setField(1);
            entityManager.persist(t2);
            a.add(t2);
            t2=new Test2();
            t2.setField(2);
            entityManager.persist(t2);
            a.add(t2);
            // t1.setTest2s(a);
            t1.setField(1);
            entityManager.persist(t1);
            t1.setTest2s(a);
            entityManager.merge(t1);

            the only issue being that the results are as in the initial case: no foreign keys

            I failed to mention it previously, but as to my ManyToMany relationships--the join table is created, and correctly, but there are never any rows inserted, even though either side has the correct data