8 Replies Latest reply on Aug 6, 2008 8:21 PM by some one

    How to update foreign key on associated entity?

    some one Newbie

      Using Seam 2.0.2 SP1, MySQL 5, Tomcat 6, I have a one-to-one bidirectional relationship using the foreign key as:


      member(id, ...)
      memberaddress(id, ...)


      The member class has:



      public class Member ...
      {
      ...
          @Id
          @Column(name = "member_id")
          @GeneratedValue(strategy = AUTO)
          public int getId() {
              return id;
          }
      
          @OneToOne(optional = true, cascade = ALL, fetch = EAGER)
          @JoinColumn(name="member_id", unique = true,
            updatable = false)
          public MemberAddress getMemberAddress()
          {
              return memberAddress;
          }
      ...
      }



      The member address class has:


      public class MemberAddress ...
      {
          @OneToOne(mappedBy = "memberAddress",
            optional = false, cascade = ALL)
          public Member getMember()
          {
              return member;
          }
      
          @Id
          @Column(name = "member_id")
          public int getId()
          {
              return id;
          }
      ...
      }



      In my application I call:



        // Member newMember = Member();
        ...
        MemberAddress ma = new
         stateOrProvince, zipOrpostalCode);
        ma.setId(newMember.getId());
        newMember.setMemberAddress(ma);
        ma.setMember(newMember);
        em.persist(newMember);



      Both entities are updated to the MySQL DB. However, the MemberAddress id value (foreign/primary key) is not updated (it still has the -1 value that was set during construction) because the memberaddress does not have the foreign key set. If get the MySQL DDL from this statement:



      mysqldump -u mysqlaccount -p mydb
        --no-data  >  script_file.sql




      I can see the memberaddress table which lacks a foreign key:



      CREATE TABLE 'memberaddress' (
        'member_id' int(11) NOT NULL,
        'mailingaddress' varchar(255) default NULL,
        'stateorprovince' varchar(255) default NULL,
        'ziporpostalcode' varchar(255) default NULL,
        PRIMARY KEY  ('member_id')




      How do I get Hibernate to update the weak entity MemberAddress's foreign key which references the parent entity? And actually I thought with all the annotations I was using, that this was suppose to be already taken care of for me. Or am I missing something?


      Any help most appreciated.

        • 1. Re: How to update foreign key on associated entity?
          Francisco Jose Peredo Noguez Master

          Have you read about cascading in the core documentation, and how to configure it using annotations?


          BTW I think this is a question for the Hibernate Forums, if you have further questions please make them there.


          • 2. Re: How to update foreign key on associated entity?
            some one Newbie

            Have you read about cascading in the core documentation,



            Yes I have but it doesn't have anything to do with annotations unfortunately. Also the link you've directed me to points to using sessions. I'm using Seam's injected entity manager which is an entirely different animal.



            and how to configure it using annotations?

            Yes I have. The link you've provided me points to annotations that I'm already in my code. I am using CascadeType.ALL so any and all cascading should be performed. I'm also going by how the hibernate example of one-to-one is given based on this document:


            2.2.5.1. OneToOne


            But still with no luck.




            BTW I think this is a question for the Hibernate Forums, if you have further questions please make them there.

            Problem is if I mention Seam in there (the hibernate forum), no one touches it because they all consider the question to be a Seam issue. One forum tells me to go to the other forum, and round and round I go. Also, and like I mentioned earlier, I'm using Seam's injected entity manager which is of type:
            org.jboss.seam.persistence.FullTextEntityManagerProxy


            ... so the persistence implementation is very much within the Seam framework. Sorry if I sound terse, but I get the impression that you didn't read the context of my problem.

            • 3. Re: How to update foreign key on associated entity?
              Francisco Jose Peredo Noguez Master

              Maybe you are missing em.flush() or not committing the transaction (what FlusModeType are you using?).


              • 4. Re: How to update foreign key on associated entity?
                some one Newbie

                Francisco,


                Thanks for getting back to me so quickly. the flush() method seemed to be the problem. Thank you for your help and your patience, very much appreciated.


                :)

                • 5. Re: How to update foreign key on associated entity?
                  some one Newbie

                  I spoke too soon. I get problems with duplicate id values after updating with a second record (but thanks anyways). I think I'm going to have to re-evaluate whether or not to use Hibernate and Seam since using them cannot produce a simple foreign key relationship. I've tried doing it based on what the hibernate annotations documentation prescribes, what the JPA docs prescribe, and several other sites but no foreign key in the underlying DDL in MySQL.

                  • 6. Re: How to update foreign key on associated entity?
                    Alexander Torstling Newbie

                    I can assure you that it works, and it works for hundreds of entities. I think that updatable causes the problem.

                    • 7. Re: How to update foreign key on associated entity?
                      Francisco Jose Peredo Noguez Master

                      Yes, I agree, what is that updatable = false doing there? IMHO it shouldn't be there.

                      • 8. Re: How to update foreign key on associated entity?
                        some one Newbie

                        I assure you it doesn't ... at least not the way the documentation is suggesting that it does. Also, updatable has no impact.


                        There is a bug report opened which is called Foreign key name on @OneToOne which is found at the Hibernate JIRA web site.


                        The description for this bug is:




                        Foreign key constraint name cannot be changed using @org.hibernate.annotations.ForeignKey on a @OneToOne with foreign key. It doesn't matter which class is annotated with @ForeignKey.

                        The bug was reported 22/Oct/07 and is unresolved. So creating a foreign key for a one-to-one relationship using the ForeignKey annotation has been broken for almost a year. I can't see how this is considered a minor lost of function since this seems to be a pretty important piece of functionality.


                        The Hibernate annotations documentation in section 2.4.5. Single Association related annotations, states that:



                        Foreign key constraints, while generated by Hibernate, have a fairly unreadable name. You can override the constraint name by use @ForeignKey.

                        Which alludes to the generation of foreign keys but shows no examples on how one goes about forcing Hibernate to generate and use these fairly unreadable foreign key names. And again, using the Foreign key annotation (mentioned in the above bug report) doesn't work.


                        Incidentally, the above bug report points to an original forum post which actually has a solution. Half way down the page, a fellow going by the name of efriedman offers a solution that uses the GenericGenerator annotation. He sets the strategy to the string value foreign. How on Earth is a developer suppose to guess that this would work? This really should be in the documentation - either Seam or Hibernate or better yet, both ... since Hibernate annotations are an integral part of Seam.


                        It doesn't matter how great the framework is, if developers can't figure out how to use it, they won't.