-->
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.  [ 10 posts ] 
Author Message
 Post subject: HELP ME:composite-id insert fail in 2 one-to-many (N:M)
PostPosted: Mon Nov 27, 2006 8:30 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Hi again.
I'm handle a many-to-many relationship with a intermediate class, and I mapped this as two one-to-many associations between three entities.

Alumno --- one-to-many ---> RamoAlumno
Ramo ---- one-to-many ----> RamoAlumno

I use composite-id in RamoAlumno.

My problem is: I can't insert RamoAlumno in the database :(.
Before a commit sentence, relationship works fine, and I can print information.

// p belong to Alumno

for (Iterator it = p.getRamosAlumno().iterator(); it.hasNext();) {
RamoAlumno ram = (RamoAlumno) it.next();
out.println("<p>ramo del alumno:"+ram.getAlumno().getNombre());
out.println("ramo-alumno2:"+ram.getRamo().getNombre());
}
// this print fine

But after commit, I look my database, and RamoAlumno is not persist (don't save it).

Please, I need help. I'm very sad because early I don't use composite-id and I did can insert in database fine, but I can't handle relationship well, because just one work fine (the other is dead :S ), and I guess that is not well. I did write in the forum about that, but was'nt anybody response to me. I'm newbie, I need help, If you want, I'll send you my whole system, with a picture about UML design.

Hibernate version: Hibernate 3.1.3

Mapping documents:

//persona.hbm.xml (Alumno is a subclass)

<?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="encuestas.Persona"
table="PERSONA"
lazy="true"
abstract="true">

<id name="id"
type="long"
column="PERSONA_ID"
access="field">
<generator class="native"/>
</id>
<property name="rut"
type="long"
not-null="true">
<column name="RUT"/>
</property>
....

<joined-subclass name="encuestas.Alumno" table="ALUMNO">
<key column="ALUMNO_ID"/>

<property name="password"
type="string"
not-null="true">
<column name="PASSW"/>
</property>

<set name="Formas_rptas"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="alumnoID" />
</key>
<one-to-many class="encuestas.FormaRpta"/>
</set>

<set name="ramosAlumno"
lazy="true"
inverse="true"
cascade="all, delete-orphan"
fetch="subselect"
access="field">
<key foreign-key="FK1_RAMO_ALUMNO_ID">
<column name="alumnoID" not-null="true"/>
</key>
<one-to-many class="encuestas.RamoAlumno"/>
</set>


<!-- It don't allow bidirectional relationship (Ramo-->) because that, I comment it
<set name="ramos" lazy="true" table="ALUMNO_RAMO">
<key column="alumnoID"/>
<composite-element class="encuestas.AlumnoRamo">
<parent name="alumno"/>
<many-to-one name="ramo"
class="encuestas.Ramo"
column="ramoID"
not-null="true"/>
<property name="semestre" column="SEMESTRE" not-null="true"/>
<property name="agno" column="AGNO" not-null="true"/>
<property name="estado" column="ESTADO" not-null="true"/>
</composite-element>
</set> -->

</joined-subclass>
<joined-subclass name="encuestas.Profesor" table="PROFESOR">
<key column="PROFESOR_ID"/>
<property name="tipo"
type="string"
not-null="true">
<column name="TIPO"/>
</property>
<!-- this is my other way to handle many-to-many, but it don't work fine, because I lose Ramo---RamoProfesor relationship -->
<set name="Ramo_profesor"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="profesorID" />
</key>
<one-to-many class="encuestas.RamoProfesor"/>
</set>

</joined-subclass>


</class>

</hibernate-mapping>


Ramo.hbm.xml

<?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="encuestas.Ramo" table="RAMO" lazy="true">

<id name="id"
type="long"
column="RAMO_ID"
unsaved-value="null"
access="field">
<generator class="native"/>
</id>
<version name="version"
column="VERSION"
access="org.hibernate.property.DirectPropertyAccessor"/>

<property name="nombre"
type="string"
not-null="true"
access="field">
<column name="NOMBRE"/>
</property>
<property name="numero"
type="int"
not-null="true">
<column name="NUMERO"/>
</property>
...
<!--this don't work in r.getRamoAlumno-->
<set name="Ramo_profesor"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="ramoID" />
</key>
<one-to-many class="encuestas.RamoProfesor"/>
</set>
<!-- I don't know why-->

<set name="ramosAlumno"
lazy="true"
inverse="true"
cascade="all, delete-orphan"
access="field">
<key foreign-key="FK2_RAMO_ALUMNO_ID">
<column name="ramoID" not-null="true"/>
</key>
<one-to-many class="encuestas.RamoAlumno"/>
</set>


<set name="Formas_rptas"
lazy="true"
inverse="true"
cascade="delete" >
<key>
<column name="ramoID" />
</key>
<one-to-many class="encuestas.FormaRpta"/>
</set>

</class>

</hibernate-mapping>

RamoAlumno.hbm.xml

<?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 package="encuestas">
<class name="RamoAlumno" table="RAMO_ALUMNO">

<composite-id name="id"
class="RamoAlumno$Id"
access="field">

<key-property name="alumnoId"
access="field"
column="ALUMNO_ID"/>

<key-property name="ramoId"
access="field"
column="RAMO_ID"/>
</composite-id>

<!-- I don't know if Date is set automatic or I need to do that
<property name="fechaTomado"
column="FECHA_TOMADO"
type="timestamp"
not-null="true"
access="field"/> -->

<property name="semestre"
type="string"
not-null="true"
update="false"
access="field">
<column name="SEMESTRE"/>
</property>
<property name="agno"
type="int"
not-null="true"
update="false"
access="field">
<column name="AGNO"/>
</property>
<property name="estado"
type="string"
not-null="true"
access="field">
<column name="ESTADO"/>
</property>

<many-to-one
name="alumno"
column="ALUMNO_ID"
not-null="true"
access="field"
insert="false"
update="false"
/>

<many-to-one
name="ramo"
column="RAMO_ID"
not-null="true"
access="field"
insert="false"
update="false"/>

</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

sess.beginTransaction();
Query q = sess.createQuery("from Persona r where r.rut = :rut");
q.setString("rut", rut);
Alumno p = (Alumno) q.uniqueResult();
p.getId();
out.println("<p>nombre alum:"+p.getNombre() +" "+p.getApellido_pat());
for(i=0;i < j;i++){
ids=request.getParameter("ch"+Integer.toString(i+1));

if(ids!=null){
id_ramo=Long.valueOf((ids));
//out.println(" idramo:"+id_ramo);

Ramo r = (Ramo) sess.load(Ramo.class, id_ramo);
nom_ramo = r.getNombre();
out.println("<p>nombre ramo:"+r.getNombre()+" id_ramo:"+r.getId()+"</p>");

RamoAlumno.Id id_ra = new RamoAlumno.Id(p.getId(),r.getId());
RamoAlumno rp = new RamoAlumno();
rp.setId(id_ra);
rp.setAlumno(p);
rp.setRamo(r);
rp.setSemestre(sem);
rp.setAgno(agno);
rp.setEstado(estado);
sess.saveOrUpdate(rp);
//sess.flush(); this don't work :(
out.println("<p>constructor ramo_alumno</p>");

out.println("<p>estado:"+rp.getEstado());

sess.persist(rp);
//sess.saveOrUpdate(rp);

r.getRamosAlumno().add(rp);
out.println("<p>r add ramo_alumno</p>");
p.getRamosAlumno().add(rp);
out.println("<p>p add ramo_alumno</p>");
sess.saveOrUpdate(r);
out.println("<p> save-upd r</p>");
sess.saveOrUpdate(p);
out.println("<p> save-upd p</p>");

}
}


for (Iterator it = p.getRamosAlumno().iterator(); it.hasNext();) {
RamoAlumno ram = (RamoAlumno) it.next();
out.println("<p>ram del al2:"+ram.getAlumno().getNombre());
out.println("ramo-alumno2:"+ram.getRamo().getNombre());

}
sess.getTransaction().commit();

Full stack trace of any exception that occurs:

nothing

Name and version of the database you are using:

Mysql 5.0.22-Debian_0ubuntu6.06.2

The generated SQL (show_sql=true):

Thank you. I hope your help

My email: panarch@gmail.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 4:03 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
saveOrUpdate will not work correctly with a class that has composite-id unless you also use <version> or you define Interceptor.isUnsaved().

Code:
RamoAlumno.Id id_ra = new RamoAlumno.Id(p.getId(),r.getId());
RamoAlumno rp = new RamoAlumno();
rp.setId(id_ra);
rp.setAlumno(p);
rp.setRamo(r);
rp.setSemestre(sem);
rp.setAgno(agno);
rp.setEstado(estado);
sess.saveOrUpdate(rp);


Use sess.save(rp) instead to insert.


Top
 Profile  
 
 Post subject: This don't work :(
PostPosted: Tue Nov 28, 2006 4:01 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Hello JayeshJ:
Thank you, but my problem is not solved. Before I did use sess.save(rp), and I tried again now, but it don't insert.
After I did add Date to RamoAlumno.hbm.xml

<property name="dateAdded"
column="ADDED_ON"
type="timestamp"
not-null="true"
access="field"/>

and in RamoAlumno.java: private Date dateAdded = new Date();

with its getter: public Date getDateAdded() { return dateAdded; }

This does nothing. The problem is not solved and before commit, it prints the relationship correctly, but after commit, RamoAlumno is not in database.

Next I did follow one example from hibernate in Action, although my program is a servlet/jsp and I have begin transaction and the example dont.

Example from Hibernate in Action: here, CategoryzedItem is the intermediate table (class) in many-to-many relationship between Category and Item.

public abstract class TestCaseWithData extends HibernateTest {

Category cars;
Category carsLuxury;
Category carsSUV;
...
protected void initData() {
// Prepare DAOS
CategoryDAO catDAO = DAOFACTORY.getCategoryDAO();
UserDAO userDAO = DAOFACTORY.getUserDAO();
ItemDAO itemDAO = DAOFACTORY.getItemDAO();
CommentDAO commentDAO = DAOFACTORY.getCommentDAO();
....
carsLuxury = new Category("Luxury Cars");
...
auctionFour = new Item("Item F....

itemDAO.makePersistent(auctionFour);
new CategorizedItem(u1.getUsername(), carsLuxury, auctionFour);

...
}

public void inTransaction(){
initData();
sessionFactory.getCurrentSession().flush();
sessionFactory.getCurrentSession().clear();
}

}

I don't know about DAO :(. But from here I follow the handle to create RamoAlumno:

new RamoAlumno(sem, agno, estado, p, r);

without sess.save() and p.getRamoAlumno().add(rp); and r.getRamoAlumno().add(rp); With and wihtout flush too.

and my program continues with the same output and the problem (after commit RamoAlumno is not in database).

Well, I did follow your <version>, add it to RamoAlumno.
RamoAlumno.hbm.xml:

<version name="version"
column="VERSION"
access="org.hibernate.property.DirectPropertyAccessor"/>

RamoAlumno.java

private int version = 0;
..
public int getVersion(){
return version;
}

But it gives me other problem. p.getRamoAlumno().iterator() or p.getRamoAlumno().size() don't work. My program is blocked.

In RamoAlumno.java I have the next methods:

public int compareTo(Object o) {
// CategorizedItems are sorted by date
if (o instanceof RamoAlumno)
return getDateAdded().compareTo( ((RamoAlumno)o).getDateAdded() );
return 0;
}

public String toString() {
return "Added by: '" + getAlumno() + "', " +
"On Date: '" + getDateAdded();
}


public boolean equals(Object o) {
String cadena = null;
if (this == o) return true;
if (!(o instanceof RamoAlumno)) return false;

final RamoAlumno r = (RamoAlumno) o;

cadena = String.valueOf(getId().alumnoId) + String.valueOf(getId().ramoId);
if (cadena != null ? !cadena.equals(String.valueOf(r.getId().alumnoId)+String.valueOf(r.getId().ramoId)) : (String.valueOf(r.getId().alumnoId)+String.valueOf(r.getId().ramoId)) != null) return false;

return true;
}


public int hashCode() {
int result;
result = ((String.valueOf(getId().alumnoId) + String.valueOf(getId().ramoId)) != null ? (String.valueOf(getId().alumnoId) + String.valueOf(getId().ramoId)).hashCode() : 0);
return result;
}

I'm exhausted :( , but I want to do my proyect fine. I need more help. If you want I'll send you my whole system or just Ramo, Alumno and RamoAlumno files. Thank you very much.

Paola


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 2:40 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Move your composite-id, Id to a seperate class i.e. have a separate identifier class.

Read 5.1.5. composite-id from Hibernate reference doc.

I suggest that you try and solve the problem in small steps.

First try and save a simple class (use a simpler version of RamoAlumno) with a composite-id, which does not have any relationships and has minimum properties.

In you hibernate configuration set

hibernate.show_sql true

This would allow you to see the SQLs.

If you want the date to be set automatically you could use the <timestamp> property. This is also similar to <version> tag.


Top
 Profile  
 
 Post subject: again...
PostPosted: Thu Nov 30, 2006 7:45 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Hello:
JayeshJ, I did follow your suggestion, although the "..composite-id, Id to a seperate class..", I don't know how is it. I just was following a example from hibernate in Action, where it use one inner class.

Again, I did make
Alumno - one-to-many ---> RamoAlumno<---- many-to-one - Ramo
but more simple.

RamoAlumno.hbm
<?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 package="encuestas">
<class name="RamoAlumno" table="RAMO_ALUMNO" mutable="false">

<composite-id name="id"
class="RamoAlumno$Id"
access="field">

<key-property name="alumnoId"
access="field"
column="ALUMNO_ID"/>

<key-property name="ramoId"
access="field"
column="RAMO_ID"/>
</composite-id>

<!--
-->
<version name="version"
column="VERSION"
access="org.hibernate.property.DirectPropertyAccessor"/>

<property name="dateAdded"
column="ADDED_ON"
type="timestamp"
not-null="true"
access="field"/>

<property name="semestre"
type="string"
not-null="true"
update="false"
access="field">
<column name="SEMESTRE"/>
</property>

<many-to-one
name="alumno"
column="ALUMNO_ID"
not-null="true"
access="field"
insert="false"
update="false"
/>

<many-to-one
name="ramo"
column="RAMO_ID"
not-null="true"
access="field"
insert="false"
update="false"/>

</class>

</hibernate-mapping>

Alumno.hbm.xml
<?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="encuestas.Alumno"
table="ALUMNO"
lazy="true"
abstract="true">

<id name="id"
type="long"
column="ALUMNO_ID"
access="field">
<generator class="native"/>
</id>
<property name="rut"
type="long"
not-null="true">
<column name="RUT"/>
</property>

<property name="password"
type="string"
not-null="true">
<column name="PASSW"/>
</property>

<set name="ramosAlumno"
lazy="true"
inverse="true"
cascade="all, delete-orphan"
fetch="subselect"
access="field">
<key foreign-key="FK1_RAMO_ALUMNO_ID">
<column name="alumnoID" not-null="true"/>
</key>
<one-to-many class="encuestas.RamoAlumno"/>
</set>

</class>

</hibernate-mapping>

Ramo.hbm.xml

<?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="encuestas.Ramo" table="RAMO" lazy="true">

<id name="id"
type="long"
column="RAMO_ID"
unsaved-value="null"
access="field">
<!-- <meta attribute="finder-method">findByID</meta> -->
<generator class="native"/>
</id>
<version name="version"
column="VERSION"
access="org.hibernate.property.DirectPropertyAccessor"/>

<property name="nombre"
type="string"
not-null="true"
access="field">
<column name="NOMBRE"/>
</property>

<set name="ramosAlumno"
lazy="true"
inverse="true"
cascade="all, delete-orphan"
access="field">
<key foreign-key="FK2_RAMO_ALUMNO_ID">
<column name="ramoID" not-null="true"/>
</key>
<one-to-many class="encuestas.RamoAlumno"/>
</set>

</class>

</hibernate-mapping>

RamoAlumno.java
public class RamoAlumno implements Serializable {
public static class Id implements Serializable {

private Long alumnoId;
private Long ramoId;

public Id() {}

public Id(Long alumnoId, Long ramoId) {
this.alumnoId = alumnoId;
this.ramoId = ramoId;
}

public boolean equals(Object o) {
if (o instanceof Id) {
Id that = (Id)o;
return this.alumnoId.equals(that.alumnoId) &&
this.ramoId.equals(that.ramoId);
} else {
return false;
}
}

public int hashCode() {
return alumnoId.hashCode() + ramoId.hashCode();
}
}


private Id id = new Id();
//private Date fechaTomado = new Date();
private int version = 0;
private Date dateAdded = new Date();
private String semestre;
private Ramo ramo;
private Alumno alumno;

public RamoAlumno() {};

public RamoAlumno(String semestre, Alumno alumno, Ramo ramo ) {
this.semestre = semestre;
this.alumno = alumno;
this.ramo = ramo;

// Set identifier values
this.id.alumnoId = alumno.getId();
this.id.ramoId = ramo.getId();

// Guarantee referential integrity
alumno.getRamosAlumno().add(this);
ramo.getRamosAlumno().add(this);
}

public Id getId() { return id; }

//public Date getFechaTomado() { return fechaTomado; }
public int getVersion(){
return version;
}
public Date getDateAdded() { return dateAdded; }
public String getSemestre(){
return semestre;
}
public void setSemestre(String sem){
this.semestre = sem;
}


public Alumno getAlumno() { return alumno; }

public Ramo getRamo() { return ramo; }


// ********************** Common Methods ********************** //

public int compareTo(Object o) {
// CategorizedItems are sorted by date
if (o instanceof RamoAlumno)
return getDateAdded().compareTo( ((RamoAlumno)o).getDateAdded() );
return 0;
}

public String toString() {
return "Added by: '" + getAlumno() + "', " +
"On Date: '" + getDateAdded();
}


public boolean equals(Object o) {
String cadena = null;
if (this == o) return true;
if (!(o instanceof RamoAlumno)) return false;

final RamoAlumno r = (RamoAlumno) o;

cadena = String.valueOf(getId().alumnoId) + String.valueOf(getId().ramoId);
if (cadena != null ? !cadena.equals(String.valueOf(r.getId().alumnoId)+String.valueOf(r.getId().ramoId)) : (String.valueOf(r.getId().alumnoId)+String.valueOf(r.getId().ramoId)) != null) return false;

return true;
}


public int hashCode() {
int result;
result = ((String.valueOf(getId().alumnoId) + String.valueOf(getId().ramoId)) != null ? (String.valueOf(getId().alumnoId) + String.valueOf(getId().ramoId)).hashCode() : 0);
return result;
}
}

Alumno.java
public class Alumno implements Serializable {
private Long id;
private Long rut;
private String password;

private Set ramosAlumno = new HashSet();

public Alumno() {}

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}

public Long getRut() {
return rut;
}
public void setRut(Long rut) {
this.rut = rut;
}

public String getPassword() {
return password;
}
public void setPassword(String pass) {
this.password = pass;
}

public Set getRamosAlumno(){
return ramosAlumno;
}
public void setRamo_alumno(Set ramo_al){
this.ramosAlumno = ramo_al;
}

public void addRamoAlumno(RamoAlumno ramAl) {
if (ramAl == null)
throw new IllegalArgumentException("No se puede agregar un null RamoAlumno.");
this.getRamosAlumno().add(ramAl);
}

public int compareTo(Object o) {
if (o instanceof Alumno) {
return this.getRut().compareTo( ((Alumno)o).getRut() );
}
return 0;
}
}

Ramo.java
public class Ramo implements Serializable {
private Long id;
private int version = 0;
private String nombre;

private Set ramosAlumno = new HashSet();

public Ramo() {};

public Ramo(String nombre, int numero,int semestre_correspdte, int horas_catedra,int horas_lab,String tipo, Set ramAl, Set ramProf, Set fr) {
this.nombre = nombre;
this.ramosAlumno = ramAl;

}

public Long getId(){
return id;
}
/* public void setId(Long id){
this.id = id;
}*/
public int getVersion(){
return version;
}
public String getNombre(){
return nombre;
}
public void setNombre(String nombre){
this.nombre = nombre;
}
//
public Set getRamosAlumno(){
return ramosAlumno;
}
public void setRamosAlumno(Set ramal){
this.ramosAlumno = ramal;
}
public void addRamoAlumno(RamoAlumno ramAl) {
if (ramAl == null)
throw new IllegalArgumentException("No add a null RamoAlumno.");
this.getRamosAlumno().add(ramAl);
}

public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Ramo)) return false;

