Class Company has a many-to-one relationship to Person (chief) which has a set of composite-elements (addresses). The Line: Session.createQuery( "from Company c join fetch c.owner o join fetch o.addresses a") produces a Nullpointer Exception.
Here is my test code (tested with hibernate-core-3.3.0.SP1):
Configuration cfg = new Configuration().addDirectory(new File("src/main/java/org/donmiguel"));
Properties p = new Properties(); p.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"); p.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver"); p.setProperty("hibernate.hbm2ddl.auto", "create-drop"); p.setProperty("hibernate.connection.username", "sa"); p.setProperty("hibernate.connection.password", ""); p.setProperty("hibernate.connection.url", "jdbc:hsqldb:file:testdb"); p.setProperty("hibernate.show_sql", "true"); p.setProperty("hibernate.format_sql", "true"); cfg.setProperties(p); SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession(); Query query = session.createQuery(// "from Company c " // + "join fetch c.owner o " // + "join fetch o.addresses a"// );
Here is the occuring Exception:
java.lang.NullPointerException at org.hibernate.loader.BasicLoader.isBag(BasicLoader.java:94) at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:76) at org.hibernate.loader.hql.QueryLoader.<init>(QueryLoader.java:119) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:204) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650) at org.donmiguel.TestHibernate.test(TestHibernate.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
And here are my classes:
public class Company { private Long id; private Person owner;
public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Person getOwner() { return owner; } public void setOwner(Person owner) { this.owner = owner; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.donmiguel"> <class name="Company"> <id name="id"> <generator class="native" /> </id> <many-to-one name="owner"/> </class> </hibernate-mapping>
public class Person { private Long id; private Set<Address> addresses; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Set<Address> getAddresses() { return addresses; } public void setAddresses(Set<Address> addresses) { this.addresses = addresses; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.donmiguel"> <class name="Person"> <id name="id"> <generator class="native" /> </id> <set name="addresses"> <key column="person_id" /> <composite-element class="Address"> <property name="country" /> <property name="street" /> </composite-element> </set> </class> </hibernate-mapping>
public class Address { private String street; private String country; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
Please, could anyone tell me, if I'm doing something wrong? Is the mapping incorrect? Any comments are welcome.
|