Announcement Announcement Module
Collapse
No announcement yet.
Using hibernate 'List' Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using hibernate 'List'

    hi guys
    I am using hibernate with SQL server 2005 database.
    I have three tables. The tables are

    Project
    PROJECT_ID (PK)
    Name


    Users
    USER_ID (PK)
    Fname
    Lname


    Project_Users
    PROJECT_ID (PK)
    USER_ID (PK)
    Role

    The requirments are straight forward:
    • one project can have multiple users. One user can work on multiple projects.
    • When user is searched by UserId, all the projects for that user should be retrieved.
    • When Project is searched by projectId, user information need not be fetched. Only projectId and project name are shown.

    I am trying to use 'List' to represent the above relationships but want to make sure i am doing right. Since its a bidirectional relationship, i have inverse="true"

    My Project.hbm.xml
    Code:
       <class name="hibernate.entity.Project" table="PROJECT" catalog="eProject">
            <id name="projectId" type="int">
                <column name="PROJECT_ID"/>
                <generator class="assigned" />
            </id>
            <property name="name" type="string">
                <column name="NAME" length="50" not-null="true" unique="true"  />
            </property>
            
             <list name="projectUserses" inverse="true">         
          	<key column="PROJECT_ID" not-null="true"/> 
        	<list-index column="USER_ID"/>          
     	<one-to-many class="hibernate.entity.ProjectUsers"/>
              </list>
        </class>
    User.hbm.xml
    Code:
    <class name="hibernate.entity.Users" table="USERS" catalog="eProject">
            <id name="userId" type="string">
                <column name="USER_ID" length="20" />
                <generator class="assigned" />
            </id>
            <property name="firstName" type="string">
                <column name="FIRST_NAME" length="50" />
            </property>
            <property name="lastName" type="string">
                <column name="LAST_NAME" length="50" />
            </property>
            
             <list name="projectUserses" inverse="true">         
          	<key column="USER_ID" not-null="true"/> 
        	<list-index column="PROJECT_ID"/>          
         	<one-to-many class="hibernate.entity.ProjectUsers"/>
             </list>
      </class>
    projectUser.hbm.xml

    Code:
     
    <class name="hibernate.entity.ProjectUsers" table="PROJECT_USERS" catalog="eProject">
            <composite-id name="id" class="hibernate.entity.ProjectUsersId">
                <key-property name="projectId" type="int">
                    <column name="PROJECT_ID" />
                </key-property>
                <key-property name="userId" type="string">
                    <column name="USER_ID" length="20" />
                </key-property>
            </composite-id>
            <many-to-one name="users" class="hibernate.entity.Users" lazy="false" update="false" insert="false" fetch="select">
                <column name="USER_ID" length="20" not-null="true" />
            </many-to-one>
            <many-to-one name="project" class="hibernate.entity.Project" update="false" insert="false" fetch="select">
                <column name="PROJECT_ID" not-null="true" />
            </many-to-one>
            <property name="userRole" type="string">
                <column name="USER_ROLE" length="50" />
            </property>
        </class>

    Am i doing it correctly? Any help will be greatly appreciated.

    thanks
    Last edited by jaggernat; Apr 19th, 2011, 01:08 PM.
Working...
X