Problema/Dúvida com Annotations

6 respostas
tiago_

Pessoal

Estou tentando mapear 2 tabelas (usuario e setor) mas estou com problemas na parte de ManyToOne, OneToOne, OneToMany…
Eu entendi como funciona, mas na hora de aplicar estou tendo duvidas (pelo exemplo do livro). Eu tentei mapear mas qnd
eu dou start no tomcat ele da erro… Gostaria q vcs dessem uma olhada :slight_smile:
Se puderem me explicar o pq ta errado eu agradeco :smiley:

Banco: http://postimage.org/image/4fmic5t0v/

Mapeamento das 2 classes:

package requisicao.usuario;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import requisicao.setor.*;


@Entity
@Table(name="usuario")

public class Usuario implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6755569886340789158L;
	
	@Id
	@GeneratedValue
	@Column(name="codigo")
	private Integer codigo;
	
	@ManyToOne //N sei se isso ta  ta certo, acho q errado
	@JoinColumn(name="codigoSetor")
	private Setor setor;
	
	@Column(name="nome", length=45, nullable= true)
	private String nome;
	
    @Column(name="matricula", length=45, nullable= true)
	private String matricula;
    
    @Column(name="login", length=45, nullable= true)
	private String login;
    
    @Column(name="senha", length=45, nullable= true)
	private String senha;
    
    @Column(name="codigoSetor", nullable= true)
	private  Integer codigoSetor;
    
    @Column(name="ativo")
	private boolean ativo;
	
	
	
	public Integer getCodigo() {
		return codigo;
	}
	public void setCodigo(Integer codigo) {
		this.codigo = codigo;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	public String getMatricula() {
		return matricula;
	}
	public void setMatricula(String matricula) {
		this.matricula = matricula;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getSenha() {
		return senha;
	}
	public void setSenha(String senha) {
		this.senha = senha;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;	
	}
	public boolean isAtivo() {
		return ativo;
	}
	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}
	
	public Integer getCodigoSetor() {
		return codigoSetor;
	}
	public void setCodigoSetor(Integer codigoSetor) {
		this.codigoSetor = codigoSetor;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (ativo ? 1231 : 1237);
		result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
		result = prime * result
				+ ((codigoSetor == null) ? 0 : codigoSetor.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result
				+ ((matricula == null) ? 0 : matricula.hashCode());
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		result = prime * result + ((senha == null) ? 0 : senha.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Usuario other = (Usuario) obj;
		if (ativo != other.ativo)
			return false;
		if (codigo == null) {
			if (other.codigo != null)
				return false;
		} else if (!codigo.equals(other.codigo))
			return false;
		if (codigoSetor == null) {
			if (other.codigoSetor != null)
				return false;
		} else if (!codigoSetor.equals(other.codigoSetor))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (matricula == null) {
			if (other.matricula != null)
				return false;
		} else if (!matricula.equals(other.matricula))
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		if (senha == null) {
			if (other.senha != null)
				return false;
		} else if (!senha.equals(other.senha))
			return false;
		return true;
	}
	
	
	
	
	

}
package requisicao.setor;

import java.io.Serializable;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="setor")
public class Setor implements Serializable{

	private static final long serialVersionUID = -3280733486545529194L;
	
	@Column(name="codigo")
	private int codigo;
	
	@Column(name="nome", length=65, nullable= true)
	private String nome;

	public int getCodigo() {
		return codigo;
	}

	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + codigo;
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Setor other = (Setor) obj;
		if (codigo != other.codigo)
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		return true;
	}
	
		

}

6 Respostas

Hebert_Coelho

Qual o erro?

tiago_

Criação inicial do objeto SessionFactory falhou. Erro: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

Vc olhou o codigo e o banco ?
Mesmo sem dar erro eu n tinha certeza se tinha feito certo…

Hebert_Coelho

tiago__:
Criação inicial do objeto SessionFactory falhou. Erro: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

Vc olhou o codigo e o banco ?
Mesmo sem dar erro eu n tinha certeza se tinha feito certo…

Não. Não tem lógica olhar código antes de erro.
@OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

Está falando que Setor não é uma Entity válida.

E realmente não. Uma classe para ser Entity precisa ter a anotação @Entity, um construtor default publico Setor() e um ID declaro. @ID.

Creio que alguma coisa está faltando lá.

tiago_

Hebert Coelho:
tiago__:
Criação inicial do objeto SessionFactory falhou. Erro: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

Vc olhou o codigo e o banco ?
Mesmo sem dar erro eu n tinha certeza se tinha feito certo…

Não. Não tem lógica olhar código antes de erro.
@OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

Está falando que Setor não é uma Entity válida.

E realmente não. Uma classe para ser Entity precisa ter a anotação @Entity, um construtor default publico Setor() e um ID declaro. @ID.

Creio que alguma coisa está faltando lá.

Entao tava falando o @Id e @GeneratedValue… Mas continua dando o erro: Criação inicial do objeto SessionFactory falhou. Erro: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

