Ola pessoal, estou começando a trabalhar com java agora e estou com dificuldade na utilização do hibernate quando tenho que fazer relacionamento de tabelas.
O que acontece é o seguinte… tenho uma tabela com o nome setor.
CREATE TABLE `setor` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT,
`setor` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
e uma tabela com o nome usuario que é relacionada com setor, portando um setor pode ter vários usuários mas um usuário só pode ser de um setor.
CREATE TABLE `usuario` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT,
`nome` varchar(20) DEFAULT NULL,
`idsetor` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ID1` (`idsetor`),
CONSTRAINT `FK_usuario` FOREIGN KEY (`idsetor`) REFERENCES `setor` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
E através do hibernate tools eu fiz a engenharia reversa e me gerou as seguintes classes:
Class Setor
[code]package teste;
// Generated 06/10/2011 17:33:07 by Hibernate Tools 3.4.0.CR1
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
-
Setor generated by hbm2java
*/
@Entity
@Table(name = “setor”, catalog = “test”)
public class Setor implements java.io.Serializable {private Byte id;
private String setor;
private Set usuarios = new HashSet(0);public Setor() {
}public Setor(String setor, Set usuarios) {
this.setor = setor;
this.usuarios = usuarios;
}@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = “id”, unique = true, nullable = false)
public Byte getId() {
return this.id;
}public void setId(Byte id) {
this.id = id;
}@Column(name = “setor”, length = 20)
public String getSetor() {
return this.setor;
}public void setSetor(String setor) {
this.setor = setor;
}@OneToMany(fetch = FetchType.LAZY, mappedBy = “setor”)
public Set getUsuarios() {
return this.usuarios;
}public void setUsuarios(Set usuarios) {
this.usuarios = usuarios;
}
}[/code]
Class Usuario
[code]package teste;
// Generated 06/10/2011 17:33:07 by Hibernate Tools 3.4.0.CR1
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
-
Usuario generated by hbm2java
*/
@Entity
@Table(name = “usuario”, catalog = “test”)
public class Usuario implements java.io.Serializable {private Byte id;
private Setor setor;
private String nome;public Usuario() {
}public Usuario(Setor setor, String nome) {
this.setor = setor;
this.nome = nome;
}@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = “id”, unique = true, nullable = false)
public Byte getId() {
return this.id;
}public void setId(Byte id) {
this.id = id;
}@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “idsetor”)
public Setor getSetor() {
return this.setor;
}public void setSetor(Setor setor) {
this.setor = setor;
}@Column(name = “nome”, length = 20)
public String getNome() {
return this.nome;
}public void setNome(String nome) {
this.nome = nome;
}
}[/code]
Mas o problema é na hora de gravar no banco, ai da o seguinte erro:
Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on teste.Usuario.setor references an unknown entity: teste.Setor
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:81)
at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:456)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:438)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:309)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at DAO.UsuarioDAO.addusuario(UsuarioDAO.java:16)
at main.executar.main(executar.java:26)
Estou com um projeto aqui na empresa pra começar daqui a 2 semanas e vou precisar de um relacionamento parecido com esse, e to desesperado não sei mais o que fazer.
Eu já estou a uma semana procurando uma solução e não encontro, por favor se algum puder me ajudar, eu agradeço.