-->
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.  [ 2 posts ] 
Author Message
 Post subject: ClassCastException error with a set
PostPosted: Tue Sep 20, 2005 5:41 pm 
Newbie

Joined: Sat Jul 23, 2005 7:19 pm
Posts: 11
I have a 1 to n relationship between Recetado and HoraRecetado. HorasRecetado is a Set in Recetado (named horarios). In my code when I call horarios.isEmpty() TreeSet (from java.util) throws an exception when it tries to compare something in the set. TreeSet's compare method is called by Hibernate the first time it tries to access the set.

Hibernate version:
3.0

Mapping documents:
I have

HoraRecetado
public class HoraRecetado {

/** Crea una nueva instancia de HoraRecetado */
public HoraRecetado() {
}

private long id;

private Recetado recetado;

private Integer horaRecetado;

private MomentoPrefijado momentoPrefijado;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public Recetado getRecetado() {
return recetado;
}

public void setRecetado(Recetado recetado) {
this.recetado = recetado;
}

public Integer getHoraRecetado() {
return horaRecetado;
}

public void setHoraRecetado(Integer horaRecetado) {
this.horaRecetado = horaRecetado;
}

public MomentoPrefijado getMomentoPrefijado() {
return momentoPrefijado;
}

public void setMomentoPrefijado(MomentoPrefijado momentoPrefijado) {
this.momentoPrefijado = momentoPrefijado;
}

}
<?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="com.neaprog.geriatrico.datos.HoraRecetado"
table="HORAS_RECETADOS">
<id name="id" column="idHoraRecetado">
<generator class="increment"/>
<!-- <generator class="org.hibernate.id.IdentityGenerator"/> -->
</id>
<property name="horaRecetado" type="int" column="horaRecetado"/>
<many-to-one name="momentoPrefijado" column="idmomprefijado"/>
<many-to-one name="recetado" column="idrecetado"/>

</class>
</hibernate-mapping>

<?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="com.neaprog.geriatrico.datos.Recetado"
table="RECETADOS">
<id name="id" column="idRecetado">
<generator class="increment"/>
<!-- <generator class="org.hibernate.id.IdentityGenerator"/> -->
</id>
<property name="fechaInicio" type="date" column="fechaInicio"/>
<property name="fechaFin" type="date" column="fechaFin"/>
<property name="dosis" type="float" column="dosis"/>
<property name="observaciones" type="string" column="observaciones"/>
<property name="intervaloMinutos" type="int" column="intervaloMinutos"/>
<property name="horaBase" type="int" column="horaBase"/>
<many-to-one name="internado" column="noAfiliado"/>
<many-to-one name="medicamento" column="idmedicamento"/>
<set name="horarios"
cascade="all-delete-orphan"
inverse="true"
access="field"
sort="natural">
<key>
<column name="idRecetado"/>
</key>
<one-to-many class="com.neaprog.geriatrico.datos.HoraRecetado"/>
</set>
</class>
</hibernate-mapping>

