Ver Mensaje Individual
  #3 (permalink)  
Antiguo 09/09/2009, 04:47
Pelirr
 
Fecha de Ingreso: diciembre-2008
Mensajes: 233
Antigüedad: 16 años, 3 meses
Puntos: 1
Respuesta: error con LazyInitializationException.java

Hola, escribo el código que se supone da error (linea 110 de Empresa.java, donde pone más abajo "tipoempresa.getEmpresas().add(this);"):


package es.aena.sgma.controlresiduos.data.entity;

import javax.persistence.*;

import org.apache.log4j.Logger;

import es.aena.sgcomun.base.data.entity.TraceableEntity;
import es.aena.sgcomun.base.service.StaticServiceLocator;
import es.aena.sgcomun.base.service.impl.SpringStaticWebC onfiguredServiceLocator;
import es.aena.sgma.controlresiduos.data.dao.TipoEmpresaD ao;

@Entity
@Table (name = "D_EMPRESAS")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "D_TIPOS_EMPRESA_NOMBRE")
public class Empresa extends TraceableEntity {

public static final String EMPRESA_GESTION = "GESTION";
public static final String EMPRESA_ENTREGA = "ENTREGA";
public static final String EMPRESA_TRANSPORTE = "TRANSPORTE";

protected static TipoEmpresa TIPO_EMPRESA_GESTION = null;
protected static TipoEmpresa TIPO_EMPRESA_ENTREGA = null;
protected static TipoEmpresa TIPO_EMPRESA_TRANSPORTE = null;

private transient Logger logger = Logger.getLogger(this.getClass());

@Id
@Column (name = "ID")
@GeneratedValue (generator = "SeqDEmpresas")
@SequenceGenerator (name= "SeqDEmpresas", sequenceName ="SEQ_D_EMPRESAS", allocationSize = 1)
private Long id;

@Column (name = "NOMBRE")
private String nombre;

@Column (name = "DESCRIPCION")
private String descripcion;

@Column (name = "CIF")
private String cif;

@ManyToOne (targetEntity = TipoEmpresa.class)
@JoinColumn (name = "D_TIPOS_EMPRESA_NOMBRE", nullable = true)
private TipoEmpresa tipoempresa;

public String getNombre() {
return nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

public String getDescripcion() {
return descripcion;
}

public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}

public String getCif() {
return cif;
}

public void setCif(String cif) {
this.cif = cif;
}

public Long getId() {
return id;
}

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

public Empresa() {
super();
try {
StaticServiceLocator serviceLocator = SpringStaticWebConfiguredServiceLocator.getDefault ServiceLocator();
if (serviceLocator == null) {
this.logger.warn("No hay ServiceLocator disponible, no se pueden inicializar los tipos de empresa");
}
else {
TipoEmpresaDao tipoEmpresaDao = (TipoEmpresaDao) serviceLocator.getBean("tipoEmpresaDao");
TIPO_EMPRESA_GESTION = tipoEmpresaDao.loadByPrimaryKey(EMPRESA_GESTION);
TIPO_EMPRESA_ENTREGA = tipoEmpresaDao.loadByPrimaryKey(EMPRESA_ENTREGA);
TIPO_EMPRESA_TRANSPORTE = tipoEmpresaDao.loadByPrimaryKey(EMPRESA_TRANSPORTE );
}
} catch (Exception e) {
throw new RuntimeException("Error Grave al inicializar entidad \"Empresa\"" + e.getMessage());
}
//this.tipoempresa.setTipo(TIPO_EMPRESA_TRANSPORTE);
//this.tipoempresa = TIPO_EMPRESA_TRANSPORTE;
}

public TipoEmpresa getTipoempresa() {
return tipoempresa;
}

public void setTipoempresa(TipoEmpresa tipoempresa) {
if (this.tipoempresa != null)
{
this.tipoempresa.getEmpresas().remove(this);
}
if (tipoempresa != null)
{
tipoempresa.getEmpresas().add(this);
}
this.tipoempresa = tipoempresa;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Empresa) {
Empresa objeto = (Empresa) obj;
return (this.id != null) ? this.id.equals(objeto.getId()) : false;
}
else {
return false;
}
}

@Override
public int hashCode() {
if (this.id != null) {
return this.id.hashCode();
}
else {
return 0;
}
}
}

por lo poco que entiendo, no permite hacer ese add. Lo que pretendo es: tengo una entidad (TipoEmpresa.java) que tiene un campo nombre, que puede adoptar varios valores. Tengo otra entidad (Empresa.java) que tira de la anterior, mediante una relación ManyToOne, y la cual, según el valor del campo nombre proveniente de TipoEmpresa, me redireccione a otras entidades diferentes (a saber, EmpresaGestion.java, EmpresaTransporte.java y EmpresaEntrega.java). El código de la entidad TipoEmpresa.java es:

package es.aena.sgma.controlresiduos.data.entity;

import java.util.*;
import javax.persistence.*;

import es.aena.sgcomun.base.data.entity.TraceableEntity;

@Entity
@Table ( name = "D_TIPOS_EMPRESA")
public class TipoEmpresa extends TraceableEntity{

@Id
@Column(name = "NOMBRE", unique = true, nullable = false)
private String nombre;

@Column (name = "DESCRIPCION")
private String descripcion;

@OneToMany(mappedBy = "tipoempresa")
private Set<Empresa> empresas = new HashSet <Empresa>();

public void setEmpresas(Set<Empresa> empresas){
this.empresas = empresas;
}

public Set<Empresa> getEmpresas() {
return empresas;
}

public String getNombre() {
return nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

public String getDescripcion() {
return descripcion;
}

public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}

public void addEmpresa(Empresa empresa){
if (empresa == null){
throw new IllegalArgumentException("Empresa null");
}
empresa.setTipoempresa(this);
}

public void removeEmpresa(Empresa empresa){
if (empresa == null){
throw new IllegalArgumentException("Empresa null");
}
empresa.setTipoempresa(null);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof TipoEmpresa) {
TipoEmpresa objeto = (TipoEmpresa) obj;
return (this.nombre != null) ? this.nombre.equals(objeto.getNombre()) : false;
}
else {
return false;
}
}

@Override
public int hashCode() {
if (this.nombre != null) {
return this.nombre.hashCode();
}
else {
return 0;
}
}
}

Espero que puedas echarme un cable, y gracias por la rapidez. Un saludo