1 Reply Latest reply on Sep 21, 2012 6:03 AM by nickarls

    Problem with entity persist with bidirectional relationship

    lukasw44

      Hello

       

      Info : in my app i am using Mysql so im using


      @GeneratedValue(strategy=GenerationType.IDENTITY)

      for id key;

       

      So first of all i am modify standard spring mvc example application and add bidirectional relationship with new entities Orders :

       

      My standard supper abstract class Person:

       

       

      @MappedSuperclass
      @Inheritance(strategy=InheritanceType.JOINED)
      public abstract class Person {
      
          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          private Long id;
      
          @NotNull
          @Size(min = 1, max = 25)
          @Pattern(regexp = "[A-Za-z ]*", message = "must contain only letters and spaces")
          private String name;  
      
      
          public Person(String name) {
              super();
              this.name = name;
          }
      

       

      My entity Member extends Person look like:

       

       

      @Entity
      @Table(uniqueConstraints = @UniqueConstraint(columnNames = "email"))
      public class Member extends Person implements Serializable
      {
         /** Default value included to remove warning. Remove or modify at will. **/
         private static final long serialVersionUID = 1L;
      
      
         @NotNull
         @NotEmpty
         @Email
         private String email;
      
         @NotNull
         @Size(min = 10, max = 12)
         @Digits(fraction = 0, integer = 12)
         @Column(name = "phone_number")
         private String phoneNumber;
      
         @OneToMany(cascade=CascadeType.ALL , mappedBy="member")
         private List<UOrder> orders;
      
         public Member() {
          super();
      
         }  
      
           public Member(String name, String email, String phoneNumber ,List<UOrder> orders) {
             super(name);
             this.orders = orders;
             this.email = email;
             this.phoneNumber = phoneNumber;
         }
      ........///getters and setters
      

       

       

      please see field orders and her annotation:

       

      @OneToMany(cascade=CascadeType.ALL , mappedBy="member")

       

       

      And fine my class Order look like :

       

       

      @Entity
      public class UOrder {
      
          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          private Integer id;
      
          private float price; 
      
          @ManyToOne(optional=false)
          private Member member;
      
          private String name;
      

       

       

      So it is simply bi @OneToMany bi directional relationship with entity Member and Order

       

      So i have got cascade all and when i need persist enttiy Membere i also need persist all orders

       

      So i wrote simple Junit test for this operation below all standard test :

       

       

        @Test
          public void testInsertWithOrder(){
              UOrder order = new UOrder(20.0f, "first stuff");
             UOrder order2 = new UOrder(40.0f, "secondary stuff");
              List<UOrder> orders = new ArrayList<UOrder>();
              orders.add(order2);
              orders.add(order);
      
              Member member = new Member("Member name", "member23@gmail.com", "2125552141", orders);
      
              memberDao.register(member);
      
              List<Member> members = memberDao.findAllOrderedByName();
      
              Assert.assertNotNull(members);
              Assert.assertEquals(1, members.size());      
      
          }
      

       

       

      But i have got error first hibernate error :

       

       

       

      NULL not allowed for column "MEMBERID"; SQL statement:
      insert into UOrder (id, memberId, name, price) values (null, ?, ?, ?) [23502-165]
      2012-09-20 17:25:51 org.springframework.test.context.transaction.TransactionalTestExecutionListener endTransaction
      INFO: Rolled back transaction after test execution for test context [[TestContext@308a1f38 testClass = MemberDaoTest, testInstance = org.jboss.tools.example.springmvc.test.MemberDaoTest@5ad3c69c, testMethod = testInsertWithOrder@MemberDaoTest, testException = javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Pole nie moze byc puste "MEMBERID"
      NULL not allowed for column "MEMBERID"; SQL statement:
      

      So what is gooing on ?? plz help i attach this simple application thx for replies