Hello everybody, good afternoon. I'm having a little trouble in configuring an application with Java 5 + Spring + Hibernate. My hibernate versions are:
Hibernate version: 3.2
Mapping documents: I don't like XML configuration very much, so I'm using hibernate annotations, version 3.2
I'm trying to make a system, were it's first part is a user login. Everything seens well in the view and controller (I pass through my Validation class with no problem). My bean it's like this:
Code:
package br.com.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
@Entity
@Table (name="usuario")
public class Usuario {
private Integer id;
private String login;
private String email;
private char sexo;
private String senha;
/* ------------------ Constructors -------------------- */
public Usuario(){}
public Usuario(String login, String senha){
this.login = login;
this.senha = senha;
}
/* ---------------- Getters e Setters ---------------- */
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return id;
}
@Column
@NotNull
public String getLogin() {
return login;
}
...
I'm using a mySQL 5 database, and there I have a table called "usuario" (low case) with one register already there (to test validation code), which I want the bean above to map.
I've created the hibernate configuration file just like this:
Code:
<!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="hibernate.connection.url">
jdbc:mysql://localhost:3306/my_database
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pass</property>
<property name="hibernate.connection.pool_size">0</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<property name="hibernate.show_sql">false</property>
<!-- "Import" the mapping resources here -->
<mapping class="br.com.beans.Usuario"/>
</session-factory>
</hibernate-configuration>
I've a DAO class, which extends
HibernateDaoSupport, and I instanciate it like this:
Code:
public UsuarioDAO(){
factory = new AnnotationConfiguration()
.configure()
.buildSessionFactory();
}
Where "factory" its a private SessionFactory variable. I'm also using Jboss 4.0.5, and in its logs I see the following:
Code:
...
INFO [Version] Hibernate Annotations 3.2.0.GA
INFO [Configuration] configuring from resource: /hibernate.cfg.xml
INFO [Configuration] Configuration resource: /hibernate.cfg.xml
[Configuration] Configured SessionFactory: null
[AnnotationBinder] Binding entity from annotated class: br.com.beans.Usuario
INFO [EntityBinder] Bind entity br.com.beans.Usuario on table usuario
...
Which made me think everything was ok with the mapping part (for now I've only that bean). However, when I try some code to interact with the database, for example, retrieve a list, with this code
Code:
List result = session.createQuery("from "+Usuario.class.getName()).list();
I get the following warn on my jboss log:
Code:
WARN [QuerySplitter] no persistent classes found for query class: from br.com.beans.Usuario
To test, I also created an insert code:
Code:
Usuario teste = new Usuario("teste", "teste");
teste.setEmail("teste@teste.com");
teste.setSexo('M');
getHibernateTemplate().save(teste);
Which gives the following error:
Code:
ERROR [[locadora]] Servlet.service() for servlet locadora threw exception
org.hibernate.MappingException: Unknown entity: br.com.beans.Usuario
And I have no clue up to now were I'm I doing wrong. I tried also to change DAO initialization to:
Code:
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure();
cfg.addAnnotatedClass(Usuario.class);
factory = cfg.buildSessionFactory();
Just to see what'd happen, but nothin changed, still got the same erros. Could anyone help me to find what is causing the error?
Thanks for the attention, and I'm sorry if I made any english writing mistake, I need some more praticing (portuguese is my natural language ^^)