Announcement Announcement Module
Collapse
No announcement yet.
Simple (I Hope) Hibernate Mapping question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple (I Hope) Hibernate Mapping question

    I'm just getting a little confused with the documentation Ive read about joining tables. I was hoping to get advice since I was reading about the performance problems that may arise if I go about this the wrong way. Basically I have two tables joined by some id.. So in a very simplified version,

    Table_1 has id, name
    Table_2 has id, permission

    Table_2 will have several results for one id though, and what I want in the end is one object with string name, list permissions from the join. What should I use in my mapping to enforce this relationship?

  • #2
    Re: Simple (I Hope) Hibernate Mapping question

    If you simply want to map a collection, you should read Hibernate reference documentation about Collection Mapping(chapter 6).

    You could try something like this(for example):

    <hibernate-mapping>
    <class name ="Class1" table="Table_1">
    <id name="id" column="ID">
    ...
    </id>

    <property name="name" column="NAME"/>

    <set(or bag) name="permissions">
    <key column="FOREIGN_KEY"/>
    <one-to-many class="Class2"/>
    </set(or bag)>
    </class>

    </hibernate-mapping>

    You did not mention a foreign key from table2 to table1...
    If you need a bidirectional mapping, you should also use <many-to-one> in Class2's mapping file and use the inverse flag on the bag...

    Comment


    • #3
      Hi,
      Meaning that you need to load the Object with the filled permission list?
      say you have the id as 1 and name as "sample1" in the table1. Also you have several records relevent to id 1 in the table2, like
      1, "permission1"
      1, "permission2"
      1, "permission3"
      and so on..
      is that the thing you are talking about?
      And finally you need to load the object with relevent permission list.
      Pls comment on this.
      /Viraj

      Comment


      • #4
        viraj, that is exactly what I am talking about. there will always be one entry in table 1, but there may be multiple permissions in table2, so I was hoping on storing them all in a list.

        Right now table 2 does not have any pk but I could add one and will probably have to anyway. The object you described would look like:

        Object
        id=1
        String name = "sample1"
        List permissions = ("permission1","permission2","permission3");

        Comment


        • #5
          It seems like a simple one-to-many association. I'd have a look at the examples in the reference manual.
          http://www.hibernate.org/hib_docs/v3.../#associations
          Last edited by karldmoore; Aug 30th, 2007, 06:26 AM.

          Comment


          • #6
            Hi,
            I need to add something more to johanr's and karldmoore's comments.

            It is assumed that you have a reference of type set (java.util.set) in the object class that you told.
            Also table1 and table2 like follows.

            table_1
            ID NAME

            table_1
            OBJ_ID PERMISSION


            meaning that:
            Your persistance class or model class looks like follows.

            Code:
            public class PersistanceObject implements Serializable{
            
            private String name;
            private String id;
            private Set permissions;
            
            //here you must have relevent getters and setters.
            ...
            ...
            .....
            .
            }
            you should have a mapping declaration like this.

            Code:
            <hibernate-mapping>
            <class name ="PersistanceObject" table="Table_1">
            <id name="id" column="ID"> 
                <generator class="native">
            </id>
            <property name="name" column="NAME"/>
            
            <set name="permissions" lazy="true" table="table_2">
             <key column="OBJ_ID"/>
             <element type="string" column="PERMISSION" not-null="true"/>
            </set>
             
            </class>
            
            </hibernate-mapping>
            Hope this will help you.

            /Viraj

            Comment

            Working...
            X