-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Anfaenger mit NullPointer
PostPosted: Sun Nov 05, 2006 1:58 pm 
Newbie

Joined: Sun Nov 05, 2006 1:41 pm
Posts: 4
Hallo,

Ich hoffe jemand kann mir weiterhelfen. Ich hoffe das es ein einfach zu loesendes Problem ist .....

Bei Ausfuehrung des aufgefuehrten Codes tritt eine NullPointer Exception auf...

Rahmenbedingungen:

Versionen
HibernateCore = 3.2
HAnno = 3.2.0
HEManager = 3.2.0

DB: HSQL

Keine XML Konfigurationsdateien !

Code:
Code:
public static void main(String[] args)
  {
    Ejb3Configuration configuration = new Ejb3Configuration();
    configuration.setProperty("hibernate.dialect","org.hibernate.dialect.HSQLDialect");
    configuration.setProperty("hibernate.connection.driver_class","org.hsqldb.jdbcDriver");
    configuration.setProperty("hibernate.connection.url","jdbc:hsqldb:C:/temp/testData");
    configuration.setProperty("hibernate.connection.username","sa");
    configuration.setProperty("hibernate.connection.password","");
   
    configuration.addAnnotatedClass(AddressImpl.class);

    SchemaExport schemaExport = new SchemaExport(configuration.getHibernateConfiguration());
    schemaExport.setOutputFile("c://temp//sql.txt");
    schemaExport.create(true,true);

    EntityManagerFactory entityManagerFactory = configuration.buildEntityManagerFactory();
    EntityManager entityManager = entityManagerFactory.createEntityManager();
   
    EntityTransaction transaction = entityManager.getTransaction();

    transaction.begin();
    AddressImpl address = new AddressImpl();       
    entityManager.persist(address); // HIER KOMMT DIE NULLPOINTER
    transaction.commit();
    entityManager.close();
    entityManagerFactory.close();

    schemaExport.drop(true,true);
  }


Problem:
Code:
2006.11.05 18:37:54,601 INFO  [main] - org.hibernate.ejb.Version - Hibernate EntityManager 3.2.0.GA
2006.11.05 18:37:54,691 INFO  [main] - org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.2.0.GA
2006.11.05 18:37:54,781 INFO  [main] - org.hibernate.cfg.Environment - Hibernate 3.2.0
2006.11.05 18:37:54,801 INFO  [main] - org.hibernate.cfg.Environment - hibernate.properties not found
2006.11.05 18:37:54,801 INFO  [main] - org.hibernate.cfg.Environment - Bytecode provider name : cglib
2006.11.05 18:37:54,851 INFO  [main] - org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
2006.11.05 18:37:55,622 INFO  [main] - org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.HSQLDialect
2006.11.05 18:37:56,163 INFO  [main] - org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: businesslayer.modules.Contact.businessObject.Address._impl.AddressImpl
2006.11.05 18:37:56,323 INFO  [main] - org.hibernate.cfg.annotations.EntityBinder - Bind entity businesslayer.modules.Contact.businessObject.Address._impl.AddressImpl on table Address
2006.11.05 18:37:56,834 INFO  [main] - org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
2006.11.05 18:37:56,844 INFO  [main] - org.hibernate.tool.hbm2ddl.SchemaExport - writing generated schema to file: c://temp//sql.txt
2006.11.05 18:37:56,844 INFO  [main] - org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
2006.11.05 18:37:56,864 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
2006.11.05 18:37:56,864 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
2006.11.05 18:37:56,864 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
2006.11.05 18:37:56,864 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:C:/temp/testData
2006.11.05 18:37:56,864 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=sa, password=****}
drop table Address if exists
drop table dual_AddressIDSeq if exists
drop sequence AddressIDSeq
create table Address (_addressPK bigint not null, primary key (_addressPK))
create table dual_AddressIDSeq (zero integer)
insert into dual_AddressIDSeq values (0)
create sequence AddressIDSeq start with 1
2006.11.05 18:37:58,086 INFO  [main] - org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete
2006.11.05 18:37:58,086 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:hsqldb:C:/temp/testData
2006.11.05 18:37:58,126 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
2006.11.05 18:37:58,126 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
2006.11.05 18:37:58,126 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
2006.11.05 18:37:58,126 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:C:/temp/testData
2006.11.05 18:37:58,126 INFO  [main] - org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=sa, password=****}
2006.11.05 18:37:58,176 INFO  [main] - org.hibernate.cfg.SettingsFactory - RDBMS: HSQL Database Engine, version: 1.8.0
2006.11.05 18:37:58,176 INFO  [main] - org.hibernate.cfg.SettingsFactory - JDBC driver: HSQL Database Engine Driver, version: 1.8.0
2006.11.05 18:37:58,196 INFO  [main] - org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.HSQLDialect
2006.11.05 18:37:58,196 INFO  [main] - org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
2006.11.05 18:37:58,246 INFO  [main] - org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2006.11.05 18:37:58,246 INFO  [main] - org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
2006.11.05 18:37:58,246 INFO  [main] - org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
2006.11.05 18:37:58,246 INFO  [main] - org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
2006.11.05 18:37:58,256 INFO  [main] - org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
2006.11.05 18:37:58,256 INFO  [main] - org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
2006.11.05 18:37:58,256 INFO  [main] - org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
2006.11.05 18:37:58,256 INFO  [main] - org.hibernate.cfg.SettingsFactory - Connection release mode: auto
2006.11.05 18:37:58,256 INFO  [main] - org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
2006.11.05 18:37:58,256 INFO  [main] - org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
2006.11.05 18:37:58,256 INFO  [main] - org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
2006.11.05 18:37:58,256 INFO  [main] - org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2006.11.05 18:37:58,276 INFO  [main] - org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
2006.11.05 18:37:58,276 INFO  [main] - org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
2006.11.05 18:37:58,276 INFO  [main] - org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
2006.11.05 18:37:58,276 INFO  [main] - org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
2006.11.05 18:37:58,276 INFO  [main] - org.hibernate.cfg.SettingsFactory - Query cache: disabled
2006.11.05 18:37:58,276 INFO  [main] - org.hibernate.cfg.SettingsFactory - Cache provider: org.hibernate.cache.NoCacheProvider
2006.11.05 18:37:58,276 INFO  [main] - org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
2006.11.05 18:37:58,276 INFO  [main] - org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
2006.11.05 18:37:58,296 INFO  [main] - org.hibernate.cfg.SettingsFactory - Statistics: disabled
2006.11.05 18:37:58,296 INFO  [main] - org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
2006.11.05 18:37:58,306 INFO  [main] - org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
2006.11.05 18:37:58,516 INFO  [main] - org.hibernate.impl.SessionFactoryImpl - building session factory
2006.11.05 18:37:59,678 INFO  [main] - org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" java.lang.NullPointerException
   at org.hibernate.ejb.event.EJB3PersistEventListener.invokeSaveLifecycle(EJB3PersistEventListener.java:31)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:176)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
   at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
   at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
   at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
   at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
   at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:212)
   at test.Test.main(Test.java:38)
   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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Process finished with exit code 1


