-->
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.  [ 7 posts ] 
Author Message
 Post subject: [ISSUE] Hibernate, Junit4, Ant1.7.1
PostPosted: Tue Mar 03, 2009 5:59 pm 
Newbie

Joined: Fri Feb 06, 2009 2:24 pm
Posts: 12
I'm fairly new to Hibernate and I'm trying to set up ant (1.7.1) to run my junit4 tests etc. However, it seems I keep running into this one exception (as seen below) from my junit reports. I'm not quite sure what's going on. My only guess is that ant isn't running in a jboss container and is therefore not excuting something that the container would. As a result, the annotations on the com.xxxx.pcm.spi.builder.xmlbuilders.ItemOwnerXmlBuilderRecord class are not getting read and therefore the mapping from the mapping file isn't working. Hence the reason why I am getting the "Errors in named queries: importer:itemOwnerSelect"

Any thoughts on what I could be doing wrong? Thanks for any help!

Hibernate version:
3.2

Mapping documents:
<?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 http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">

<named-native-query name="importer:itemOwnerSelect"
result-set-mapping="mapping:itemOwnerSelect">
<query>
<![CDATA[
<query here - trying to save space, yes it works when run with sqldeveloper or sqlplus or in eclipse>
]]>
</query>
</named-native-query>

<sql-result-set-mapping name="mapping:itemOwnerSelect">
<entity-result entity-class="com.xxxx.pcm.spi.builder.xmlbuilders.ItemOwnerXmlBuilderRecord"/>
</sql-result-set-mapping>


Full stack trace of any exception that occurs:
java.lang.ExceptionInInitializerError at com.xxx.common.util.persistence.HibernateUtil.init(HibernateUtil.java:69) at com.xxx.pcm.spi.builder.JunitSetup.setup(Unknown Source) at com.xxx.pcm.spi.builder.xmlbuilders.ItemOwnerXmlBuilderTest.setup(Unknown Source)Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: Errors in named queries: importer:itemOwnerSelect at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:720) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83) at com.xxx.common.util.persistence.HibernateUtil.init(HibernateUtil.java:61)Caused by: org.hibernate.HibernateException: Errors in named queries: importer:itemOwnerSelect at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:365) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)N/A

java.lang.NullPointerException at com.xxx.pcm.spi.builder.xmlbuilders.ItemOwnerXmlBuilderTest.tearDown(Unknown Source)

Name and version of the database you are using:
Oracle 10.2

The generated SQL (show_sql=true):


