-->
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: Problem with refresh ManyToOne relationship in session
PostPosted: Sat Mar 06, 2010 1:38 pm 
Newbie

Joined: Sat Mar 06, 2010 1:19 pm
Posts: 8
I have a customer that has contacts. The relationship is below:

Customer

Code:
id
name
... other fields


Contact

Code:
id
name
... other fields
@ManyToOne
@JoinColumn(name="customer")
customer


When I save a contact to this customer

Code:
this.session = HibernateUtil.pegarSessao(id);   
this.tx = this.session.beginTransaction();   
               
this.session.merge(customerContact);   
this.session.flush();   
tx.commit(); 


I make a search to find the collection with new customer (in the same session):

Code:
this.session = HibernateUtil.pegarSessao(id);   
this.tx = this.session.beginTransaction();   
 
return this.session   
            .createQuery("from Contact WHERE customer.id = ?")   
            .setInteger(0, customer.getId())   
            .list(); 


This collection returned without the new contact, but in the database this was saved.

When I open a new session before to load the collection. It list the registers correctly:

Code:
this.session = HibernateUtil.pegarSessao(id);   
this.tx = this.session.beginTransaction();   
 
System.out.println(this.session   
            .createQuery("from Contact WHERE customer.id = ?")   
            .setInteger(0, customer.getId())   
            .list().size());   
               
this.session = HibernateUtil.getSessionFactory(id).openSession();   
               
System.out.println(this.session   
            .createQuery("from Contact WHERE customer.id = ?")   
            .setInteger(0, customer.getId())   
            .list().size());   


The return is: 0 for the first sysout, and 1 for the second.

P.S: I tried to use Cascade.ALL, and Cascade.MERGE in the relationship but it doesn't works
P.S: I maintain the session in current session context thread ( <property name="current_session_context_class">thread</property> ).Is there a problem?


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.