Recetado
public class Recetado extends Entidad {

/** Crea una nueva instancia de Recetado */
public Recetado() {
}

private static final int LONG_OBSERVACIONES = 200;

public static final int SIN_DATOS = -1;
public static final int INTERVALO_MINUTOS = 0;
public static final int HORARIOS_PREFIJADOS = 1;

private long id;

private Date fechaInicio;

private Date fechaFin;

private float dosis;

private Internado internado;

private Medicamento medicamento;

private String observaciones;

private Integer intervaloMinutos;

private Integer horaBase;

private Set<HoraRecetado> horarios;

public String getTextoDescriptivo() {
DateFormat df = DateFormat.getDateInstance();
return df.format(getFechaInicio()) + " - " + df.format(getFechaFin());
}

public Date getFechaInicio() {
return fechaInicio;
}

public void setFechaInicio(Date fechaInicio) {
this.fechaInicio = fechaInicio;
}

public Date getFechaFin() {
return fechaFin;
}

public void setFechaFin(Date fechaFin) {
this.fechaFin = fechaFin;
}

public float getDosis() {
return dosis;
}

public void setDosis(float dosis) {
this.dosis = dosis;
}

public Internado getInternado() {
return internado;
}

public void setInternado(Internado internado) {
this.internado = internado;
}

public Medicamento getMedicamento() {
return medicamento;
}

public void setMedicamento(Medicamento medicamento) {
this.medicamento = medicamento;
}

public String getObservaciones() {
return observaciones;
}

public void setObservaciones(String observaciones) {
this.observaciones = this.getString(observaciones, LONG_OBSERVACIONES);
}

public Integer getIntervaloMinutos() {
return intervaloMinutos;
}

public void setIntervaloMinutos(Integer intervaloMinutos) {
this.intervaloMinutos = intervaloMinutos;
}

public Integer getHoraBase() {
return horaBase;
}

public void setHoraBase(Integer horaBase) {
this.horaBase = horaBase;
}

public Set<HoraRecetado> getHorarios() {
return horarios;
}

public void setHorarios(Set<HoraRecetado> horarios) {
this.horarios = horarios;
}

public int getHorario() {
if (horaBase != null) {
return INTERVALO_MINUTOS;
} else {
if (horarios.isEmpty()) {
return SIN_DATOS;
} else {
return HORARIOS_PREFIJADOS;
}
}

}

public String getHorarioDescriptivo() {
switch(getHorario()) {
case SIN_DATOS:
return "Sin datos";
case INTERVALO_MINUTOS:
Calendar c = getIntervaloHoras();
int horas = c.get(Calendar.HOUR_OF_DAY);
int minutos = c.get(Calendar.MINUTE);
String resul = "Cada " + String.valueOf(horas) + " horas";
if (c.get(Calendar.MINUTE) > 0) {
resul.concat(" y " + minutos + " minutos");
}
return resul;
case HORARIOS_PREFIJADOS:
return "Horarios prefijadas";
default:
return "Error";
}
}

public Calendar getIntervaloHoras() {
Calendar c = Calendar.getInstance();

if (intervaloMinutos == null) {
return null;
} else {
c.set(Calendar.HOUR_OF_DAY, intervaloMinutos / 60);
c.set(Calendar.MINUTE, intervaloMinutos % 60);
return c;
}

}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public boolean equals(Object o) {
if (o instanceof Recetado) {
Recetado that = (Recetado) o;
return this.medicamento.equals(that.getMedicamento()) &&
this.fechaInicio.equals(that.getFechaInicio()) &&
this.fechaFin.equals(that.getFechaFin()) &&
this.dosis == that.getDosis();
}
return (this == o);
}

public int hashCode() {
return this.medicamento.hashCode() + this.fechaInicio.hashCode() + this.fechaFin.hashCode() +
(new Float(dosis)).hashCode();
}
}

<?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="com.neaprog.geriatrico.datos.Recetado"
table="RECETADOS">
<id name="id" column="idRecetado">
<generator class="increment"/>
<!-- <generator class="org.hibernate.id.IdentityGenerator"/> -->
</id>
<property name="fechaInicio" type="date" column="fechaInicio"/>
<property name="fechaFin" type="date" column="fechaFin"/>
<property name="dosis" type="float" column="dosis"/>
<property name="observaciones" type="string" column="observaciones"/>
<property name="intervaloMinutos" type="int" column="intervaloMinutos"/>
<property name="horaBase" type="int" column="horaBase"/>
<many-to-one name="internado" column="noAfiliado"/>
<many-to-one name="medicamento" column="idmedicamento"/>
<set name="horarios"
cascade="all-delete-orphan"
inverse="true"
access="field"
sort="natural">
<key>
<column name="idRecetado"/>
</key>
<one-to-many class="com.neaprog.geriatrico.datos.HoraRecetado"/>
</set>
</class>
</hibernate-mapping>

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

Full stack trace of any exception that occurs:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.neaprog.geriatrico.datos.HoraRecetado
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)
at java.util.AbstractCollection.addAll(AbstractCollection.java:318)
at java.util.TreeSet.addAll(TreeSet.java:258)
at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:242)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:183)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:268)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:249)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:554)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:541)
at org.hibernate.loader.Loader.doQuery(Loader.java:436)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)
at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:111)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:176)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:118)
at com.neaprog.geriatrico.datos.Recetado.getHorario(Recetado.java:135)

Name and version of the database you are using:
MS SQL Server 2000. jTDS 1.1


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 5:50 pm 
Newbie

Joined: Sat Jul 23, 2005 7:19 pm
Posts: 11
Sorry, I solved it. I didn't realize I had put sort="natural" and didn't implement a compare() method.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.