Last edited by paulsjv on Thu Mar 05, 2009 12:59 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 5:26 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
(Do you have to use JPA-xml?
When using JPA-only (no hibernate-additional functions), I'd prefer annotations.)


When using JUnit, you normally have to add the annotated classes manually to your test-case-configuration. You can do it in an abstract class which serves as base-class for your tests:
Code:
public abstract class AbstractTest extends TestCase {

   private SessionFactory factory;
   private Session session;

   @Override
   protected void setUp() throws Exception {
      super.setUp();

      AnnotationConfigurationcfg = new AnnotationConfiguration();
      for (Class c : getAnnotatedClasses()) {
         cfg.addAnnotatedClass(c);
      }
      cfg.configure();

      factory = cfg.buildSessionFactory();
      session = factory.openSession();
      ...
   }
   //should return annotated classes
   protected abstract Class[] getAnnotatedClasses();
}

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 2:10 pm 
Newbie

Joined: Fri Feb 06, 2009 2:24 pm
Posts: 12
We are actually using persistence.xml and importing the external named queries.

However, I still tried your solution and it was looking for the hibernate.cfg.xml file, which we aren't using. Have any suggestions as to what to do next?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 5:32 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
For your testcases you could create a hibernate.cfg.xml and set hibernate.ejb.cfgfile in persistence.xml to point to the cfg. But that shouldn't be necessary, as class-mappings can be autodiscovered.

You can also set them manually, look here.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 12:54 pm 
Newbie

Joined: Fri Feb 06, 2009 2:24 pm
Posts: 12
Thanks for your continued help on this! I've been trying to figure it out for weeks now. :) So your help is GREATLY appreciated!

I believe I have things set up manually (as far as I can tell). Let me post my persistence.xml and the xml files that hold the named query that I'm trying to test in my unit tests.

Couple things... I build my project that I am going to run my tests against in a totally different ant build which is then put into a separate directory. Not a big deal as I include the class files/jars that it creates into my test class path. The persistence.xml and the queries-importer.xml files are part of my test ant build. My test code is in a different ant build so if the project build is successful my continuous integration tool fires off the test build. The test build uses the persistence.xml file and the queries-importer.xml file. The queries-importer.xml file is from the project build and is copied over upon the test ant build start. In the <jar-file> element it points to a jar file but it does not contain the persistence.xml file but it does contain all the annotated classes that are referenced in the queries-importer.xml file.

At any rate, I hope this is helpful. I'm going to keep trying other ideas I have as well. Thanks again for your continued help with my issue!

persistence.xml
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
        <persistence-unit name="extract" transaction-type="RESOURCE_LOCAL">
                <mapping-file>queries-importer.xml</mapping-file>
                <jar-file>file:/path/to/jar</jar-file>
                <properties>
                   <!-- db information was here -->
                        <property name="hibernate.show_sql" value="false" />
                        <property name="hibernate.format_sql" value="true" />
                        <property name="hibernate.default_batch_fetch_size" value="20" />
                        <property name="hibernate.jdbc.batch_size" value="20" />
                        <property name="hibernate.jdbc.fetch_size" value="20" />
                        <property name="hibernate.jdbc.use_get_generated_keys" value="true" />
                        <property name="hibernate.c3p0.max_size" value="10" />
                        <property name="hibernate.c3p0.min_size" value="1" />
                        <property name="hibernate.c3p0.timeout" value="1800" />
                        <property name="hibernate.c3p0.max_statements" value="40" />
                        <property name="hibernate.c3p0.maxStatementsPerConnection" value="20" />
                        <property name="hibernate.bytecode.use_reflection_optimizer" value="true" />
                        <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
                        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
                        <property name="hibernate.archive.autodetection" value="class,hbm" />
                </properties>
        </persistence-unit>
</persistence>


queries-importer.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 http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="1.0">

<named-native-query name="importer:itemOwnerSelect" result-set-mapping="mapping:itemOwnerSelect">
        <query>
                <![CDATA[
                SELECT
                        *
                FROM <VIEW>
                ]]>
        </query>
</named-native-query>

<sql-result-set-mapping name="mapping:itemOwnerSelect">
        <entity-result entity-class="com.xxx.pcm.spi.builder.xmlbuilders.ItemOwnerXmlBuilderRecord"/>
</sql-result-set-mapping>

</entity-mappings>


ItemOwnerXmlBuilderRecord.java
Code:
@SqlResultSetMapping(name = "mapping:itemOwnerSelect",
      entities = @EntityResult(entityClass = ItemOwnerXmlBuilderRecord.class))      
@Entity
@org.hibernate.annotations.Entity(mutable=false)
public class ItemOwnerXmlBuilderRecord
{
      @Id
      @Column(name = "ASSIGNMENT_KEY")
      Long assignmentKey;
// more columns
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 4:03 pm 
Newbie

Joined: Fri Feb 06, 2009 2:24 pm
Posts: 12
This might be helpful as well.

Quote:
2009-Mar-05 14:13:09:124 hibernate.impl.SessionFactoryImpl ERROR Error in named query: importer:itemAVLSelect
org.hibernate.MappingException: Unknown entity: com.xxx.pcm.spi.builder.xmlbuilders.ItemOwnerBuilderRecord
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:312)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:353)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:332)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:148)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:64)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:43)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:114)
at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:446)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:352)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at com.xxx.common.util.persistence.HibernateUtil.init(HibernateUtil.java:61)
at com.xxx.pcm.spi.builder.JunitSetup.setup(Unknown Source)
at com.xxx.pcm.spi.builder.xmlbuilders.ItemOwnerBuilderTest.setup(Unknown Source)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:420)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:911)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:768)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 4:13 pm 
Newbie

Joined: Fri Feb 06, 2009 2:24 pm
Posts: 12
Well I finally found something that got it to work. In my persistence.xml file I needed to manually add the annotated classes. This is kind of a pain so now I'm wondering if there is a way to auto detect what classes are annotated and go ahead and load them?

Solution:
persistence.xml

Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0">
         <persistence-unit name="extract" transaction-type="RESOURCE_LOCAL">
                 <mapping-file>e2mcm-queries-importer.xml</mapping-file>
                 <jar-file>file:/path/to/jar</jar-file>
                 <!-- annotated class below -->
                 <class>com.xxx.pcm.spi.builder.xmlbuilders.ItemOwnerXmlBuilderRecord</class>
                 <properties>
<!-- other stuff below -->


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.