Olá pessoal estou tentando implementar o id como autoincremento na minha classe usuario só que está dando o seguinte erro:
Mapping:
<hibernate-mapping>
<class name="entidades.Usuario" table="usuario">
<id name="id" column="id">
<generator class="increment"/>
</id>
<property name="nome" column="nome"/>
<property name="sexo" column="sexo"/>
<property name="dataNasc" column="dataNasc"/>
<property name="setor" column="setor"/>
<property name="login" column="login"/>
<property name="senha" column="senha"/>
</class>
</hibernate-mapping>
Classe para testar a inclusão:
[code]
public class Teste {
private UsuarioDAO uDAO = DAOFactory.getInstance().getUsuarioDAO();
public Teste(){
testar();
}
public void testar(){
Usuario u1 = new Usuario();
u1.setNome("João");
u1.setSexo("f");
u1.setDataNasc("05/10/1985");
u1.setSetor("cap");
u1.setLogin("joao");
u1.setSenha("123456");
try{
SessionFactory session = null;
session = new Configuration()
.configure("hibernate.cfg.xml" )
.buildSessionFactory();
uDAO.save( u1 );
System.out.println("Nome: " + u1.getId());
session.close();
}catch(HibernateException e1){
e1.printStackTrace();
}
}
public static void main(String[] args){
Teste teste = new Teste();
}
}[/code]
Erro:
Compiling 1 source file to D:\Estudos_Java\TCC\build\classes
compile-single:
run-single:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.PropertyAccessException: Exception occurred inside getter of entidades.Usuario.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:148)
at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:44)
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:44)
at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:115)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:411)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:215)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1152)
at teste.Teste.testar(Teste.java:45)
at teste.Teste.<init>(Teste.java:27)
at teste.Teste.main(Teste.java:60)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
... 11 more
Caused by: java.lang.NullPointerException
at entidades.Usuario.getId(Usuario.java:40)
... 16 more
BUILD SUCCESSFUL (total time: 2 seconds)