-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 
Author Message
 Post subject: composite key and many to one mapping
PostPosted: Tue Jan 23, 2007 7:43 am 
Newbie

Joined: Tue Dec 26, 2006 2:57 am
Posts: 11
i have the following table.

prf_vendor_details:

PRF_id PK, FK
vendor_name PK, FK

the above is a composite primary key, the table is referring to two different tables and having many-to-one relation with PRF_details and vendor_master.

how to map this in hibernate. plz reply me asap.

thank you
vinay


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 23, 2007 8:13 am 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
I'm not sure I understand the full question, but here is a quick start:

If you have 3 classes representing your tables:
- PrfVendorDetails
- PRF_details
- Vendor_master

You will need to have an extra class to manage the composite key: PrfVendorDetailsId which includes the 2 properties you mention. Then have PrfVendorDetails include a property called id of type PrfVendorDetailsId.

Then your PrfVendorDetails.hbm.xml mapping would look like this:

Code:
<class name="PrfVendorDetails" table="prf_vendor_details">
    <composite-id name="id" class="PrfVendorDetailsId">
            <key-property name="PRF_id">
                <column name="PRF_id" />
            </key-property>
            <key-property name="vendor_name">
                <column name="vendor_name"/>
            </key-property>
        </composite-id>

    ... other property mappings...

    <many-to-one name="details" column="PRF_details_id" class="PRF_details" not-null="true" />

    <many-to-one name="master" column="vendor_master_id" class="Vendor_master" not-null="true" />

</class>



If this isn't what you are looking for, please give more details.


Top
 Profile  
 
 Post subject: thanks
PostPosted: Tue Jan 23, 2007 9:29 am 
Newbie

Joined: Tue Dec 26, 2006 2:57 am
Posts: 11
thanks mariec

plz tell me How can I produce such mapping from java file (with XDoclet2)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 23, 2007 11:34 am 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
To be honest, I don't use XDoclet2 at all... someone else will need to explain this part to you.


Top
 Profile  
 
 Post subject: my query in detail...
PostPosted: Thu Feb 01, 2007 12:35 pm 
Newbie

Joined: Tue Dec 26, 2006 2:57 am
Posts: 11
let me be more clear....

I've three tables...

PRF_DETAILS_TABLE:


PD_PRF_ID PK

PRF_VENDOR_DETAILS_TABLE:

PVD_PRF_ID PK, FK(refers PRF_DETAILS_TABLE(PD_PRF_ID))
PVD_VENDOR_ID PK, FK(refers VENDOR_MASTER(VM_VENDOR_ID))

(composite primary key)

VENDOR_MASTER:

VM_VENDOR_ID PK


How can i map these three tables in Hibernate3.0.

i would like to give parent/child relation to PRF_DETAILS_TABLE(PARENT) and PRF_VENDOR_DETAILS_TABLE(child).

and one-to-many relation between..
VENDOR_ID -[one-to-many]-->PRF_VENDOR_DETAILS_TABLE.

how can do these mappings? plz help me out in this regard.

mariec can you help me out in this regard....

reply me asap...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 3:08 am 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
You can start with these hibernate mappings:

PRF_DETAILS_TABLE.hbm.xml
Code:
<class name="PRF_Details" table="PRF_DETAILS_TABLE">
    <id name="PD_Prf_Id" column="PD_PRF_ID">
        <generator class="identity" />
    </id>

    ... other column/property mappings...

    <set name="PRF_vendor_details" inverse="true" lazy="true">
        <key>   <!-- a collection inherits the composite key type -->
            <column name="PVD_PRF_ID" />
            <column name="PVD_VENDOR_ID" />
        </key>
        <one-to-many class="PRF_Vendor_Details" />
    </set>
</class>


VENDOR_MASTER.hbm.xml
Code:
<class name="Vendor_Master" table="VENDOR_MASTER">
    <id name="VM_Vendor_Id" column="VM_VENDOR_ID">
        <generator class="identity" />
    </id>

    ... other column/property mappings...

    <set name="PRF_vendor_details" inverse="true" lazy="true">
        <key>   <!-- a collection inherits the composite key type -->
            <column name="PVD_PRF_ID" />
            <column name="PVD_VENDOR_ID" />
        </key>
        <one-to-many class="PRF_Vendor_Details" />
    </set>
