facebook

Insert of primary/foreign keys/composite id’s

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #218538 Reply

    RUFFIE
    Member

    I don’t know if this should be here or in Database forum, so my excuses for that in advance. I’ll try to be as clear as possible.

    Mapping documents:
    Component.hbm.xml:

    <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:

    <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>

    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 and still building on the hibernate tutorial on your site.

    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.

    First i got Component.java:

    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:

    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 */ 
    
    } 

    Now SubComponentKey.java (the composite class):

    
    /*
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     *
     * Created Wed Oct 27 11:06:00 GMT-03:00 2004 by MyEclipse Hibernate Tool.
     */
    package com.frontier.hibernate;
    
    import java.io.Serializable;
    
    /**
     * A class representing a composite primary key id for the subcomponent
     * table.  This object should only be instantiated for use with instances 
     * of the Subcomponent class.
     */
    public class SubcomponentKey
        implements Serializable
    {
        /** The cached hash code value for this instance.  Settting to 0 triggers re-calculation. */
        private volatile int hashValue = 0;
    
        /** The value of the ID_COMPONENT component of this composite id. */
        private Component component;
    
        /** The value of the ID_SUBCOMPONENT component of this composite id. */
        private java.lang.Integer idSubcomponent;
    
        /**
         * Simple constructor of SubcomponentKey instances.
         */
        public SubcomponentKey()
        {
        }
    
        /**
         * Returns the value of the component property.
         * @return Component
         */
        public Component getComponent()
        {
            return component;
        }
    
        /**
         * Sets the value of the component property.
         * @param component
         */
        public void setComponent(Component component)
        {
            hashValue = 0;
            this.component = component;
        }
    
        /**
         * Returns the value of the idSubcomponent property.
         * @return java.lang.Integer
         */
        public java.lang.Integer getIdSubcomponent()
        {
            return idSubcomponent;
        }
    
        /**
         * Sets the value of the idSubcomponent property.
         * @param idSubcomponent
         */
        public void setIdSubcomponent(java.lang.Integer idSubcomponent)
        {
            hashValue = 0;
            this.idSubcomponent = idSubcomponent;
        }
    
        /**
         * Implementation of the equals comparison on the basis of equality of the id components.
         * @param rhs
         * @return boolean
         */
        public boolean equals(Object rhs)
        {
            if (rhs == null)
                return false;
            if (! (rhs instanceof SubcomponentKey))
                return false;
            SubcomponentKey that = (SubcomponentKey) rhs;
            if (this.getComponent() != null && that.getComponent() != null)
            {
                if (! this.getComponent().equals(that.getComponent()))
                {
                    return false;
                }
            }
            if (this.getIdSubcomponent() != null && that.getIdSubcomponent() != null)
            {
                if (! this.getIdSubcomponent().equals(that.getIdSubcomponent()))
                {
                    return false;
                }
            }
            return true;
        }
    
        /**
         * Implementation of the hashCode method conforming to the Bloch pattern with
         * the exception of array properties (these are very unlikely primary key types).
         * @return int
         */
        public int hashCode()
        {
            if (this.hashValue == 0)
            {
                int result = 17;
                int componentValue = this.getComponent() == null ? 0 : this.getComponent().hashCode();
                result = result * 37 + componentValue;
                int idSubcomponentValue = this.getIdSubcomponent() == null ? 0 : this.getIdSubcomponent().hashCode();
                result = result * 37 + idSubcomponentValue;
                this.hashValue = result;
            }
            return this.hashValue;
        }
    }
    

    This is my interface class ComponentService.java, I’m just gonna put the Add (insert) method. It isn’t really correct like this. It’s something I just tried, without the subcomponent handlings (only component thus) it works:

    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: 
    
    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 declare 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.

    For example. I insert a component with code 01.
    This component has a subcomponent which i give 01A. By clicking the checkbox indicating that this is a subcomponent, it should insert everything in the component table like this was a component, BUT insert the calc_operator, the foreign key of id_component and set the id_subcomponent (auto increment) in the subcomponent table.
    It’s like parent-child, class-subclass, you get the idea 🙂

    Any idea how that can be done in a simple way?

    #218549 Reply

    Riyad Kalla
    Member

    Is this a software development question or a “MyEclipse” question?

    #218600 Reply

    RUFFIE
    Member

    @support-rkalla wrote:

    Is this a software development question or a “MyEclipse” question?

    Uhm..software development…that’s why i thought i should post it here.. 😉

    #218603 Reply

    Riyad Kalla
    Member

    Ohh ok, I’ll move it (Off Topic -> Software dev). This is a bit confusing, but we try and separate questions related to MyEclipse and other questions.

    #254923 Reply

    dervre
    Member

    This message has not been recovered.

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Insert of primary/foreign keys/composite id’s

You must be logged in to post in the forum log in