I've found a bug in AnnotationConfiguration that causes a NullPointerException.
Hibernate Annotations version:
3.3.0 GA
Hibernate version:
3.2.3 GA
Mapping documents:
orm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
version="1.0">
</entity-mappings>
(Yes, it is empty)
example/Main.javaCode:
package example;
import org.hibernate.cfg.AnnotationConfiguration;
public class Main {
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addResource("orm.xml");
config.addPackage("example");
}
}
example/package-info.javaCode:
@GenericGenerator(name = "myGenerator", strategy = "sequence")
package example;
import org.hibernate.annotations.GenericGenerator;
Full stack trace of any exception that occurs:Code:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.cfg.annotations.reflection.EJB3OverridenAnnotationReader.initAnnotations(EJB3OverridenAnnotationReader.java:360)
at org.hibernate.cfg.annotations.reflection.EJB3OverridenAnnotationReader.isAnnotationPresent(EJB3OverridenAnnotationReader.java:263)
at org.hibernate.annotations.common.reflection.java.JavaXAnnotatedElement.isAnnotationPresent(JavaXAnnotatedElement.java:43)
at org.hibernate.cfg.AnnotationBinder.bindPackage(AnnotationBinder.java:220)
at org.hibernate.cfg.AnnotationConfiguration.addPackage(AnnotationConfiguration.java:165)
at example.Main.main(Main.java:9)
Name and version of the database you are using:N/A
The generated SQL (show_sql=true):N/A
Debug level Hibernate log excerpt:Code:
2007-04-18 14:59:05,419 [main] INFO org.hibernate.cfg.annotations.Version:15 - Hibernate Annotations 3.3.0.GA
2007-04-18 14:59:05,451 [main] INFO org.hibernate.cfg.Environment:509 - Hibernate 3.2.3
2007-04-18 14:59:05,451 [main] INFO org.hibernate.cfg.Environment:542 - hibernate.properties not found
2007-04-18 14:59:05,451 [main] INFO org.hibernate.cfg.Environment:676 - Bytecode provider name : cglib
2007-04-18 14:59:05,466 [main] INFO org.hibernate.cfg.Environment:593 - using JDK 1.4 java.sql.Timestamp handling
2007-04-18 14:59:05,560 [main] INFO org.hibernate.cfg.Configuration:553 - Reading mappings from resource : orm.xml
2007-04-18 14:59:05,669 [main] DEBUG org.hibernate.util.DTDEntityResolver:38 - trying to resolve system-id [file:///C:/dev/intellijprojects/hibernate-examples/orm_1_0.xsd]
2007-04-18 14:59:05,669 [main] DEBUG org.hibernate.cfg.EJB3DTDEntityResolver:31 - recognized EJB3 ORM namespace; attempting to resolve on classpath under org/hibernate/ejb
2007-04-18 14:59:05,669 [main] DEBUG org.hibernate.cfg.EJB3DTDEntityResolver:40 - located [file:///C:/dev/intellijprojects/hibernate-examples/orm_1_0.xsd] in classpath
2007-04-18 14:59:05,810 [main] INFO org.hibernate.cfg.AnnotationConfiguration:163 - Mapping package example
The problem is caused by the order of the addResource and addPackage calls in combination with having a package-info.java file with some contents. If the addPackage is called before the addResource, or if the GenericGenerator annotation in the package info file is removed, there is no problem.
I cannot change the order of those calls for my production code, however, because Spring is being used to create the SessionFactory and it calls the methods in that order: resource followed by package.
There appears to be a problem with initializing
org.hibernate.cfg.annotations.reflection.EJB3OverridenAnnotationReader. When this call is made on line 360:
Code:
annotationsMap.put( ann.annotationType(), ann );
the map is null. Inserting a creation for the map, (borrowed form elsewhere in the method) on line 359, before the "for" statement:
Code:
annotationsMap = new HashMap<Class, Annotation>( annotations.length + 5 );
resolves the problem.
I wanted to post this here just to be sure there isn't something I could be missing before I create an issue in the JIRA.