Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to map this using EJB3?

 
Christopher Au
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a user table which collects such data as residential address and postal address, both addresses are mandatory and I wish to store them on the same table.

I want to use EJB3 + JPA to deal with this situation.

I believe under EJB3 specification that you are required to have the field name as address_id on my user table. This is ok for 1 of the fields what about the other address field.

I can use a joining table for the other address but I want to avoid this.

Thanks

Chris
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe under EJB3 specification that you are required to have the field name as address_id on my user table. This is ok for 1 of the fields what about the other address field.

- Well you can have, many fields regarding to address associated with a particular user. Consider this example,

employee_id
employee_name
address_field1
address_field2
city
state
zip

Now, you can assume this model, if you think that the there is going to be only one address associated with the patient. But if your employee is going to have one or more addresses, then you need to have two rows in the database which would defy 2nd normal form. Probably, you can have three tables like this,

  • employee_id
  • employee_name


  • employee_id
  • address_id


  • address_id
  • address_field1
  • address_field2
  • city
  • state
  • zip




  • I can use a joining table for the other address but I want to avoid this.

    But, if you still want to have the address fields you have something like this,

    employee_id
    employee_name
    address_field1
    address_field2
    city
    state
    zip

    and also have two optional tables,

    employee_id
    address_id


    address_id
    address_field1
    address_field2
    city
    state
    zip

    which are used only to fetch the optional addresses.
     
    Arun Kumarr
    Ranch Hand
    Posts: 661
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I believe under EJB3 specification that you are required to have the field name as address_id on my user table.


    - Can you please elaborate on this? I don't understand why EJB3 says this as a requirement. Probably it recommends this a best practise.
     
    Mike Keith
    author
    Ranch Hand
    Posts: 304
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Chris,

    It sounds like you have mistakenly interpreted the join column defaults in JPA. We defined the defaults to be the name of the attribute, appended by an underscore and the name of the primary key column that it joins to. There is no problem having two different addresses referenced from the same user entity, in fact we even defined the defaults the way we did to allow this and not require additional configuration.

    Your User and Address classes might look like this:

    @Entity
    public class User {
    @Id int id;
    @ManyToOne Address residence;
    @ManyToOne Address postal;
    ...
    }

    @Entity
    public class Address{
    @Id int id;
    String street;
    String city;
    ...
    }

    This would assume an ADDRESS table with columns ID, STREET, CITY, etc, and a USER table with columns ID, RESIDENCE_ID, POSTAL_ID, etc. So you can have as many references to different kinds of addresses as you want. Note that if you don't want the name of the join column to be the default name you can aways override it with a @JoinColumn annotation on the relationship.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic