Results 1 to 11 of 11
  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Hibernate Mappings

    My question is: In the different types of mappings(one-one, one-many, many-one, many-many) the extra column should be on which side of the relationship? No tutorial seems to explain it.

    Is there any trick to remember these mappings? I see that even the hibernate official documentation does not give concrete example on the mappings.

    Help is appreciated.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Hibernate Mappings

    Hi,

    these relationships come from the relational databases. So if you want to read about it, you shuld read about relational databases. Then you should find the documentation that you are looking for.

    With kind regards,

    Konrad

  3. #3
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Hibernate Mappings

    Any gud link/reference?

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Hibernate Mappings

    Hi,

    sorry, I had the impression that you was reading a lot already so I asumed that you might have some favourite sites already. (It is really nice to have the feeling that the person who asked a question already spend some time reading and understanding the issue!)

    I don't have good links for this because I mainly read books if required.
    But I did a search and found a few sites that might be usefull:
    An introduction to databases is an introduction to databases including relationships.
    Database Relationships: Foreign Keys, Joins, E-R Diagrams
    gives a quick overview about different relationships


    Also be aware that 1:n is the same as n:1 - you just switched the view. So you can have a relationship of coin -> owner where each coin has exacly one owner and a owner can own multiple coins. So this relationship does not change if you look at owner -> coin.

    With kind regards,

    Konrad

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Hibernate Mappings

    And maybe you should check the documentation for your prefered database. I know that there are a lot of good books regarding Database development with SQL Server. I just hope that such books also exist for mysql, postgresql or whatever database you prefer to use.

    Konrad

  6. #6
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Hibernate Mappings

    Is there a rule saying that the extra column should be added to which side of the relationship? Or can we have an implementation where we actually place the extra column on the other table?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Hibernate Mappings

    Well the assumption is that you understand SQL and foreign key relationships and can from that understanding envision what the object structure is going to be like. Lets take a one to many relationship: person to address. One person can have many addresses.

    Java Code:
    public class Person {
    
       private Set<Address> addresses;
    } 
    
    public class Address {
    
      private Person person;
    }
    See? Its a 1:N SQL relationship and you map it the same way in the objects.

    It might benefit you to not start from the level of Hibernate which is one of many persistence providers, but to start high level with the standardized JPA specification; its a specification that Hibernate also implements, as well as a few other ORM frameworks. The above example would then become:

    Java Code:
    @Entity
    public class Person {
    
      @Id
      private Integer id;
    
      @OneToMany(mappedBy="person")
      public Set<Address> addresses;
    }
    
    @Entity
    public class Address {
    
      @ManyToOne
      @JoinColumn(name = "person_id")
      private Person person;
    }


    When I was learning this stuff I used the Toplink JPA documentation as a brain backup until I had it all memorized:

    TopLink JPA Annotation Reference

    But I also had a good book to help me get started; the latest version is called "Pro JPA 2" and I highly recommend it.

    Pro JPA 2 (Expert&#39;s Voice in Java): Mike Keith, Merrick Schincariol: 9781430249269: Amazon.com: Books

    ORM persistence is a really difficult topic so I would not limit yourself to only the internet; its not going to make your life an easy one.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Hibernate Mappings

    Quote Originally Posted by jmohandos304 View Post
    Is there a rule saying that the extra column should be added to which side of the relationship? Or can we have an implementation where we actually place the extra column on the other table?
    For a relational database there's only one place for the extra column in a 1..n relationship.
    For an n..n there's a mapping table.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Hibernate Mappings

    Quote Originally Posted by Tolls View Post
    For a relational database there's only one place for the extra column in a 1..n relationship.
    For an n..n there's a mapping table.
    Thanks Tolls. Your post seems to be perfect and to the point. Pls tell if I am correct or wrong

    1..1 - extra column on first side of relation
    1..n - extra column on n side
    n..1 - extra column on n side
    n..n - none(extra table is used)

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Hibernate Mappings

    Yeah I only spent 20 minutes of my boss' time typing up my reply.

    You're the type that ignores something when it becomes too much effort to read, right? I'd like to know, because then the next time I won't bother.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Hibernate Mappings

    Hi,

    you understood that a relation can be seen from two sides so a 1:n relationship is the same as a n:1 relationship (In mathematics we say it is commutative)?
    So with that knowledge your asumption is not fully correct.

    Technically you can store the reference to a single end in a field. But that is not even a fixed rule. You could have a mapping table with some constraints (So a 1:1 mapping could have unique constraints on both columns that store the id of referenced item).

    @gimbal2: Seems that I was wrong when I had the feeling that he was reading about this topic already. That is a shame. In my eyes this is some basic stuff that really needs deeper research and a full understanding. A developer who does not know such basics simply has to fail when developing software in this area ... (But I am to old already to get upset about these things. But each day I pray that I never have to take over responsiility of code build by such developers :) )

    Konrad

Similar Threads

  1. Association Mappings
    By suhaas_mohandos in forum Hibernate
    Replies: 1
    Last Post: 11-03-2013, 07:58 AM
  2. Should I be using hibernate?
    By sephiroth2212 in forum JDBC
    Replies: 4
    Last Post: 10-11-2013, 08:57 AM
  3. Hibernate
    By Freaky123 in forum New To Java
    Replies: 16
    Last Post: 04-19-2011, 05:58 AM
  4. new to hibernate
    By ajhansiprasad in forum Advanced Java
    Replies: 2
    Last Post: 11-06-2008, 05:20 AM
  5. Replies: 2
    Last Post: 06-30-2007, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •