I have a customer that has contacts. The relationship is below:
CustomerCode:
id
name
... other fields
ContactCode:
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?