</class>


PRF_VENDOR_DETAILS_TABLE
Code:
<class name="PRF_Vendor_Details" table="PRF_VENDOR_DETAILS_TABLE">
    <composite-id name="id" class="PrfVendorDetailsId">
        <key-property name="PVD_PRF_id">
            <column name="PVD_PRF_ID" />
        </key-property>
        <key-property name="PVD_Vendor_Id">
            <column name="PVD_VENDOR_ID"/>
        </key-property>
    </composite-id>

    ... other column/property mappings...

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 3:38 am 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
Here are the classes you would need:

For the composite Id in child

PrfVendorDetailsId
Code:
public class PrfVendorDetailsId{
   
    private Integer PVD_PRF_id;
    private Integer PVD_Vendor_Id;

    //Add getters and setters
}


Child:
PRF_Vendor_Details
Code:
public class PRF_Vendor_Details{
   
    private PrfVendorDetailsId id;

    //Add getters and setters
}


Parents:
PRF_Details
Code:
public class PRF_Details{
   
    private Integer PD_Prf_Id;

    private Set<PRF_Vendor_Details> PRF_vendor_details = new HashSet<PRF_Vendor_Details>();

    //Add getters and setters
}


Vendor_Master
Code:
public class Vendor_Master{
   
    private Integer VM_Vendor_Id;

    private Set<PRF_Vendor_Details> PRF_vendor_details = new HashSet<PRF_Vendor_Details>();

    //Add getters and setters
}


Top
 Profile  
 
 Post subject: small change in my requirement..
PostPosted: Fri Feb 02, 2007 4:36 am 
Newbie

Joined: Tue Dec 26, 2006 2:57 am
Posts: 11
thanks mariec,

i've parent child relationship only for..

PRF_DETAILS_TABLE(PARENT).... PRF_VENDOR_DETAILS_TABLE(Child).

and simple foreign key relationship between..

VENDOR_MASTER and PRF_VENDOR_DETAILS_TABLE (not parent-child realtionship)

plz don't mind i'm very new to Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 4:56 am 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
Then you can simply remove the Set from VENDOR_MASTER.

Is there still a composite key for PRF_VENDOR_DETAILS_TABLE ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 5:25 am 
Newbie

Joined: Tue Dec 26, 2006 2:57 am
Posts: 11
yes i've composite key in PRF_VENDOR_DETAILS_TABLE

which will have a foreign key refers to VENDOR_MASTER.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 5:28 am 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
In my first post, you will find how to map the foreign key relationship.

Try playing around with the current mappings and see if you can do simple queries.

If you have any problems, I'll try to help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 7:06 am 
Newbie

Joined: Tue Dec 26, 2006 2:57 am
Posts: 11
PRF_DETAILS
PD_PRF_ID PK

PRF_VENDOR_DETAILS
PVD_PRF_ID PK, FK(referes PRF_DETAILS which will have parent/child relation)
PVD_VENDOR_ID PK, FK( just refers VENDOR_MASTER no parent/child relation)

(composite primary key)

VENDOR_MASTER
VM_VENDOR_ID PK



the code i've written :

PRFVendorDetails.hbm.xml

Code:
<hibernate-mapping>
   <class name="com.i3l.ppsapplication.model.PRFVendorDetails"
      table="PRF_VENDOR_DETAILS" dynamic-update="true" select-before-update="true"
      dynamic-insert="false">


      <composite-id name="prfVendorDetailsPK" class="com.i3l.ppsapplication.model.PRFVendorDetailsPK">
              <key-property name="PRFId" type="int">
                  <column name="PVD_PRF_ID" />
              </key-property>
              
              <key-property name="vendorId" type="int">
                  <column name="PVD_VENDOR_ID" />                 
              </key-property>
       </composite-id>
      
       <many-to-one name="vendor" column="PVD_VENDOR_ID"
          class="com.i3l.ppsapplication.model.VendorMaster" not-null="true" />

...........  other mappings.

</class>
</hibernate-mapping>



PRFDetails.hbm.xml

