Hibernate-atributo refencia ao construtor de outra classe [RESOLVIDO]

1 resposta
K

Coe galera… não sei se o titulo deixou claro :stuck_out_tongue:

eu tenho a seguinte situação
um atributo do meu objeto preenche o construtor de outro objeto… segue meu código:

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.url">jdbc:postgresql://localhost/ECM4</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">senha</property>
	<!-- hibernate 3 -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="show_sql">true</property>
        <!-- mapping files -->
        <mapping resource="ecm/dto/Usuario.hbm.xml"/>
        <mapping resource="ecm/dto/Permissao.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

UsuarioDTO.java

/**
 * 
 */
package ecm.dto;

import java.util.Set;

import org.hibernate.Hibernate;


/*************************************************
 * @author Sirius Sistemas Digitais
 * Arthur Stockler
 ************************************************/
public class UsuarioDTO implements IDefaultDTO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	private int hashValue = 0;
	/**
	 * Identificador do usuário
	 */
	private Integer idUsuario;
	/**
	 * Nome completo
	 */
	private String nomeCompleto;
	/**
	 * Objeto permissao
	 */
	private PermissaoDTO permissao;
	/*************************************************
	 * 
	 ************************************************/
	public UsuarioDTO() {
	}
	/*************************************************
	 * @return the idUsuario
	 ************************************************/
	public Integer getIdUsuario() {
		return idUsuario;
	}
	/*************************************************
	 * @param p_idUsuario the idUsuario to set
	 ************************************************/
	public void setIdUsuario(Integer p_idUsuario) {
		idUsuario = p_idUsuario;
	}
	/*************************************************
	 * @return the nomeCompleto
	 ************************************************/
	public String getNomeCompleto() {
		return nomeCompleto;
	}
	/*************************************************
	 * @param p_nomeCompleto the nomeCompleto to set
	 ************************************************/
	public void setNomeCompleto(String p_nomeCompleto) {
		nomeCompleto = p_nomeCompleto;
	}
	/*************************************************
	 * @return the permissao
	 ************************************************/
	public PermissaoDTO getPermissao() {
		return permissao;
	}
	/*************************************************
	 * @param p_permissao the permissao to set
	 ************************************************/
	public void setPermissao(PermissaoDTO p_permissao) {
		permissao = p_permissao;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object p_object)
	{
		if (p_object == null)
			return false;
		if (! (p_object instanceof PastaDTO))
			return false;
		UsuarioDTO that = (UsuarioDTO)p_object;
		if (this.getIdUsuario() != null && that.getIdUsuario() != null)
			if (! this.getIdUsuario().equals(that.getIdUsuario()))
				return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode()
	{
		if (this.hashValue == 0)
		{
			int result = 17;
			int idValue = this.getIdUsuario() == null ? 0 : this.getIdUsuario().hashCode();
			result = result * 37 + idValue;
			this.hashValue = result;
		}
		return this.hashValue;
	} 
}

Usuario.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="ecm.dto">
	<class name="UsuarioDTO" schema="public" table="usuarios">
        <id name="idUsuario" column="id" type="java.lang.Integer">
            <generator class="sequence">
            	<param name="sequence">sirius.usuarios_id_seq</param>
            </generator>
        </id>
        <property name="nomeCompleto" column="nomecompleto" type="java.lang.String"  not-null="true" />
        <!-- PERMISSAO -->
        <many-to-one name="permissao" column="permissao" class="PermissaoDTO"/>
    </class>
    
</hibernate-mapping>

PermissaoDTO.java

/**
 * 
 */
package ecm.dto;



/*************************************************
 * @author Sirius Sistemas Digitais
 * Arthur Stockler
 ************************************************/
public class PermissaoDTO implements IDefaultDTO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	private int hashValue = 0;
	
	public static enum EnumPermissao {
		/**
		 * Insere setor no qual o usuário irá também pertencer
		 */
		SETOR_INSERIR(0x01),
		/**
		 * Troca descrição do setor
		 */
		SETOR_EDITAR(0x02),

		EMAIL_ENVIAR(0x8000);
		
		private final int permissao; // note que uma enum não deve mudar de valor!
		// portanto não deve haver um método "setValor".
		public int getPermissao() { return permissao; }
		EnumPermissao (int p_permissao) {
			this.permissao = p_permissao;
		}
		
	}
	
	private boolean setorInserir;
	private boolean setorEditar;
	private boolean emailEnviar;
	private Integer permissao;
	private Integer id;
	
	/*************************************************
	 * 
	 ************************************************/
	public PermissaoDTO(Integer p_permissao) {
                this.permissao = p_permissao;
		if ( ( permissao & EnumPermissao.SETOR_INSERIR.getPermissao()) != 0)
			this.setorInserir = true;
		if ( ( permissao & EnumPermissao.SETOR_EDITAR.getPermissao()) != 0)
			this.setorEditar = true;
		if ( ( permissao & EnumPermissao.EMAIL_ENVIAR.getPermissao()) != 0)
			this.emailEnviar = true;
		
	}
	/*************************************************
	 * @return the id
	 ************************************************/
	public Integer getId() {
		return id;
	}
	/*************************************************
	 * @param p_id the id to set
	 ************************************************/
	public void setId(Integer p_id) {
		id = p_id;
	}
	/*************************************************
	 * @return the emailEnviar
	 ************************************************/
	public boolean isEmailEnviar() {
		return emailEnviar;
	}
	/*************************************************
	 * @param p_emailEnviar the emailEnviar to set
	 ************************************************/
	public void setEmailEnviar(boolean p_emailEnviar) {
		emailEnviar = p_emailEnviar;
	}
	/*************************************************
	 * @return the permissao
	 ************************************************/
	public Integer getPermissao() {
		return permissao;
	}
	/*************************************************
	 * @param p_permissao the permissao to set
	 ************************************************/
	public void setPermissao(Integer p_permissao) {
		permissao = p_permissao;
	}
	/*************************************************
	 * @return the setorEditar
	 ************************************************/
	public boolean isSetorEditar() {
		return setorEditar;
	}
	/*************************************************
	 * @param p_setorEditar the setorEditar to set
	 ************************************************/
	public void setSetorEditar(boolean p_setorEditar) {
		setorEditar = p_setorEditar;
	}
	/*************************************************
	 * @return the setorInserir
	 ************************************************/
	public boolean isSetorInserir() {
		return setorInserir;
	}
	/*************************************************
	 * @param p_setorInserir the setorInserir to set
	 ************************************************/
	public void setSetorInserir(boolean p_setorInserir) {
		setorInserir = p_setorInserir;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object p_object)
	{
		if (p_object == null)
			return false;
		if (! (p_object instanceof PermissaoDTO))
			return false;
		PermissaoDTO that = (PermissaoDTO)p_object;
		if (this.getId() != null && that.getId() != null)
			if (! this.getId().equals(that.getId()))
				return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode()
	{
		if (this.hashValue == 0)
		{
			int result = 17;
			int idValue = this.getId() == null ? 0 : this.getId().hashCode();
			result = result * 37 + idValue;
			this.hashValue = result;
		}
		return this.hashValue;
	}
}

