Pessoal, comecei há alguns dias estudar o JEE, estou usando para as atividades práticas a apostila da Caelum, ela tem vários exemplos, é fácil de entender, mas talvez por ignorância minha tenho encontrado muuuitos problemas em algumas partes, pois faltam informações (isso até é bom pois nos força a quebrar a cabeça para entender, apesar de ser cansativo…)
Nunca trabalhei com annotations e estou vendo isso agora no capítul sobre o Hibernate.
Na página 94, tem um exemplo de uma classe Produto
private Long id;
private String nome;
private String descricao;
private double preco;
@Id @GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Column(name= "descri", nullable = true, length = 50)
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public double getPreco() {
return preco;
}
public void setPreco(double preco) {
this.preco = preco;
}
}[/code]
As annotations Entity e Column eu achei, mas a Id e a GeneratedValue não. Procurei em todos os pacotes.
Importei para o netbeans apenas o hibernate*.jar e o hibernate-annotations.jar. Está faltando algo?
Ja voloquei o arquivo de properties no pacote que está a minha classe tbm e não fuincinou. Olha o erro
Exception in thread "main" org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
at org.hibernate.dialect.Dialect.getDialect(Dialect.java:610)
at org.hibernate.dialect.Dialect.getDialect(Dialect.java:632)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:84)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:59)
at testehibernate.GeraTabelas.create(GeraTabelas.java:23)
at testehibernate.GeraTabelas.main(GeraTabelas.java:32)
Então cara para vc importar estas annotations vc precisa colocar no seu projeto o jar do ejb3-persistence.jar, pois é dela q vc deve importar o @id e o @Entity
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2073)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1298)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
at teste.main(teste.java:12)
o q pode ser? como trabalhar com o hibernate.properties?
[quote=davidbuzatto]é que o magnífico tutorial que estou seguindo usa o arquivo hibernate.properties ao invés do arquivo xml de configuração.
Valeu![/quote]
David, o tutorial que voce esta seguindo esta certo. Tanto faz voce usar o xml ou o properties no hibernate, voce que esta colocando no lugar errado o arquivo. Sempre coloque no path do seu projeto.
Se quer um material mais mastigado, que ate mesmo os imports aparecem em grande parte do codigo, use o material do FJ-28. Mas creio que é melhor mesmo voce bater cabeca do que so copiar e colar.