final Ramo r = (Ramo) o;

if (nombre != null ? !nombre.equals(r.nombre) : r.nombre != null) return false;

return true;
}

public int hashCode() {
int result;
result = (nombre != null ? nombre.hashCode() : 0);
return result;
}

public String toString() {
return "Item ('" + getId() + "'), " +
"Name: '" + getNombre() + "' ";
}

public int compareTo(Object o) {
if (o instanceof Ramo) {
return this.getNombre().compareTo( ((Ramo)o).getNombre() );

}
return 0;
}

}

And a manager class: EncuestaManager

public class EncuestaManager {

public static void main(String[] args) {
EncuestaManager mgr = new EncuestaManager();

if (args[0].equals("store")) {
mgr.createAndStoreAlumnos(new Long(14104), "pola");
mgr.createAndStoreAlumnos(new Long(15006), "jero");
mgr.createAndStoreRamos("BD");
mgr.createAndStoreRamos("topicos");
mgr.addRamosDelAl("II",new Long(1),new Long(1));
}

else if (args[0].equals("list")) {

List alumnos = mgr.listAlumnos();
List ramos = mgr.listRamos();

for (int i = 0; i < alumnos.size(); i++) {
Alumno un_al = (Alumno) alumnos.get(i);
System.out.println("Id Alumno: " + un_al.getId() +
" Rut Alumno: " + un_al.getRut() +
" Pass: " + un_al.getPassword());
}
for (int i = 0; i < ramos.size(); i++) {
Ramo un_r = (Ramo) ramos.get(i);
System.out.println("IdRamo: " + un_r.getId() +
" Ramo: " + un_r.getNombre());
}

}
HibernateUtil.getSessionFactory().close();
}

private void createAndStoreAlumnos(Long rut, String pass) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Alumno al = new Alumno();
al.setRut(rut);
al.setPassword(pass);
session.save(al);
session.getTransaction().commit();
}

