4 Replies Latest reply on Nov 14, 2006 5:50 AM by divxyoda

    Advaned problems with Jboss and Hibernate !

    divxyoda

      Hi every body,
      Look at these classes examples to see my problems with hibernate on jboss (for a mySql database) and understand what i want to do :

      @Entity
      @Table(name=Mother)
      @Inheritance(strategy = InheritanceType.JOINED)
      public class Mother implements Serializable{
      @Id
      private String mother_id;
      public Mother(){}

      //... getters/setters generated for all fields members
      }

      Note : The Mother table has a single primary key : its id



      @Embeddable
      public Class DaughterKey implements Serializable{
      private String mother_Id;
      private String daughter_id;
      public DaughterKey(){}

      //getters/setters generated for the both fields

      // the methods : toString(),hasCode(),equals() are overrided
      }

      Note : the DaughterKey is a class that specifies (according to my understandings) a composite primary key class when mappings happen !



      @Entity
      @Table(name = "Daughter")
      @IdClass(DaughterKey.class)
      @PrimaryKeyJoinColumn(name = "Mother_id")
      public class Daughter extends Mother{
      @Id
      private String daughter_id;
      public Daughter(){ super();}

      //.. getter/setter for all fields
      }

      Note: the Daughter table has a composite key (mother_Id+daughter_id).




      So,the idea is that eventhough a Daughter Class extends a Mother Class, the Daughter must have 2 fields-members as PrimaryKey in the Database (the mother_id and its own daughter_id) : a composite key.

      After compiling and creating a JAR file, I try to deploy the JAR-file and i have a such exception :

      java.lang.reflect.InvocationTargetException
      //.... at ....
      caused by

      org.hibernate.AnnotationException : Unable to define/override @Id(s) on a subclass : Daughter.

      Thanks a lot if someOne can help me, I know that it's because my conception is not correct but I can't get any better ided.


      PS : Ask your questions, if you don't understand my idea !
      Best regards, Divx.

        • 1. Re: Advaned problems with Jboss and Hibernate !
          konstantin.ermakov

          Hi!

          I think you should use the composite key as an id in your daughter class, I think it is so in EJB3 spec.

          P.S. May the force be with you

          • 2. Re: Advanced problems with Jboss and Hibernate !
            divxyoda

            Hi konstantin thanks alot for your reply, do you mean that i shoul have :

            @Entity
            @Table(name = "Daughter")
            @IdClass(DaughterKey.class)
            @PrimaryKeyJoinColumn(name = "Mother_id")
            public class Daughter extends Mother{

            private String daughter_id;

            @Id
            private DaughterKey myKey=new DaughterKey();

            public Daughter(){
            super();
            myKey.setMotherId(super.getId());
            myKey.setDaughterId(getDaugherId())),
            }

            //.. getter/setter for all fields
            }




            instead of :



            @Entity
            @Table(name = "Daughter")
            @IdClass(DaughterKey.class)
            @PrimaryKeyJoinColumn(name = "Mother_id")
            public class Daughter extends Mother{
            @Id
            private String daughter_id;
            public Daughter(){ super();}

            //.. getter/setter for all fields
            }

            Is it your idea ?

            • 3. Re: Advanced problems with Jboss and Hibernate !
              konstantin.ermakov

              Yes, exactly.

              For me it was a big problem to understand how the composite key concept works. But it became clear after I generated some beans from the datbase structure with the JAG product: http://jag.sourceforge.net.

              I can recommend you to generate some examples and you will see the best way of Database/ EJB implementation....

              • 4. Re: Advaned problems with Jboss and Hibernate !
                divxyoda

                Hi konstantin, thanks you so much for your help. I'm gonna try to use JAG. Once again, thanks, i think that i'm on the right way to understand the composite keys mechanism...

                Best regards, DivxYoda