Code:
<hibernate-mapping>
   <class name="com.i3l.ppsapplication.model.PRFDetails"
      table="PRF_DETAILS" dynamic-update="false" dynamic-insert="false"
      select-before-update = "true">

      <id name="PRFId" column="PD_PRF_ID" type="int">
         <generator class="native"></generator>
      </id>
   
    ...... other mappings

      <set name="vendors" inverse="true" lazy="true">
           <key>   <!-- a collection inherits the composite key type -->
               <column name="PVD_PRF_ID" />
               <column name="PVD_VENDOR_ID" />
           </key>
           <one-to-many class="com.i3l.ppsapplication.model.PRFVendorDetails" />
       </set>

</class>
</hibernate-mapping>



VendorMaster.hbm.xml

Code:
<hibernate-mapping>
   <class name="com.i3l.ppsapplication.model.VendorMaster"
      table="VENDOR_MASTER" dynamic-update="false" dynamic-insert="false">

      <id name="vendorId" column="VM_VENDOR_ID"
         type="int" unsaved-value="null">
         <generator class="native"></generator>
      </id>
  ....... other mappings

</class>
</hibernate-mapping>



with this code im getting following error when i'm trying to generate java code through (ant task hbm2java of hibernate tool):

Code:

hibernatetool:
[hibernatetool] Executing Hibernate Tool with a Standard Configuration
[hibernatetool] 1. task: hbm2java (Generates a set of .java files)
[hibernatetool] 16:08:44,657  WARN HbmBinder:422 - Could not perform validation checks for component as the class com.i3l.ppsapplication.model.PRFStatusPK was not found
[hibernatetool] 16:08:44,813  WARN HbmBinder:422 - Could not perform validation checks for component as the class com.i3l.ppsapplication.model.PRFVendorDetailsPK was not found
[hibernatetool] An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] org.hibernate.MappingException: Foreign key (FKAD541546DB6DB123:PRF_VENDOR_DETAILS [PVD_PRF_ID,PVD_VENDOR_ID])) must have same number of columns as the referenced primary key (PRF_DETAILS [PD_PRF_ID])

BUILD FAILED
java.lang.NoClassDefFoundError: org/hibernate/MappingNotFoundException
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1225)
   at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
   at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
   at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
   at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
   at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/MappingNotFoundException
   at org.hibernate.tool.ant.HibernateToolTask.getProbableSolutionOrCause(HibernateToolTask.java:230)
   at org.hibernate.tool.ant.HibernateToolTask.reportException(HibernateToolTask.java:215)
   at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
   at org.apache.tools.ant.Task.perform(Task.java:364)
   at org.apache.tools.ant.Target.execute(Target.java:341)
   at org.apache.tools.ant.Target.performTasks(Target.java:369)
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
   ... 6 more
--- Nested Exception ---
java.lang.NoClassDefFoundError: org/hibernate/MappingNotFoundException
   at org.hibernate.tool.ant.HibernateToolTask.getProbableSolutionOrCause(HibernateToolTask.java:230)
   at org.hibernate.tool.ant.HibernateToolTask.reportException(HibernateToolTask.java:215)
   at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
   at org.apache.tools.ant.Task.perform(Task.java:364)
   at org.apache.tools.ant.Target.execute(Target.java:341)
   at org.apache.tools.ant.Target.performTasks(Target.java:369)
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
   at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
   at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
   at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
   at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
   at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)

Total time: 4 seconds


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 10:34 am 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
I'm not completely sure what is going on here... and I can't test it myself for the moment.

Could you try adding the following to your PRFVendorDetails.hbm.xml mapping:

<many-to-one name="details" column="PD_PRF_ID"
class="com.i3l.ppsapplication.model.PRFDetails" not-null="true" />


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 03, 2007 1:51 am 
Newbie

Joined: Tue Dec 26, 2006 2:57 am
Posts: 11
thanks mariec,

finally i've solved mapping problem..

but still i'm facing one problem with composite-id in PRFVendorDetails.hbm.xml

Code:
<class name="com.i3l.ppsapplication.model.PRFVendorDetails"
      table="PRF_VENDOR_DETAILS" dynamic-update="true" select-before-update="true"
      dynamic-insert="false" mutable="false">


      <composite-id name="id" class="com.i3l.ppsapplication.model.PRFVendorDetailsPK">
              <key-property name="PRFId" type="int" access="field">
                  <column name="PVD_PRF_ID" />
              </key-property>
              
              <key-property name="vendorId" type="int" access="field">
                  <column name="PVD_VENDOR_ID" />                 
              </key-property>
       </composite-id>