Zur Vervollstaendigung:
Code:
package businesslayer.modules.Contact.businessObject.Address._impl;

import javax.persistence.*;

@Entity
@Table(name = AddressTable.TABLENAME)
public class AddressImpl
{
  @Id
  @GeneratedValue(generator = "Address_Gen",strategy = GenerationType.SEQUENCE)
  @SequenceGenerator(name = "Address_Gen",sequenceName = "AddressIDSeq")
  public long _addressPK;

  public long getAddressPK()
  {
    return _addressPK;
  }

  public void setAddressPK(long addressPK)
  {
    _addressPK = addressPK;
  }
}


Meine letzte Alternative waere mir den Source Code von Hibernate reinzuziehen.

Danke im Vorraus fuer eure Bemuehungen

ps: Falls ihr euch auch daran die Zaehne ausbeisst, waere es gut, wenn ihr mir das mitteilen wuerdet. Dann weiss ich wenigstens das ich nicht ganz doof bin ...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 7:16 pm 
Newbie

Joined: Mon Nov 06, 2006 7:08 pm
Posts: 3
Hi...

I don't understand german, but I understood your problem because I'm experiencing the same thing .

I think this problem is mainly caused by the way we initialize Hibernate EJB3 Configurator.

Analysing hibernate entity manager source code, I identified that if we do not call the configure(...) methods, callbacks are not properly initialized in listeners. This leads to this NullPointerException.

The createEntityManagerFactory() method is maked as "deprecated" but if you look at the source code:

public EntityManagerFactory createEntityManagerFactory() {
configure( cfg.getProperties(), new HashMap() );
return buildEntityManagerFactory();
}

you'll see that it calls the configure method...

So using createEntityManagerFactory in place of buildEntityManagerFactory solved the problem for me.


Perhaps this is a bug ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 3:18 am 
Newbie

Joined: Sun Nov 05, 2006 1:41 pm
Posts: 4
Many Thanks!
and Yes its a bug. The TODO Comment is unambiguous.
//TODO move it to each event listener initialize() ? The Answer: Yes

OR
Code:
private Ejb3Configuration configure(
         Properties properties, Map workingVars
   )

-->
Code:
public  Ejb3Configuration configure(
         Properties properties, Map workingVars
   )


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.