private void createAndStoreRamos(String nom) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Ramo r = new Ramo();
r.setNombre(nom);
session.save(r);
session.getTransaction().commit();
}

private void addRamosDelAl(String sem, Long idal, Long idr) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Alumno el_al = (Alumno) session.load(Alumno.class, idal);
Ramo el_r = (Ramo) session.load(Ramo.class, idr);
RamoAlumno rp = new RamoAlumno(sem,el_al,el_r);
//session.save(rp);
session.getTransaction().commit();

}

private List listAlumnos() {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Alumno").list();
session.getTransaction().commit();
return result;

}

private List listRamos() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Ramo").list();
session.getTransaction().commit();
return result;
}
}

Well. I did save fine Alumno and Ramo. But When I did try to do persist RamoAlumno, I did have the next errors:

Case 1: In addRamosDelAl(String sem, Long idal, Long idr)
without session.save(rp); (commented //session.save(rp))
...
[java] 16:09:49,312 INFO SessionFactoryImpl:154 - building session factory
[java] 16:09:50,319 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
[java] Hibernate: select alumno0_.ALUMNO_ID as ALUMNO1_0_0_, alumno0_.RUT as RUT0_0_, alumno0_.PASSW as PASSW0_0_ from ALUMNO alumno0_ where alumno0_.ALUMNO_ID=?
[java] Hibernate: select ramo0_.RAMO_ID as RAMO1_1_0_, ramo0_.VERSION as VERSION1_0_, ramo0_.NOMBRE as NOMBRE1_0_ from RAMO ramo0_ where ramo0_.RAMO_ID=?
[java] Hibernate: select ramosalumn0_.alumnoID as alumnoID1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_1_, ramosalumn0_.RAMO_ID as RAMO2_1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_2_0_, ramosalumn0_.RAMO_ID as RAMO2_2_0_, ramosalumn0_.VERSION as VERSION2_0_, ramosalumn0_.ADDED_ON as ADDED4_2_0_, ramosalumn0_.SEMESTRE as SEMESTRE2_0_ from RAMO_ALUMNO ramosalumn0_ where ramosalumn0_.alumnoID=?
[java] Hibernate: select ramosalumn0_.ramoID as ramoID1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_1_, ramosalumn0_.RAMO_ID as RAMO2_1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_2_0_, ramosalumn0_.RAMO_ID as RAMO2_2_0_, ramosalumn0_.VERSION as VERSION2_0_, ramosalumn0_.ADDED_ON as ADDED4_2_0_, ramosalumn0_.SEMESTRE as SEMESTRE2_0_ from RAMO_ALUMNO ramosalumn0_ where ramosalumn0_.ramoID=?
[java] Hibernate: select ramoalumno_.ALUMNO_ID, ramoalumno_.RAMO_ID, ramoalumno_.VERSION as VERSION2_, ramoalumno_.ADDED_ON as ADDED4_2_ from RAMO_ALUMNO ramoalumno_ where ramoalumno_.ALUMNO_ID=? and ramoalumno_.RAMO_ID=?
[java] Hibernate: insert into RAMO_ALUMNO (VERSION, ADDED_ON, SEMESTRE, ALUMNO_ID, RAMO_ID) values (?, ?, ?, ?, ?)
[java] Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
[java] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
[java] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
...

Case 2: addRamosDelAl(String sem, Long idal, Long idr)
with session.save(rp); (no commented)
...
[java] 16:13:24,972 INFO SessionFactoryImpl:154 - building session factory
[java] 16:13:25,952 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
[java] Hibernate: select alumno0_.ALUMNO_ID as ALUMNO1_0_0_, alumno0_.RUT as RUT0_0_, alumno0_.PASSW as PASSW0_0_ from ALUMNO alumno0_ where alumno0_.ALUMNO_ID=?
[java] Hibernate: select ramo0_.RAMO_ID as RAMO1_1_0_, ramo0_.VERSION as VERSION1_0_, ramo0_.NOMBRE as NOMBRE1_0_ from RAMO ramo0_ where ramo0_.RAMO_ID=?
[java] Hibernate: select ramosalumn0_.alumnoID as alumnoID1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_1_, ramosalumn0_.RAMO_ID as RAMO2_1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_2_0_, ramosalumn0_.RAMO_ID as RAMO2_2_0_, ramosalumn0_.VERSION as VERSION2_0_, ramosalumn0_.ADDED_ON as ADDED4_2_0_, ramosalumn0_.SEMESTRE as SEMESTRE2_0_ from RAMO_ALUMNO ramosalumn0_ where ramosalumn0_.alumnoID=?
[java] Hibernate: select ramosalumn0_.ramoID as ramoID1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_1_, ramosalumn0_.RAMO_ID as RAMO2_1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_2_0_, ramosalumn0_.RAMO_ID as RAMO2_2_0_, ramosalumn0_.VERSION as VERSION2_0_, ramosalumn0_.ADDED_ON as ADDED4_2_0_, ramosalumn0_.SEMESTRE as SEMESTRE2_0_ from RAMO_ALUMNO ramosalumn0_ where ramosalumn0_.ramoID=?
[java] Hibernate: insert into RAMO_ALUMNO (VERSION, ADDED_ON, SEMESTRE, ALUMNO_ID, RAMO_ID) values (?, ?, ?, ?, ?)
[java] 16:13:27,081 WARN JDBCExceptionReporter:71 - SQL Error: 1364, SQLState: HY000
[java] 16:13:27,082 ERROR JDBCExceptionReporter:72 - Field 'alumnoID' doesn't have a default value
[java] 16:13:27,085 ERROR AbstractFlushingEventListener:300 - Could not synchronize database state with session
[java] org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
[java] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
...

Case 3: addRamosDelAl(String sem, Long idal, Long idr)
with session.saveOrUpdate(rp); (no commented)

[java] 16:14:46,565 INFO SessionFactoryImpl:154 - building session factory
[java] 16:14:47,549 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
[java] Hibernate: select alumno0_.ALUMNO_ID as ALUMNO1_0_0_, alumno0_.RUT as RUT0_0_, alumno0_.PASSW as PASSW0_0_ from ALUMNO alumno0_ where alumno0_.ALUMNO_ID=?
[java] Hibernate: select ramo0_.RAMO_ID as RAMO1_1_0_, ramo0_.VERSION as VERSION1_0_, ramo0_.NOMBRE as NOMBRE1_0_ from RAMO ramo0_ where ramo0_.RAMO_ID=?
[java] Hibernate: select ramosalumn0_.alumnoID as alumnoID1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_1_, ramosalumn0_.RAMO_ID as RAMO2_1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_2_0_, ramosalumn0_.RAMO_ID as RAMO2_2_0_, ramosalumn0_.VERSION as VERSION2_0_, ramosalumn0_.ADDED_ON as ADDED4_2_0_, ramosalumn0_.SEMESTRE as SEMESTRE2_0_ from RAMO_ALUMNO ramosalumn0_ where ramosalumn0_.alumnoID=?
[java] Hibernate: select ramosalumn0_.ramoID as ramoID1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_1_, ramosalumn0_.RAMO_ID as RAMO2_1_, ramosalumn0_.ALUMNO_ID as ALUMNO1_2_0_, ramosalumn0_.RAMO_ID as RAMO2_2_0_, ramosalumn0_.VERSION as VERSION2_0_, ramosalumn0_.ADDED_ON as ADDED4_2_0_, ramosalumn0_.SEMESTRE as SEMESTRE2_0_ from RAMO_ALUMNO ramosalumn0_ where ramosalumn0_.ramoID=?
[java] Hibernate: select ramoalumno_.ALUMNO_ID, ramoalumno_.RAMO_ID, ramoalumno_.VERSION as VERSION2_, ramoalumno_.ADDED_ON as ADDED4_2_ from RAMO_ALUMNO ramoalumno_ where ramoalumno_.ALUMNO_ID=? and ramoalumno_.RAMO_ID=?

[java] Hibernate: insert into RAMO_ALUMNO (VERSION, ADDED_ON, SEMESTRE, ALUMNO_ID, RAMO_ID) values (?, ?, ?, ?, ?)
[java] Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update

[java] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
...
Case 4: addRamosDelAl(String sem, Long idal, Long idr)
with
Alumno el_al = (Alumno) session.load(Alumno.class, idal);
Ramo el_r = (Ramo) session.load(Ramo.class, idr);

el_al.getId();
el_r.getId();
System.out.println("id al:"+el_al.getId());
System.out.println("id r:"+el_r.getId());

RamoAlumno rp = new RamoAlumno(sem,el_al,el_r);
el_al.getRamosAlumno().add(rp);
el_r.getRamosAlumno().add(rp);
session.saveOrUpdate(rp);
....

[java] Hibernate: select ramoalumno_.ALUMNO_ID, ramoalumno_.RAMO_ID, ramoalumno_.VERSION as VERSION2_, ramoalumno_.ADDED_ON as ADDED4_2_ from RAMO_ALUMNO ramoalumno_ where ramoalumno_.ALUMNO_ID=? and ramoalumno_.RAMO_ID=?
[java] Hibernate: insert into RAMO_ALUMNO (VERSION, ADDED_ON, SEMESTRE, ALUMNO_ID, RAMO_ID) values (?, ?, ?, ?, ?)
[java] Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
...
Case 5: Without version in RamoAlumno
Without version and with session.saveOrUpdate(rp);
<!--

<version name="version"
column="VERSION"
access="org.hibernate.property.DirectPropertyAccessor"/>
-->
session.saveOrUpdate(rp);

[java] Hibernate: select ramoalumno_.ALUMNO_ID, ramoalumno_.RAMO_ID, ramoalumno_.ADDED_ON as ADDED3_2_ from RAMO_ALUMNO ramoalumno_ where ramoalumno_.ALUMNO_ID=? and ramoalumno_.RAMO_ID=?
[java] Hibernate: insert into RAMO_ALUMNO (ADDED_ON, SEMESTRE, ALUMNO_ID, RAMO_ID) values (?, ?, ?, ?)
[java] Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update

Well. I hope your help again. Please.

Best Regards.

Paola


Last edited by panarch on Fri Dec 01, 2006 11:25 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: ---
PostPosted: Thu Nov 30, 2006 7:52 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
I did look in forum hibernate for "Hibernate Error: 1364 Field doesn't have a default value" and I find it:

jmmm82: Posted: Wed Oct 04, 2006 4:26 pm Post subject: Error Help me
...
Hibernate operation: could not insert: [com.promainsur.sigepweb.model.Provincia]; uncategorized SQLException for SQL [insert into provincias (nombre, idMatriz, idProvincia) values (?, ?, ?)]; SQL state [HY000]; error code [1364]; Field 'idPais' doesn't have a default value; nested exception is java.sql.SQLException: Field 'idPais' doesn't have a default value
...

and CWitherow reply:
"Looks to me as if the table contains a column that is not included in your Hibernate mapping AND the column does not have a default value.

More information would help -- follow the posting instructions.

Curtis ... "

Is this my problem? Can you explain this? What is the solution?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 3:18 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Check your Sets ramosAlumnos.

Code:
<set name="ramosAlumno"
lazy="true"
inverse="true"
cascade="all, delete-orphan"
access="field">
<key foreign-key="FK2_RAMO_ALUMNO_ID">
<column name="ramoID" not-null="true"/>
</key>
<one-to-many class="encuestas.RamoAlumno"/>
</set>


<set name="ramosAlumno"
lazy="true"
inverse="true"
cascade="all, delete-orphan"
fetch="subselect"
access="field">
<key foreign-key="FK1_RAMO_ALUMNO_ID">
<column name="alumnoID" not-null="true"/>
</key>
<one-to-many class="encuestas.RamoAlumno"/>
</set>



Shouldn't ramoID here be RAMO_ID and alumnoID be ALUMNO_ID ?

We should have the actual column names here.


Top
 Profile  
 
 Post subject: Thank you JayeshJ, RamoAlumno is save!!
PostPosted: Mon Dec 04, 2006 11:45 am 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Just I have one problem, when I call in the method list()

for (int i = 0; i < alumnos.size(); i++) {
Alumno _al = (Alumno) alumnos.get(i);
System.out.println("Id Alumno: " + _al.getId());//its print fine
for (Iterator it = _al.getRamosAlumno().iterator(); it.hasNext();) {
RamoAlumno ram = (RamoAlumno) it.next();
System.out.println("ram del al:"+ram.getAlumno().getId());
System.out.println("ramo-alumno:"+ram.getRamo().getNombre());

}
}

[java] 11:34:35,732 INFO SessionFactoryImpl:154 - building session factory
[java] 11:34:44,040 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured [java] Hibernate: select alumno0_.ALUMNO_ID as ALUMNO1_0_, alumno0_.RUT as RUT0_, alumno0_.PASSW as PASSW0_ from ALUMNO alumno0_
[java] Hibernate: select ramo0_.RAMO_ID as RAMO1_1_, ramo0_.VERSION as VERSION1_, ramo0_.NOMBRE as NOMBRE1_ from RAMO ramo0_
[java] Id Alumno: 1 Rut Alumno: 14104 Pass: pola
[java] Id Alumno: 2 Rut Alumno: 15006 Pass: jero
[java] IdRamo: 1 Ramo: BD
[java] IdRamo: 2 Ramo: topicos
[java] Id Alumno: 1
[java] 11:34:54,731 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: encuestas.Alumno.ramosAlumno, no session or session was closed
[java] org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: encuestas.Alumno.ramosAlumno, no session or session was closed
[java] at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
[java] at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
[java] at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
[java] at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
[java] at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
[java] at encuestas.EncuestaManager.main(Unknown Source)
[java] Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: encuestas.Alumno.ramosAlumno, no session or session was closed
[java] at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
[java] at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
[java] at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
[java] at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
[java] at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
[java] at encuestas.EncuestaManager.main(Unknown Source)
[java] Java Result: 1

I must to make other session for retrieve RamoAlumno?

Thank you JayeshJ.
Best Regards


Top
 Profile  
 
 Post subject: i did try this...
PostPosted: Mon Dec 04, 2006 11:55 am 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
I did add this in the main method:

else if (args[0].equals("listRA")) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List alumnos = mgr.listAlumnos();
for (int i = 0; i < alumnos.size(); i++) {
Alumno _al = (Alumno) alumnos.get(i);
System.out.println("Id Alumno: " + _al.getId());//its print fine
for (Iterator it = _al.getRamosAlumno().iterator(); it.hasNext();) {
RamoAlumno ram = (RamoAlumno) it.next();
System.out.println("ram del al:"+ram.getAlumno().getId());
System.out.println("ramo-alumno:"+ram.getRamo().getNombre());

}
}
session.getTransaction().commit();
}

But
[java] Id Alumno: 1
[java] 11:53:38,711 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: encuestas.Alumno.ramosAlumno, no session or session was closed


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 3:48 pm 
Newbie

Joined: Mon Sep 25, 2006 7:58 pm
Posts: 19
Location: Chile
Now I run this:

public class ListaRA {
public static void main(String[] args) {

ListaRA lra = new ListaRA();
if (args[0].equals("listRa")) {
lra.listaRA();

}
HibernateUtil.getSessionFactory().close();
}
private void listaRA() {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Alumno _al = (Alumno) session.load(Alumno.class, new Long(1));
for (Iterator it = _al.getRamosAlumno().iterator(); it.hasNext();) {
RamoAlumno ram = (RamoAlumno) it.next();
System.out.println("ram del al:"+ram.getAlumno().getId());
System.out.println("ramo-alumno:"+ram.getRamo().getNombre());

}

session.getTransaction().commit();


}
}


and works fine. Thank you very much!!!
I'm very glad :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

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.