Hi,
I have a many-to-many association between Grupo <---> Usuario, where the association have an attribute (Class GrupoUsuario).
I am using a composite-element mapping. When I load a 'group' with 'users' associated, i obtain a exception in the client when try to calculate the hashcode in the class 'GrupoUsuario', because the property grupoId of grupo is not initiliazed.
Any ideas?
Thanks.
Hibernate version:
2.1.6
Mapping documents:
<hibernate-mapping>
<class
name="ar.gov.ceride.admusu.domain.Grupo"
table="grupos"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native">
</generator>
</id>
<property
name="desGrupo"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="desgrupo"
length="40"
not-null="true"
/>
<property
name="grupoId"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="grupoid"
length="8"
not-null="true"
unique="true"
/>
<set
name="usuarios"
table="grupos_usuarios"
lazy="true"
>
<key column="grupo_id"/>
<composite-element class="ar.gov.ceride.admusu.domain.GrupoUsuario">
<parent name="grupo"/>
<many-to-one name="usuario"
class="ar.gov.ceride.admusu.domain.Usuario"
column="user_id"
not-null="true"/>
<property name="ctaHab" column="ctahab" not-null="true"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class
name="ar.gov.ceride.admusu.domain.Usuario"
table="usuarios"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native">
</generator>
</id>
<property
name="apeNom"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="apeynom"
length="40"
not-null="true"
/>
<property
name="userId"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="userid"
length="8"
not-null="true"
unique="true"
/>
<set
name="grupos"
table="grupos_usuarios"
inverse="true"
lazy="true"
>
<key column="user_id"/>
<composite-element class="ar.gov.ceride.admusu.domain.GrupoUsuario">
<parent name="usuario"/>
<many-to-one name="grupo"
class="ar.gov.ceride.admusu.domain.Grupo"
column="grupo_id"
not-null="true"/>
<property name="ctaHab" column="ctahab" not-null="true"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
Grupo.java
public class Grupo extends BaseObject {
private Long id;
private String grupoId;
private String desGrupo;
private Set usuarios;
public Long getId() {
return id;
}
public String getDesGrupo() {
return desGrupo;
}
public String getGrupoId() {
return grupoId;
}
public Set getUsuarios() {
return usuarios;
}
public void setId(Long id) {
this.id = id;
}
public void setDesGrupo(String desGrupo) {
this.desGrupo = desGrupo;
}
public void setGrupoId(String grupoId) {
this.grupoId = grupoId;
}
public void setUsuarios(Set usuarios) {
this.usuarios = usuarios;
}
public String toString() {
return "[Grupo] - grupoId: " + getGrupoId() + " - desGrupo: "
+ getDesGrupo();
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Grupo)) {
return false;
}
final Grupo grupo = (Grupo) other;
if (!getGrupoId().equals(grupo.getGrupoId())) {
return false;
}
return true;
}
public int hashCode() {
return getGrupoId().hashCode();
}
}
Usuario.java
public class Usuario extends BaseObject {
private Long id;
private String userId;
private String apeNom;
private Set grupos;
public Long getId() {
return id;
}
public String getApeNom() {
return apeNom;
}
public Set getGrupos() {
return grupos;
}
public String getUserId() {
return userId;
}
public void setId(Long id) {
this.id = id;
}
public void setApeNom(String apeNom) {
this.apeNom = apeNom;
}
public void setGrupos(Set grupos) {
this.grupos = grupos;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String toString() {
return "[Usuario] - userId: " + getUserId() + " - apeNom: "
+ getApeNom();
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Usuario)) {
return false;
}
final Usuario usuario = (Usuario) other;
if (!getUserId().equals(usuario.getUserId())) {
return false;
}
return true;
}
public int hashCode() {
return getUserId().hashCode();
}
}
GrupoUsuario.java
public class GrupoUsuario extends BaseObject {
private Boolean ctaHab;
private Usuario usuario;
private Grupo grupo;
public Boolean getCtaHab() {
return ctaHab;
}
public Grupo getGrupo() {
return grupo;
}
public Usuario getUsuario() {
return usuario;
}
public void setCtaHab(Boolean ctaHab) {
this.ctaHab = ctaHab;
}
public void setGrupo(Grupo grupo) {
this.grupo = grupo;
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
public String toString() {
return "[GrupoUsuario] - grupo: " + getGrupo().toString()
+ " - usuario: " + getUsuario().toString();
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof GrupoUsuario)) {
return false;
}
final GrupoUsuario grupoUsuario = (GrupoUsuario) other;
if (!getGrupo().getGrupoId().equals(
grupoUsuario.getGrupo().getGrupoId())) {
return false;
}
if (!getUsuario().getUserId().equals(
grupoUsuario.getUsuario().getUserId())) {
return false;
}
return true;
}
public int hashCode() {
return getGrupo().hashCode() + getUsuario().hashCode();
}
}
Code between sessionFactory.openSession() and session.close():
Client
Grupo grupo = admUsuarios.loadGrupo(new Long(24));
System.out.println("Grupo: " + grupo);
SessionBean
public Grupo loadGrupo(Long id) throws Exception {
DAOFactory daoFactory = DAOFactory.getDAO();
daoFactory.getTransaction().begin(); // Init a session
try {
GrupoDAO grupoDAO = daoFactory.getGrupoDAO();
Grupo grupo = grupoDAO.loadGrupoAll(id);
daoFactory.getTransaction().commit();
return grupo;
} catch (Exception e) {
daoFactory.getTransaction().rollback();
throw e;
}
}
GrupoDao
public Grupo loadGrupoAll(Long id) {
Grupo g = (Grupo) HibernateUtil.getSession().load(Grupo.class, id);
Hibernate.initialize(g.getUsuarios());
return g;
}
Full stack trace of any exception that occurs:
java.lang.NullPointerException
at ar.gov.ceride.admusu.domain.Grupo.hashCode(Grupo.java:151)
at ar.gov.ceride.admusu.domain.GrupoUsuario.hashCode(GrupoUsuario.java:106)
at java.util.HashMap.hash(HashMap.java:261)
at java.util.HashMap.putForCreate(HashMap.java:404)
at java.util.HashMap.readObject(HashMap.java:1007)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:838)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1746)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at java.rmi.MarshalledObject.get(MarshalledObject.java:135)
at org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy.invoke(JRMPInvokerProxy.java:136)
at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:96)
at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:53)
at org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:100)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
at $Proxy1.loadGrupo(Unknown Source)
at gov.ceride.admusu.CargaDatos.main(CargaDatos.java:43)
Name and version of the database you are using:
MySql 4.0.21
The generated SQL (show_sql=true):
13:45:45,449 INFO [STDOUT] Hibernate: select grupo0_.id as id0_, grupo0_.desgrupo as desgrupo0_, grupo0_.grupoid as grupoid0_ from grupos grupo0_ where grupo0_.id=?
13:45:45,452 INFO [STDOUT] Hibernate: select usuarios0_.grupo_id as grupo_id__, usuarios0_.user_id as user_id__, usuarios0_.ctahab as ctahab__, usuario1_.id as id0_, usuario1_.apeynom as apeynom0_, usuario1_.userid as userid0_ from grupos_usuarios usuarios0_ left outer join usuarios usuario1_ on usuarios0_.user_id=usuario1_.id where usuarios0_.grupo_id=?
Debug level Hibernate log excerpt:
|