Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Is this correct?

  1. #11
    Join Date
    Jul 2007
    Location
    Italy
    Posts
    61

    Default

    Quote Originally Posted by karldmoore View Post
    Any chance you can explain more about what you don't understand with this stuff. I don't really follow what you struggling with. As for your example, I think that declarative transactions would be a better idea.
    Well....let's image i have this kind of hibernate configuration:

    molti.hbm.xml
    Code:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    <class name="Apparecchiatura" table="APPARECCHIO">
    	<id name="id" type="int" column="APPA_ID" unsaved-value="0">
    		<generator class="native"/>
    	</id>
    	<many-to-one name="organo" column="ORGANO_ID" not-null="true" class="it.eng.Organo" cascade="delete"/>
    	<property name="descrizione" column="DESCRIZIONE" type="string"/>
    </class>
    </hibernate-mapping>

    Apparecchiatura.java

    Code:
    import java.io.Serializable;
    
    public class Apparecchiatura extends GenericEntity{
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -6700481256808933166L;
    	
    	private int id;
    	private String descrizione;
    	private Organo organo;
    	public String getDescrizione() {
    		return descrizione;
    	}
    	public void setDescrizione(String descrizione) {
    		this.descrizione = descrizione;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public Organo getUnoAmolti() {
    		return organo;
    	}
    	public void setUnoAmolti(Organo organo) {
    		this.organo = organo;
    	}
    	public Serializable getKey() {
    		
    		return new Integer(getId());
    	}
    	public Organo getOrgano() {
    		return organo;
    	}
    	public void setOrgano(Organo organo) {
    		this.organo = organo;
    	}
    }

    uno.hbm.xml

    Code:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    <class name="Organo" table="ORGANO">
    	<id name="id" type="int" column="ID" unsaved-value="0">
    		<generator class="native"/>
    	</id>
    	<property name="descrizione" column="DESCRIZIONE" type="string"/>
        <set name="apps" cascade="all" lazy="true">
            <key column="APPA_ID"/>
            <one-to-many class="it.eng.Apparecchiatura"/>
        </set> 
    </class>
    </hibernate-mapping>
    Organo.java

    Code:
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    
    public class Organo extends GenericEntity {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -1692194167515470470L;
    	
    	private int id;
    	private String descrizione;
    	private Set apps = new HashSet();
    	public String getDescrizione() {
    		return descrizione;
    	}
    	public void setDescrizione(String descrizione) {
    		this.descrizione = descrizione;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public Serializable getKey() {
    			
    			return new Integer(getId());
    		}
    	public Set getApps() {
    		return apps;
    	}
    	public void setApps(Set apps) {
    		this.apps = apps;
    	}
    	public void addApparecchiatura(Apparecchiatura ap){
    		ap.setOrgano(this);
    		apps.add(ap);
    	}
    }

    Now if i try to search an Organo i can use getHibernateTemplate for doing it; if i need to have all the Apparecchiatura associated to the Organo i have to use the doInHibernate method so i can load Organo and all the Apparecchiatura associated; and untill here it's still all OK.
    Now let's image i have to update some Apparecchiatura associated to Organo; i need to do this always in doinHibernate method...but let's image i have two or more transaction i have to do in the current open Hibernate session and let's image i need to be sure about the full commit transaction....how can i do?
    Can i use the way i showed before or must i use any other way?

  2. #12
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote Originally Posted by craig1980 View Post
    Now if i try to search an Organo i can use getHibernateTemplate for doing it; if i need to have all the Apparecchiatura associated to the Organo i have to use the doInHibernate method so i can load Organo and all the Apparecchiatura associated; and untill here it's still all OK.
    You can do this via a join.
    http://www.hibernate.org/hib_docs/v3...queryhql-joins

    Quote Originally Posted by craig1980 View Post
    Now let's image i have to update some Apparecchiatura associated to Organo; i need to do this always in doinHibernate method...but let's image i have two or more transaction i have to do in the current open Hibernate session and let's image i need to be sure about the full commit transaction....how can i do? Can i use the way i showed before or must i use any other way?
    I'm sorry I still don't understand what the problem is.
    Last edited by karldmoore; Aug 27th, 2007 at 03:44 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #13
    Join Date
    Jul 2007
    Location
    Italy
    Posts
    61

    Default

    Hi....so sorry for disturbing you...i try to be more clear.
    Always in the scenario i described before let's imagine i have a third table.
    Imagine i have to do some operation on the tables described before (i mean some update), then some other control and then, after these controls, i must do an insert in the third table; all this in one transaction and i must be sure that if all the DB operations end well i must commit the transaction otherwise i must rollback the full transaction....this is my scenario.
    Now by having the mapping file with lazy=true i must recover all details of Organo (that is Organo with all the Apparecchaitura related) in the current open session...so i use the doInHibernate method....more i have to handle the full transaction. I hope i have been more clear
    Try to pardon me
    Thanx,
    Angelo.

  4. #14
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'm still not clear on what you are trying to do. If you want all the operations to be contained within one transaction, then simply ensure you perform them within the scope of that one transaction.
    http://www.springframework.org/docs/...ansaction.html
    Last edited by karldmoore; Aug 27th, 2007 at 03:44 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  5. #15
    Join Date
    Jul 2007
    Location
    Italy
    Posts
    61

    Default

    Hi.

    Thanx for your patience.
    I have been able in studing more the Spring hibernate support.
    I have understood this this:

    the doInHibernate method is not transactional...i mean if there is a transaction it supports the transaction otherwise it doesn't. What is transactional is the doIntransaction method of the transactionTemplate.
    Indeed i have done this test:
    i have the three tables described in the previous reply: i have to do some quries and some check on the Organo and Apparecchiatura table, then, after all the controls, i have to add in a third table; i have done this operation:
    • Load organo

    • Load all the organo's apparecchiatura

    • Do some update on apparecchiatura

    • Do an insert in the third table


    I caused an error in the insert in the third table (i wanted this error).What i expected was that since there is this error also the update on apparecchiatura was rollback.
    I tried this in the doInHibernate method and as there was the error the update on apparecchiatura was not rollbacked.
    I tried the same code in the doInTransaction method and, as i wanted, the update was rollbacked.

    I'm sorry for distubing all you...and sorry for my bad english....
    What i wanted to be sure was about the full transaction. Thanks again.
    Angelo

  6. #16
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Right ok. If you are using the HibernateTransactionManager then the HibernateCallback will give you the same Session within that transaction. You should be able to call into various HibernateCallback instances and find you have the same Session everytime. Personally, if you are using transactions I would go for declarative support. However if you do want to use transactions programmatically, TransactionTemplate is the way to go.
    Last edited by karldmoore; Aug 27th, 2007 at 03:44 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #17
    Join Date
    Jul 2007
    Location
    Italy
    Posts
    61

    Default

    Quote Originally Posted by karldmoore View Post
    Right ok. If you are using the HibernateTransactionManager then the HibernateCallback will give you the same Session within that transaction. You should be able to call into various HibernateCallback instances and find you have the same Session everytime. Personally, if you are using transactions I would go for declarative support. However if you do want to use transactions programmatically, TransactionTemplate is the way to go.
    Ok.
    Thank you for the support.
    I have ideas little bit more clear
    Sorry for disturbing.

  8. #18
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Not a problem, glad to help. If you have anymore questons, post back and I'll see what I can do.
    Last edited by karldmoore; Aug 27th, 2007 at 03:43 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •