4 Ekim 2009 Pazar

notes on schuchert jpa tutor 1

i am following schuchert's jpa tutor 1 at http://schuchert.wikispaces.com/JPA+Tutorial+1+-+Getting+Started
however, my environment is netbeans 6.5 + glassfish v2 + postgre..
lets note down some drawbacks i faced:
  • i get the following error while trying to run PersonTest :
Testcase: emptyTest(entity.PersonTest):        Caused an ERROR
[PersistenceUnit: SchuchertJpaTutor1PU] Unable to build EntityManagerFactory
javax.persistence.PersistenceException: [PersistenceUnit: SchuchertJpaTutor1PU] Unable to build EntityManagerFactory
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
        at entity.PersonTest.initEmfAndEm(PersonTest.java:24)
Caused by: org.hibernate.HibernateException: Could not find datasource
        at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:56)
        at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
        at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
        at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
        at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)

Testcase: emptyTest(entity.PersonTest):        Caused an ERROR
null
java.lang.NullPointerException at entity.PersonTest.cleanup(PersonTest.java:31)
Test entity.PersonTest FAILED

to fix:
- use in memory database in unit tests with persistence.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="SchuchertJpaTutor1TestPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source></non-jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa"/>
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <property name="hibernate.connection.username" value="sa"/>
        <property name="hibernate.connection.password" value=""/>
    </properties>
  </persistence-unit>
</persistence>
- add hsqldb.jar and (possibly) servlet.jar to test libs 

and run the test again, it should pass now..

  • when Address is added to Person, delete the final keyword from getter and setter methods for address.(it results in compile error..)

i will continue with the next tutorial, so see you at the next blog entry :)

Hiç yorum yok:

Yorum Gönder