-->
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.  [ 1 post ] 
Author Message
 Post subject: [Beginner] Help with insert PK/FK/Composite ID
PostPosted: Fri Oct 29, 2004 3:05 pm 
Newbie

Joined: Mon Oct 18, 2004 11:27 am
Posts: 4
Hibernate version:
2.1.6 (I think, I'm using latest MyEclipse)
Mapping documents:
Component.hbm.xml:
Code:
<hibernate-mapping package="com.frontier.hibernate">

    <class name="Component" table="component">
        <id name="idComponent" column="ID_COMPONENT" type="java.lang.Integer">
            <generator class="native"/>
        </id>

        <property name="cdComponent" column="CD_COMPONENT" type="java.lang.String" />
        <property name="omschrijving" column="OMSCHRIJVING" type="java.lang.String" />
        <property name="langeOmschrijving" column="LANGE_OMSCHRIJVING" type="java.lang.String" />
        <property name="indBedragwaarde" column="IND_BEDRAGWAARDE" type="java.lang.String" />
        <property name="indPrint" column="IND_PRINT" type="java.lang.String" />
        <property name="indSubcomponent" column="IND_SUBCOMPONENT" type="java.lang.String" />
        <property name="rowVersion" column="ROW_VERSION" type="java.lang.Integer" />
    </class>
   
</hibernate-mapping>


Subcomponent.hbm.xml:
Code:
<hibernate-mapping package="com.frontier.hibernate">

    <class name="Subcomponent" table="subcomponent">
        <composite-id name="id" class="SubcomponentKey">
            <key-many-to-one name="component" column="ID_COMPONENT" class="Component"/>
            <key-property name="idSubcomponent" column="ID_SUBCOMPONENT" type="java.lang.Integer"/>
        </composite-id>

        <property name="calcOperator" column="CALC_OPERATOR" type="java.lang.String" />
        <property name="rowVersion" column="ROW_VERSION" type="java.lang.Integer" />
    </class>
   
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
From my HibernateUtil.java or the Interface class?

Name and version of the database you are using:
mySQL 4.1.7 with InnoDB Tables

My question:
I'm rather new to struts and hibernate.
I have two tables: Component with PK id_component and Subcomponent which has a composite key that consists of foreign key id_component and it's own auto increment id_subcomponent.

I'm using myEclipse and started out with a interactive tutorial that's on their site and just "rewrote" their code to test this.
First i got Component.java:
Code:
package com.frontier.hibernate;

import java.io.Serializable;

/**
* A class that represents a row in the 'component' table.
* This class may be customized as it is never re-generated
* after being created.
*/
public class Component
    extends AbstractComponent
    implements Serializable
{
    /**
     * Simple constructor of Component instances.
     */
    public Component()
    {
    }

    /**
     * Constructor of Component instances given a simple primary key.
     * @param idComponent
     */
    public Component(java.lang.Integer idComponent)
    {
        super(idComponent);
    }

    /* Add customized code below */

}


Then Subcomponent.java:
Code:
package com.frontier.hibernate;

import java.io.Serializable;

/**
* A class that represents a row in the 'subcomponent' table.
* This class may be customized as it is never re-generated
* after being created.
*/
public class Subcomponent
    extends AbstractSubcomponent
    implements Serializable
{
    /**
     * Simple constructor of Subcomponent instances.
     */
    public Subcomponent()
    {
    }

    /**
     * Constructor of Subcomponent instances given a composite primary key.
     * @param id
     */
    public Subcomponent(SubcomponentKey id)
    {
        super(id);
    }

    /* Add customized code below */

}


This is my interface class ComponentService.java, I'm just gonna put the Add (insert) method. It isn't correct like this. It's something I just tried, without the subcomponent handlings (only component thus) it works:
Code:
public void addComponent(Component data, Subcomponent subdata)
   {
      Session session = null;
      
      try
      {
         session = HibernateUtil.getSession();
         HibernateUtil.beginTransaction();
         session.save(data);
         session.save(subdata);
         HibernateUtil.commitTransaction();
         session.flush();
      }
      catch (HibernateException e)
      {
         System.err.println("Hibernate Exception" + e.getMessage());
         throw new RuntimeException(e);
      }
            finally
      {
         if (session != null)
         {
            HibernateUtil.closeSession();

         }
      }

   }


And this is the execute from my action class:
Code:
public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response) {
      ComponentForm AddComponentForm = (ComponentForm) form;
      
      if (AddComponentForm.getOmschrijving() != null)
      {
         Component component = new Component();
         component.setCdComponent(AddComponentForm.getCdComponent());
         component.setOmschrijving(AddComponentForm.getOmschrijving());
         component.setLangeOmschrijving(AddComponentForm.getLangeOmschrijving());
         component.setIndBedragwaarde(AddComponentForm.getIndBedragwaarde());
         component.setIndPrint(AddComponentForm.getIndPrint());
         component.setIndSubcomponent(AddComponentForm.getIndSubcomponent());
         ComponentService.getInstance().addComponent(component);
         AddComponentForm.clear();
      }


I have a ComponentForm.java with the getters and setters and tried to declarate SubcomponetKey idSubcomponent; and put a setter for that, but I dunno..

I've read the hibernate docs about composite id's and components (chapter 7 i believe), but either I'm experiencing a blackout or i'm a real n00b.

I just want to fill in my form to add a component, and when it's a subcomponent insert the idSubcomponent (i got a checkbox in the form to indicate that) in the Subcomponent table and get the associated idComponent (FK) with it and store it also in the Subcomponent table.
How?


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

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.