Permissao.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="ecm.dto">

	<class name="PermissaoDTO">
 		<id name="id" type="java.lang.Integer">
        	     <generator class="increment"/>
     	        </id>
        <property name="setorInserir" type="java.lang.Boolean"  not-null="true" />
        <property name="setorEditar" type="java.lang.Boolean"  not-null="true" />
        <property name="emailEnviar" type="java.lang.Boolean"  not-null="true" />
        <property name="permissao" type="java.lang.Integer"  not-null="true" />
    </class>
</hibernate-mapping>

Acho que meu Permissao.hbm.xml está errado… minha idéia é… quando carregar o usuário tem um campo na tabela que guarda um inteiro… esse inteiro o hibernate deve jogar no construtor do objeto PermissaoDTO, preenchendo o atributo do usuário com o objeto PermissaoDTO.

Agradeço a ajuda de todos

Abraços

1 Resposta

K

resolvido…
como minha classe PermissaoDTO nao tem nenhuma ligacao com o BD nao tem pq mapear.
ficou assim
UsuarioDTO.java

/**
 * 
 */
package ecm.dto;

import java.util.Set;

import org.hibernate.Hibernate;


/*************************************************
 * @author Sirius Sistemas Digitais
 * Arthur Stockler
 ************************************************/
public class UsuarioDTO implements IDefaultDTO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	private int hashValue = 0;
	/**
	 * Identificador do usuário
	 */
	private Integer idUsuario;
	/**
	 * Nome completo
	 */
	private String nomeCompleto;
	/**
	 * Objeto permissao
	 */
	private PermissaoDTO permissaoDTO;
	/**
	 * Inteiro permissao
	 */
        private Integer permissao;
	/*************************************************
	 * 
	 ************************************************/
	public UsuarioDTO() {
	}
	/*************************************************
	 * @return the idUsuario
	 ************************************************/
	public Integer getIdUsuario() {
		return idUsuario;
	}
	/*************************************************
	 * @param p_idUsuario the idUsuario to set
	 ************************************************/
	public void setIdUsuario(Integer p_idUsuario) {
		idUsuario = p_idUsuario;
	}
	/*************************************************
	 * @return the nomeCompleto
	 ************************************************/
	public String getNomeCompleto() {
		return nomeCompleto;
	}
	/*************************************************
	 * @param p_nomeCompleto the nomeCompleto to set
	 ************************************************/
	public void setNomeCompleto(String p_nomeCompleto) {
		nomeCompleto = p_nomeCompleto;
	}
	/*************************************************
	 * @return the permissaoDTO
	 ************************************************/
	public PermissaoDTO getPermissaoDTO() {
		return permissaoDTO;
	}
	/*************************************************
	 * @return the permissao
	 ************************************************/
	public Integer getPermissao() {
		return permissao;
	}
	/*************************************************
	 * @param p_permissao the permissao to set
	 ************************************************/
	public void setPermissao(Integer p_permissao) {
		permissaoDTO = new PermissaoDTO(p_permissao);
		permissao = p_permissao;
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object p_object)
	{
		if (p_object == null)
			return false;
		if (! (p_object instanceof PastaDTO))
			return false;
		UsuarioDTO that = (UsuarioDTO)p_object;
		if (this.getIdUsuario() != null && that.getIdUsuario() != null)
			if (! this.getIdUsuario().equals(that.getIdUsuario()))
				return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode()
	{
		if (this.hashValue == 0)
		{
			int result = 17;
			int idValue = this.getIdUsuario() == null ? 0 : this.getIdUsuario().hashCode();
			result = result * 37 + idValue;
			this.hashValue = result;
		}
		return this.hashValue;
	} 
}

coloquei dentro do meu setPermissao da classe usuario um new PermissaoDTO(this.permissao)

vlw!!

Criado 2 de janeiro de 2008
Ultima resposta 4 de jan. de 2008
Respostas 1
Participantes 1