..... other mappings
</class>


my build.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<project name="example" basedir="." default="compile">



    <property name="src.dir" location="${basedir}/src" />
    <property name="build.dir" location="${basedir}/build" />
   <property name="build.class.dir" location="${build.dir}/classes" />
    <property name="lib.dir" location="${basedir}/WebContent/WEB-INF/lib" />

   
     <path  id="hibernate.schemaexport.classpath">
        <path   >
            <pathelement location="${lib.dir}/hibernate3.jar" />
           <pathelement location="mysql-connector-java-3.0.11-stable-bin.jar"/>
           <pathelement location="${lib.dir}/hibernate-tools.jar" />
            <fileset dir="${lib.dir}" >
                <include name="**/*.jar" />
            </fileset >
            <pathelement path="${build.class.dir}" />
        </path>
    </path>
<target name="schemaexport" depends="compile">

        <taskdef  name="schemaexport" 
            classpathref="hibernate.schemaexport.classpath"
            classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
             >
        </taskdef >

        <schemaexport config="${build.dir}/hibernate.cfg.xml"
           output="${build.dir}/exported-database-schema.sql">
            <fileset dir="${build.dir}">
                        <include name="**/*.hbm.xml"/>
            </fileset>
        </schemaexport>
    </target>

   
   <target name="hibernatetool" description="hbm2java">
      <echo>in target hibernatetool</echo>
   <taskdef name="hibernatetool"
           classname="org.hibernate.tool.ant.HibernateToolTask"
           classpathref="hibernate.schemaexport.classpath"/>
   <echo>after taskdef</echo>
       <hibernatetool destdir="${src.dir}">

           <configuration configurationfile="${build.dir}/hibernate.cfg.xml">
               <fileset dir="${src.dir}">
                   <include name="**/*.hbm.xml"/>
               </fileset>
           </configuration>

           <hbm2java />
       </hibernatetool>
   </target>
</project>


when i'm trying to generate java code and schema.. through hibernate tool ant task.. im getting following error.

Code:
[hibernatetool] Executing Hibernate Tool with a Standard Configuration
[hibernatetool] 1. task: hbm2java (Generates a set of .java files)
[hibernatetool] 21:30:56,415  WARN HbmBinder:422 - Could not perform validation checks for component as the class com.i3l.ppsapplication.model.PRFStatusPK was not found
[hibernatetool] 21:30:56,556  WARN HbmBinder:422 - Could not perform validation checks for component as the class com.i3l.ppsapplication.model.PRFVendorDetailsPK was not found

BUILD FAILED
java.lang.NoClassDefFoundError: freemarker/cache/TemplateLoader
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1225)
   at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
   at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
   at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
   at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
   at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
Caused by: java.lang.NoClassDefFoundError: freemarker/cache/TemplateLoader
   at org.hibernate.tool.hbm2x.AbstractExporter.start(AbstractExporter.java:92)
   at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:40)
   at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:183)
   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
   at org.apache.tools.ant.Task.perform(Task.java:364)
   at org.apache.tools.ant.Target.execute(Target.java:341)
   at org.apache.tools.ant.Target.performTasks(Target.java:369)
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
   ... 6 more
--- Nested Exception ---
java.lang.NoClassDefFoundError: freemarker/cache/TemplateLoader
   at org.hibernate.tool.hbm2x.AbstractExporter.start(AbstractExporter.java:92)
   at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:40)
   at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:183)
   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
   at org.apache.tools.ant.Task.perform(Task.java:364)
   at org.apache.tools.ant.Target.execute(Target.java:341)
   at org.apache.tools.ant.Target.performTasks(Target.java:369)
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
   at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
   at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
   at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
   at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
   at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)

Total time: 4 seconds


can you help me in this regard..


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 03, 2007 6:05 am 
Newbie

Joined: Tue Jan 16, 2007 8:58 am
Posts: 17
The warning says that com.i3l.ppsapplication.model.PRFVendorDetailsPK can't be found.

Did you create this class? This class is supposed to exist and represent the composite Id. It should simply have both properties

Code:
public class PRFVendorDetailsPK{
    private Integer PRFId;
    private Integer vendorId;

    //Add getters and setters
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.