joca_java 18 de fev. de 2009
Cara… a classe Perfil tá mapeada?
diogoprosoft 18 de fev. de 2009
Desde ja agradeco a atencao(y)
Vou postar meu codigo para vc dar uma olhada
Perfil.class
@Entity
@Table ( name = "perfil" )
public class Perfil implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L ;
@Id
@GeneratedValue ( strategy = GenerationType . SEQUENCE )
@Column ( name = "id_perfil" , unique = true )
private Integer idPerfil ;
@Column ( name = "nmPerfil" , unique = true , length = 30 )
private String nmPerfil ;
gets e sets
Pessoa.class
@Entity
@Table ( name = "pessoa" )
public class Pessoa implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L ;
@Id
@GeneratedValue ( strategy = GenerationType . SEQUENCE )
@Column ( name = "id_pessoa" , unique = true )
private Integer idPessoa ;
@Column ( name = "nm_pessoa" , unique = true , length = 50 )
private String nmPessoa ;
@Column ( name = "rg_ie" , unique = true , length = 11 )
private String rgIe ;
@Column ( name = "cpf_cnpj" , unique = true , length = 18 )
private String cpfCnpj ;
@ManyToMany ( fetch = FetchType . LAZY , mappedBy = "Perfil" , targetEntity = Perfil . class )
@JoinTable ( name = "perfil" , joinColumns = { @JoinColumn ( name = "id_perfil" )})
public Set < Perfil > perfil = new HashSet < Perfil > ();
gets e sets
HibernateFactory.class
public class HibernateFactory {
private static SessionFactory factory ;
static {
AnnotationConfiguration cfg = new AnnotationConfiguration ();
cfg . addAnnotatedClass ( Pessoa . class );
cfg . addAnnotatedClass ( Perfil . class );
factory = cfg . buildSessionFactory ();
}
public static Session getSession () {
return factory . openSession ();
}
}
public class GeraTabelas {
private static void create ( AnnotationConfiguration cfg ) {
new SchemaExport ( cfg ). create ( true , true );
}
public static void main ( String [] args ){
AnnotationConfiguration cfg = new AnnotationConfiguration ();
try {
create ( cfg . addAnnotatedClass ( Pessoa . class ));
create ( cfg . addAnnotatedClass ( Perfil . class ));
}
catch ( Exception e ){
e . printStackTrace ();
}
}
}
rodrigoallemand 18 de fev. de 2009
Faz o básico.... O exemplo abaixo eu tirei de um sistema meu aqui... altere para a sua necessidade...
@ Entity
@ Table ( name = "CAU_USUARIO" )
public class Usuario implements Serializable {
private static final long serialVersionUID = 4226469181963895540 L ;
@ Id
@ Column ( name = "ID_USUARIO" )
private Long id ;
//... Outros Atributos
@ ManyToMany ( mappedBy = "usuarios" )
private Set < Perfil > perfis ;
//... acessores
}
@ Entity
@ Table ( name = "CAU_PERFIL" )
public class Perfil implements Serializable {
private static final long serialVersionUID = - 7100910186111699361 L ;
@ Id
@ Column ( name = "ID_PERFIL" )
private Long id ;
//... Outros Atributos
@ ManyToMany
@ JoinTable ( name = "CAU_USUARIO_PERFIL" ,
joinColumns = @ JoinColumn ( name = "ID_PERFIL" ),
inverseJoinColumns = @ JoinColumn ( name = "ID_USUARIO" ))
private Set < Usuario > usuarios ;
//... acessores
}
diogoprosoft 18 de fev. de 2009
Fiz o que vc falou porem ainda continua dando erro
Pessoa.class
@ManyToMany ( mappedBy = "pessoa" )
private Set < Perfil > perfil ;
Perfil.class
@ManyToMany
@JoinTable ( name = "pessoa_has_perfil" , joinColumns = @JoinColumn ( name = "id_perfil" ), inverseJoinColumns = @JoinColumn ( name = "id_pessoa" ))
private Set < Pessoa > pessoa ;
Erro
Use of @ OneToMany or @ ManyToMany targeting an unmapped class : com . corp . bean . Pessoa . perfil [ com . corp . bean . Perfil ]
at org . hibernate . cfg . annotations . CollectionBinder . bindManyToManySecondPass ( CollectionBinder . java : 1068 )
at org . hibernate . cfg . annotations . CollectionBinder . bindStarToManySecondPass ( CollectionBinder . java : 600 )
at org . hibernate . cfg . annotations . CollectionBinder $ 1. secondPass ( CollectionBinder . java : 541 )
at org . hibernate . cfg . CollectionSecondPass . doSecondPass ( CollectionSecondPass . java : 43 )
at org . hibernate . cfg . Configuration . secondPassCompile ( Configuration . java : 1136 )
at org . hibernate . cfg . AnnotationConfiguration . secondPassCompile ( AnnotationConfiguration . java : 324 )
at org . hibernate . cfg . Configuration . generateDropSchemaScript ( Configuration . java : 762 )
at org . hibernate . tool . hbm2ddl . SchemaExport .< init > ( SchemaExport . java : 93 )
at org . hibernate . tool . hbm2ddl . SchemaExport .< init > ( SchemaExport . java : 61 )
at testes . GeraTabelas . create ( GeraTabelas . java : 11 )
at testes . GeraTabelas . main ( GeraTabelas . java : 18 )