3 Replies Latest reply on Mar 16, 2009 6:39 AM by wolfgangknauf

    NewBie @OneToOne question

    delarue

      Hi, I am currently learning EJB3 and I don't know express in EJB3 the following

      SQL:

      USER(
      user_id : int primary key
      mycar_id : int
      foreignkey(mycar_FK) (mycar_id, CAR.id)
      )

      CAR(car_id : int primary key)

      This is a OneToOne association

      @Entity
      @Table(name="USER")
      class User{
       @Id
       @Column(name="user_id")
       protected int userId;
       ...
       @OneToOne
       @JoinColumn(name="mycar_FK",
       referencedColumnName="xxx")
       public Car getMyCar() ...
      }



      I don't know if referencedColumnName identifies the column USER.mycar_id or the column of the target id, CAR.car_id ?

      If it is mycar_id, how is done the identification of the id of CAR, CAR.car_id ?
      If it is CAR.car_id, how is identified the mycar_id column ?

      @Entity
      ...
      class Car {
       @Id
       @Column(name="car_id")
       protected int carId;
      ...
      }


      I have the EJB in action book and often the foreign key and primary key have the same column name, which cause misunderstanding from my side.

      Is there a sample server, which genererate the SQL code from a EJB annotation ... without testing it on a free base ?

      I understood that in EJB3, the schema of a database should be created outside of the EJB3 annotation. EJB annotation should be a model expression of some sql definition. Therefore, I try to understand the EJB modeling of some SQL samples.

      Thank's for help

        • 1. Re: NewBie @OneToOne question
          wolfgangknauf

          Hi,

          I think that the "name" attribute of the "@JoinColumn" denotes the column name in your "User" table. The "referencedColumn" should be the name of the primary key column in your child table.

          So, to achieve your sql snippet, your relationship should be:

          @OneToOne
           @JoinColumn(name="mycar_id",
           referencedColumnName="car_id")
           public Car getMyCar() ...


          But normally the @OneToOne annotation should work without any @JoinColumn annotations. The server will choose defaults for the column names. So, start with the easiest solution and remove the @JoinColumn annotation ;-).

          Hope this helps

          Wolfgang

          • 2. Re: NewBie @OneToOne question
            delarue

             

            "Wolfgang Knauf" wrote:
            Hi,

            I think that the "name" attribute of the "@JoinColumn" denotes the column name in your "User" table. The "referencedColumn" should be the name of the primary key column in your child table.


            Ok, I understand better. I found my id and columns back ...

            With this form, the sql generates a constraint with a random name for the foreign key. On the trace of jboss, I see something like :

            alter table .Car add index FK107B4B78C84CE (USER_ID), add constraint FK107B4B78C84CE foreign key (USER_ID) references .User (id)
            


            Is there a way of naming the foreign key : USER_CAR_FK instead of FK107B4B78C84CE ?
            Since the design was done in the database and not in the EJB annotation.

            Is it possible to have a strict mapping between a sql schema and a EJB annotation ?


            But normally the @OneToOne annotation should work without any @JoinColumn annotations. The server will choose defaults for the column names. So, start with the easiest solution and remove the @JoinColumn annotation ;-).

            Hope this helps

            Wolfgang


            This helps a lot ;))) I'll follow your suggestion for small apps, but for big apps, I have to replicate the database schema, thus I try that.

            • 3. Re: NewBie @OneToOne question
              wolfgangknauf

              Hi,

              as far as I know it is not possible to define the foreign key names.

              Best regards

              Wolfgang