aspose file tools
The moose likes Object Relational Mapping and the fly likes how to map this using EJB3? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "how to map this using EJB3?" Watch "how to map this using EJB3?" New topic
Author

how to map this using EJB3?

Christopher Au
Greenhorn

Joined: Jul 26, 2007
Posts: 21
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

Joined: May 16, 2005
Posts: 504

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.


    If you are not laughing at yourself, then you just didn't get the joke.
    Arun Kumarr
    Ranch Hand

    Joined: May 16, 2005
    Posts: 504

    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

    Joined: Jul 14, 2005
    Posts: 304
    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.


    -Mike
    Pro JPA 2: Mastering the Java Persistence API
     
    IntelliJ Java IDE
     
    subject: how to map this using EJB3?
     
    Threads others viewed
    JTextField problem
    Table Design Question
    Hibernate Made Easy
    Can a CMP EJB be a CMP field inside another CMP EJB
    JPA data retrieval query
    WebSphere development made easy
    without the weight of IBM tools
    http://www.myeclipseide.com