Quote:
RamoAlumno rp = new RamoAlumno();
rp.setEstado(estado); //one propiety of RamoAlumno
rp.setSemestre(sem); //other
rp.setAgno(agno); //and other
rp.setAlumno(p); // this is for relation to Alumno
rp.setRamo(r); // and Ramo
sess.save(rp); // this work
p.getRamo_alumno().add(rp); // this work fine
r.getRamo_alumno().add(rp); // this don't work
sess.getTransaction().commit();
Well typically you'd do something like the following:
Ramo r = session.load(...);
Alumno p = session.load(...);
Transaction transaction = sess.getTransaction();
transaction.beginTransaction();
RamoAlumno rp = new RamoAlumno();
rp.setEstado(estado);
rp.setSemestre(sem);
...
r.add( rp );
p.add( rp );
transaction.commit();
And inside your methods you'd have:
class Ramo {
public void add( RamoAlumno ra ) {
ramoAlumnos.add( ra );
ra.setRamo( this );
}
}
and vice versa for your Alumno class with a similiar method.
Remember when you use inverse=true on your one-to-many relationships Hibernate won't modify the DB when you change those. You have to modify the RamoAlumno object for hibernate to notice there has been a change that needs to be saved to the DB. That's why it's a good idea to do the above code and encapsulate those calls behind a method so you don't forget to modify the persistant side of the relationship.
I wonder if you tried removing the session.save() call and just let everything commit when the transaction is committed if it would work.
Charlie