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

    Problem with entity persist with bidirectional relationship

    lukas lukasz Newbie



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


      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:



      public abstract class Person {
          private Long id;
          @Size(min = 1, max = 25)
          @Pattern(regexp = "[A-Za-z ]*", message = "must contain only letters and spaces")
          private String name;  
          public Person(String name) {
              this.name = name;


      My entity Member extends Person look like:



      @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;
         private String email;
         @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() {
           public Member(String name, String email, String phoneNumber ,List<UOrder> orders) {
             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 :



      public class UOrder {
          private Integer id;
          private float price; 
          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 :



          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>();
              Member member = new Member("Member name", "member23@gmail.com", "2125552141", orders);
              List<Member> members = memberDao.findAllOrderedByName();
              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