N sei se a parte ManyToOne/JoinColumn ta correta…

-Um usuario so pode ter um setor
-Um setor pode ter varios usuarios

Segui mais ou menos a linha de raciocinio q ta livro p colcar o @OneToOne ou @ManyToOne, mas acho q n esta correto…

Hebert_Coelho

Veja c ñ tem outros erros. Isso q eu te mostrei é erro e você não mostrou. ^^

tiago_

tiago__:
Hebert Coelho:
tiago__:
Criação inicial do objeto SessionFactory falhou. Erro: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

Vc olhou o codigo e o banco ?
Mesmo sem dar erro eu n tinha certeza se tinha feito certo…

Não. Não tem lógica olhar código antes de erro.
@OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

Está falando que Setor não é uma Entity válida.

E realmente não. Uma classe para ser Entity precisa ter a anotação @Entity, um construtor default publico Setor() e um ID declaro. @ID.

Creio que alguma coisa está faltando lá.

Entao tava falando o @Id e @GeneratedValue… Mas continua dando o erro: Criação inicial do objeto SessionFactory falhou. Erro: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on requisicao.usuario.Usuario.setor references an unknown entity: requisicao.setor.Setor

N sei se a parte ManyToOne/JoinColumn ta correta…

-Um usuario so pode ter um setor
-Um setor pode ter varios usuarios

Segui mais ou menos a linha de raciocinio q ta livro p colcar o @OneToOne ou @ManyToOne, mas acho q n esta correto…

Mudei as duas classes… A classe setor nao tem FK ! Entao coloquei o JoinColumn e ManyToOne na classe Setor… N deu erro no tomcat, mas ta certo o q eu fiz ?

Gostaria q vc olhasse o banco e as classes p me dizer. Banco: http://postimage.org/image/4fmic5t0v/

Classes:

package requisicao.usuario;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import requisicao.setor.*;


@Entity
@Table(name="usuario")

public class Usuario implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6755569886340789158L;
	
	@Id
	@GeneratedValue
	@Column(name="codigo")
	private Integer codigo;
	
	@Column(name="nome", length=45, nullable= true)
	private String nome;
	
    @Column(name="matricula", length=45, nullable= true)
	private String matricula;
    
    @Column(name="login", length=45, nullable= true)
	private String login;
    
    @Column(name="senha", length=45, nullable= true)
	private String senha;
    
    @Column(name="codigoSetor", nullable= true)
	private  Integer codigoSetor;
    
    @Column(name="ativo")
	private boolean ativo;
	
	
	
	public Integer getCodigo() {
		return codigo;
	}
	public void setCodigo(Integer codigo) {
		this.codigo = codigo;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	public String getMatricula() {
		return matricula;
	}
	public void setMatricula(String matricula) {
		this.matricula = matricula;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getSenha() {
		return senha;
	}
	public void setSenha(String senha) {
		this.senha = senha;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;	
	}
	public boolean isAtivo() {
		return ativo;
	}
	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}
	
	public Integer getCodigoSetor() {
		return codigoSetor;
	}
	public void setCodigoSetor(Integer codigoSetor) {
		this.codigoSetor = codigoSetor;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (ativo ? 1231 : 1237);
		result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
		result = prime * result
				+ ((codigoSetor == null) ? 0 : codigoSetor.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result
				+ ((matricula == null) ? 0 : matricula.hashCode());
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		result = prime * result + ((senha == null) ? 0 : senha.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Usuario other = (Usuario) obj;
		if (ativo != other.ativo)
			return false;
		if (codigo == null) {
			if (other.codigo != null)
				return false;
		} else if (!codigo.equals(other.codigo))
			return false;
		if (codigoSetor == null) {
			if (other.codigoSetor != null)
				return false;
		} else if (!codigoSetor.equals(other.codigoSetor))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (matricula == null) {
			if (other.matricula != null)
				return false;
		} else if (!matricula.equals(other.matricula))
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		if (senha == null) {
			if (other.senha != null)
				return false;
		} else if (!senha.equals(other.senha))
			return false;
		return true;
	}
	
	
	
	
	

}
package requisicao.setor;

import java.io.Serializable;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import requisicao.usuario.Usuario;

@Entity
@Table(name="setor")
public class Setor implements Serializable{

	private static final long serialVersionUID = -3280733486545529194L;
	
	@Id
	@GeneratedValue
	@Column(name="codigo")
	private int codigo;
	
	@ManyToOne
	@JoinColumn(name="codigoSetor")
	private Usuario usuario;
	
	@Column(name="nome", length=65, nullable= true)
	private String nome;

	public int getCodigo() {
		return codigo;
	}

	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + codigo;
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Setor other = (Setor) obj;
		if (codigo != other.codigo)
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		return true;
	}
	
	
	
	
	
	
	
	

	
	

}
Criado 28 de setembro de 2012
Ultima resposta 28 de set. de 2012
Respostas 6
Participantes 2