Announcement Announcement Module
Collapse
No announcement yet.
How to Update With Composite-id in Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Update With Composite-id in Hibernate

    I need a help to update a table ,which have primary key Pair.

    I have written a code as :
    Mapping for SapSystemandSite which has composite-id
    Code:
    <hibernate-mapping>
        <class name="com.oneaccess.hibernate.model.TSapsystemAndSite" table="t_sapsystem_and_site" catalog="oneaccess" lazy="false">
            <comment></comment>
            <composite-id name="id" class="com.oneaccess.hibernate.model.TSapsystemAndSiteId">
                <key-property name="site_id" type="int">
                   <column name="site_id"></column>
                </key-property>
                <key-property name="sap_system_id" type="int">
                   <column name="sap_system_id"></column>
                </key-property>
            </composite-id>
            
            <many-to-one name="TSapsystem" class="com.oneaccess.hibernate.model.TSapsystem" update="false" insert="false" fetch="select" lazy="false">
                <column name="sap_system_id" not-null="true">
                    <comment></comment>
                </column>
            </many-to-one>
            <many-to-one name="TSite" class="com.oneaccess.hibernate.model.TSite" update="false" insert="false" fetch="select" lazy="false">
                <column name="site_id" not-null="true">
                    <comment></comment>
                </column>
            </many-to-one>
           <set name="TSaproleBySapsystemAndSitesForSiteId" inverse="true" >
                <key>
                <column name="sap_system_id" not-null="true"></column>
                <column name="site_id" not-null="true"></column>
                </key>  
                   
                <one-to-many class="com.oneaccess.hibernate.model.TSaproleBySapsystemAndSite" />
            </set>
            
        </class>
    </hibernate-mapping>
    Persistence class for TSapsystem and Site:
    Code:
    public class TSapsystemAndSite implements java.io.Serializable {
    
    	private static final long serialVersionUID = 1L;
    	private TSapsystemAndSiteId id;
    
    	private TSapsystem TSapsystem;
    	
    	private TSite TSite;
    
    	private Set<TSaproleBySapsystemAndSite> TSaproleBySapsystemAndSitesForSiteId = new HashSet<TSaproleBySapsystemAndSite>(
    			0);
    
    	private Set<TSaproleBySapsystemAndSite> TSaproleBySapsystemAndSitesForSapSystemId = new HashSet<TSaproleBySapsystemAndSite>(
    			0);
    	public TSite getTSite() {
    		return TSite;
    	}
    
    	public void setTSite(TSite site) {
    		TSite = site;
    	}
    
    	public TSapsystemAndSiteId getId() {
    		return this.id;
    	}
    
    	public void setId(TSapsystemAndSiteId id) {
    		this.id = id;
    	}
    
    	public TSapsystem getTSapsystem() {
    		return this.TSapsystem;
    	}
    
    	public void setTSapsystem(TSapsystem TSapsystem) {
    		this.TSapsystem = TSapsystem;
    	}
    
    	public Set<TSaproleBySapsystemAndSite> getTSaproleBySapsystemAndSitesForSiteId() {
    		return this.TSaproleBySapsystemAndSitesForSiteId;
    	}
    
    	public void setTSaproleBySapsystemAndSitesForSiteId(
    			Set<TSaproleBySapsystemAndSite> TSaproleBySapsystemAndSitesForSiteId) {
    		this.TSaproleBySapsystemAndSitesForSiteId = TSaproleBySapsystemAndSitesForSiteId;
    	}
    
    	public Set<TSaproleBySapsystemAndSite> getTSaproleBySapsystemAndSitesForSapSystemId() {
    		return this.TSaproleBySapsystemAndSitesForSapSystemId;
    	}
    
    	public void setTSaproleBySapsystemAndSitesForSapSystemId(
    			Set<TSaproleBySapsystemAndSite> TSaproleBySapsystemAndSitesForSapSystemId) {
    		this.TSaproleBySapsystemAndSitesForSapSystemId = TSaproleBySapsystemAndSitesForSapSystemId;
    	}
    
    }
    persistence class for Composite-id class

    Code:
    public class TSapsystemAndSiteId implements java.io.Serializable {
    
    	private static final long serialVersionUID = 1L;
    	
    	private int site_id;
    
    	private int sap_system_id;
    
    	public int getSap_system_id() {
    		return sap_system_id;
    	}
    
    	public void setSap_system_id(int sap_system_id) {
    		this.sap_system_id = sap_system_id;
    	}
    
    	public int getSite_id() {
    		return site_id;
    	}
    
    	public void setSite_id(int site_id) {
    		this.site_id = site_id;
    	}
    
    	
    	public boolean equals(Object other) {
    		if ((this == other))
    			return true;
    		if ((other == null))
    			return false;
    		if (!(other instanceof TSapsystemAndSiteId))
    			return false;
    		TSapsystemAndSiteId castOther = (TSapsystemAndSiteId) other;
    
    		return (this.getSite_id()== castOther.getSite_id())
    				&& (this.getSap_system_id() == castOther.getSap_system_id());
    	}
    
    	public int hashCode() {
    		int result = 17;
    
    		result = 37 * result + this.getSite_id();
    		result = 37 * result + this.getSap_system_id();
    		return result;
    	}
    
    }
    I have inserted into a TSapsystemAndSite table using this mapping using getHibernateTemplate.save().
    But i dont know how to update a table .

    i need an idea is that ,how to update a table with this Type of mapping or
    with composite-ids .
    Thanks in Advance.
    Last edited by Dinesh_Kumar; Aug 21st, 2007, 05:38 AM. Reason: Some importance

  • #2
    Well how about getHibernateTemplate().update(your object)...

    Comment


    • #3
      How to Update With Composite-id in Hibernate

      Thank you very much for replying me .
      this is the Service layer I have written for Update
      Code:
      public void insertorupdate(SapSystemBean sapSystemBean,int sapSystemId) throws DataAccessException{
      		try{
      		TSapsystem sapSystem =new TSapsystem();
      		TSapsystemAndSiteId id=new TSapsystemAndSiteId();
      		TSapsystemAndSite sapSystemAndSite=new TSapsystemAndSite();
      		sapSystem.setSapSystemName(sapSystemBean.getSapSystemName());
      		sapSystem.setUserid(sapSystemBean.getUserId());
      		sapSystem.setPassword(sapSystemBean.getPassword());
      		sapSystem.setUrl("");
      		sapSystem.setDateModified(DateUtils.convertToTimeStamp());
      		if(sapSystemId==0){
      			sapSystem.setDateCreated(DateUtils.convertToTimeStamp());
      			sapSystemDao.insert(sapSystem);
      			List systemId=sapSystemDao.getLastRecord();
      			System.out.println("System Id : "+(Integer)systemId.get(0));
      			id.setSite_id(sapSystemBean.getSiteId());
      			id.setSap_system_id((Integer)systemId.get(0));
      			sapSystemAndSite.setId(id);
      			sapSystemAndSiteDao.insert(sapSystemAndSite);
      		}
      		else{
      			
      			sapSystem.setSapSystemId(sapSystemId);
      			sapSystem.setDateCreated(sapSystemBean.getDateCreated());
      			sapSystemDao.update(sapSystem);
      			sapSystemAndSite=sapSystemAndSiteDao.findBySapsystemId(sapSystemId);
      	
      			System.out.println("siteId : "+sapSystemBean.getSiteId()+"old : "+sapSystemAndSite.getId().getSite_id());
      			sapSystemAndSite.getId().setSap_system_id(sapSystemId);
      			sapSystemAndSite.getId().setSite_id(sapSystemBean.getSiteId());
      			//System.out.println("Name "+sapSystemAndSite.getTSapsystem().getSapSystemName());			
      			sapSystemAndSite.getTSite().setSiteId(sapSystemBean.getSiteId());
      			sapSystemAndSite.setTSapsystem(sapSystemDao.findBySapSystemId(sapSystemId));
      			sapSystemAndSiteDao.update(sapSystemAndSite);
      		}
      	}
      	catch(Exception e)
      	{
      		throw new DataAccessException("Exception in Insert::SapSystemService "+e);
      	}
      		
      	}
      this is the DAO Class I have written

      Code:
      package com.oneaccess.hibernate.dao;
      
      import java.util.List;
      
      import org.springframework.dao.support.DataAccessUtils;
      import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
      
      import com.oneaccess.common.exception.DataAccessException;
      import com.oneaccess.hibernate.model.TSapsystemAndSite;
      
      public class SapsystemAndSiteDao extends HibernateDaoSupport implements ISapsystemAndSiteDao{
      
      	public void insert(TSapsystemAndSite sapSystemAndSite) throws DataAccessException {
      		try{
      		getHibernateTemplate().save(sapSystemAndSite);
      		}catch(Exception e){
      			System.out.println("Error in Insert SapSytemAnd SiteDAO "+e);
      		}
      		
      	}
      
      	public void update(TSapsystemAndSite sapSystemAndSite) throws DataAccessException {
      		try{
      		getHibernateTemplate().update(sapSystemAndSite);
      		}catch(Exception e){
      			System.out.println(" Error in Update sapSystemAndSite "+e);
      		}
      		
      	}
      	public List findBySapSystemId(int sapSystemId)throws DataAccessException{
      		List siteDetails=getHibernateTemplate().find("select t.siteId,t.siteName from TSapsystemAndSite ss ,TSite t where ss.id.sap_system_id=? and ss.id.site_id= t.siteId ",new Integer(sapSystemId));
      		System.out.println("Name"+((Object[])siteDetails.get(0))[1]);
      		return siteDetails;
      	}
      
      	public TSapsystemAndSite findBySapsystemId(int sapSystemId) throws DataAccessException {
      		TSapsystemAndSite sapsystemAndSite=(TSapsystemAndSite)DataAccessUtils.uniqueResult(getHibernateTemplate().find("from TSapsystemAndSite ss where ss.id.sap_system_id=?",new Integer(sapSystemId)));
      		return sapsystemAndSite;
      	}
      
      }
      I need an idea about how to update using composite-ids .
      Is there any other example (Coding) for that .
      Thanks in Advance
      Last edited by Dinesh_Kumar; Aug 21st, 2007, 05:40 AM. Reason: some impo

      Comment


      • #4
        Please use [ code][/code ] tags when posting it makes your post readable.

        What is your problem? Isn't the update working? Does it behave differently than what you expected?

        Comment


        • #5
          How to Update With Composite-id in Hibernate

          Beyond my coding ,i need a sample example for updating a table,which have composite-id in a hibernate Mapping .

          Please help me for that

          thanks in Advance

          Comment


          • #6
            There is no difference in updating a record with a composite-id or a simple long id. Hibernate will handle this for you. Simply call the update method on the HibernateTemplate of Session is enough.

            Comment


            • #7
              How to Update With Composite-id in Hibernate

              Please tell me what is wrong in my coding ,which was posted .
              In Service I have tried to change the SiteId in TsapsystemAndSite .But i m using the existing Sapsystemid ,which was in table .
              I need to change the siteId in T_sapsystem_and_site table.

              siteId and SapSystemId are the compositeId in sapsystemAndSite's hibernate Mapping.

              Thanks in Advance.

              Comment


              • #8
                You need first to retrieve the instance you want to update from the database, change the properties and call the update method. Creating a new instance and add/change the properties on that isn't going to update the underlying database record.

                Comment


                • #9
                  I have tried that but it is not updated

                  The code will be

                  In Service Layer

                  Code:
                  TSapsystemAndSite sapSystemAndSite=sapSystemAndSiteDao.findBySapsystemId(sapSystemId);
                  			sapSystemAndSite.getId().setSite_id(sapSystemBean.getSiteId());
                  sapSystemAndSiteDao.update(sapSystemAndSite);
                  I have retrieved the instance with respect to SapsystemId.
                  on that Instance i wolud like to change the siteId .
                  (Note:Here sapsystemId and Siteid are Composite-keys in SapsystemandSite table(primary key pair))
                  and update the record .
                  But it was not updated .

                  Comment


                  • #10
                    You are actually updating/changing the primary-key. Does that mean it is an update or is it a new record (more of a filosofical question I guess).

                    In your DAO remove the try/catch if the error doesn't get propagated to a higher level the transaction is never going to rollback.

                    I don't quite understand the code in your service layer make sure that it goes into the correct if-then part.

                    Comment

                    Working...
                    X