Hi,

I just try to use Hibernate / JPA in servicemix.

What I did:

1.) in conf/jndi.xml I added an entry to jndiEntries map:

<entry key="JPAEntityManager" value-ref="entityManagerFactory" />


additionally outside the map:


<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
    <property name="loadTimeWeaver">
      <bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
    </property>
  </bean>

  <bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
      <list>
         <value>classpath*:cp-persistence.xml</value>
      </list>
    </property>
    <!-- if no datasource is specified, use this one -->
    <property name="defaultDataSource" ref="CpEsbDataSource"/>
  </bean>

  <bean id="CpEsbDataSource"
class="org.postgresql.jdbc2.optional.SimpleDataSource">
    <property name="databaseName" value="cp_esb_db" />
    <property name="user" value="myuser" />
    <property name="password" value="mypass" />
  </bean> 


2.) in conf/ I added a file called cp-persistence.xml containing the
following:


<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="CpLoggingPU" transaction-type="RESOURCE_LOCAL">
                
                <description>logging persistence unit</description>
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
        
<jar-file>file:///opt/apache-servicemix-3.3-SNAPSHOT/lib/cp-shared-1.0-SNAPSHOT.jar</jar-file>
                <class>net.compart.jbi.shared.LogEntry</class>
                <properties>
                        <property name="hibernate.dialect"
value="org.hibernate.dialect.PostgreSQLDialect" />
                        <property name="hibernate.show_sql" value="true" />
                        <property name="hibernate.format_sql" value="true" />
                        <property name="hibernate.hbm2ddl.auto" value="update" 
/>
                </properties>

        </persistence-unit>

</persistence>


3.) I created a logEntry class in a jar (see reference in the
cp-persistence.xml) like this:


package net.compart.jbi.shared;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * @author lhe
 */
@Entity
public class LogEntry implements Serializable
{
    @Id
    @GeneratedValue
    private int id;
    
    private long timestamp;
    private String correlationId;
    private String logMessage;
    
    /**
     * default constructor
     */
    public LogEntry() 
        {}

    /**
     * @return the id
     */
    public int getId()
        {
        return this.id;
        }

    /**
     * @param id the id to set
     */
    public void setId(int id)
        {
        this.id = id;
        }

    /**
     * @return the timestamp
     */
    public long getTimestamp()
        {
        return this.timestamp;
        }

    /**
     * @param timestamp the timestamp to set
     */
    public void setTimestamp(long timestamp)
        {
        this.timestamp = timestamp;
        }

    /**
     * @return the logMessage
     */
    public String getLogMessage()
        {
        return this.logMessage;
        }

    /**
     * @param logMessage the logMessage to set
     */
    public void setLogMessage(String logMessage)
        {
        this.logMessage = logMessage;
        }

    /**
     * @return the correlationId
     */
    public String getCorrelationId()
        {
        return this.correlationId;
        }

    /**
     * @param correlationId the correlationId to set
     */
    public void setCorrelationId(String correlationId)
        {
        this.correlationId = correlationId;
        }
}


4.) Because of serveral startup errors of smx I needed to add several libs
(mostly hibernate and dependent jars) to the lib folder of smx.


---------
When I startup servicemix everything seems to work fine. The table is
created and no errors are thrown. So I would think that the configuration is
fine.


INFO  - Version                        - Hibernate Annotations 3.3.1.GA
INFO  - Environment                    - Hibernate 3.2.6
INFO  - Environment                    - hibernate.properties not found
INFO  - Environment                    - Bytecode provider name : cglib
INFO  - Environment                    - using JDK 1.4 java.sql.Timestamp
handling
INFO  - Version                        - Hibernate EntityManager 3.3.2.GA
INFO  - Ejb3Configuration              - Processing PersistenceUnitInfo [
        name: CpLoggingPU
        ...]
INFO  - AnnotationBinder               - Binding entity from annotated
class: net.compart.jbi.shared.LogEntry
INFO  - EntityBinder                   - Bind entity
net.compart.jbi.shared.LogEntry on table LogEntry
INFO  - AnnotationConfiguration        - Hibernate Validator not found:
ignoring
INFO  - ConnectionProviderFactory      - Initializing connection provider:
org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
INFO  - InjectedDataSourceConnectionProvider - Using provided datasource
INFO  - SettingsFactory                - RDBMS: PostgreSQL, version: 8.2.7
INFO  - SettingsFactory                - JDBC driver: PostgreSQL Native
Driver, version: PostgreSQL 8.2 JDBC3 with SSL (build 508)
INFO  - Dialect                        - Using dialect:
org.hibernate.dialect.PostgreSQLDialect
INFO  - TransactionFactoryFactory      - Transaction strategy:
org.hibernate.transaction.JDBCTransactionFactory
INFO  - TransactionManagerLookupFactory - No TransactionManagerLookup
configured (in JTA environment, use of read-write or transactional
second-level cache is not recommended)
INFO  - SettingsFactory                - Automatic flush during
beforeCompletion(): disabled
INFO  - SettingsFactory                - Automatic session close at end of
transaction: disabled
INFO  - SettingsFactory                - JDBC batch size: 15
INFO  - SettingsFactory                - JDBC batch updates for versioned
data: disabled
INFO  - SettingsFactory                - Scrollable result sets: enabled
INFO  - SettingsFactory                - JDBC3 getGeneratedKeys(): disabled
INFO  - SettingsFactory                - Connection release mode: auto
INFO  - SettingsFactory                - Default batch fetch size: 1
INFO  - SettingsFactory                - Generate SQL with comments:
disabled
INFO  - SettingsFactory                - Order SQL updates by primary key:
disabled
INFO  - SettingsFactory                - Order SQL inserts for batching:
disabled
INFO  - SettingsFactory                - Query translator:
org.hibernate.hql.ast.ASTQueryTranslatorFactory
INFO  - ASTQueryTranslatorFactory      - Using ASTQueryTranslatorFactory
INFO  - SettingsFactory                - Query language substitutions: {}
INFO  - SettingsFactory                - JPA-QL strict compliance: enabled
INFO  - SettingsFactory                - Second-level cache: enabled
INFO  - SettingsFactory                - Query cache: disabled
INFO  - SettingsFactory                - Cache provider:
org.hibernate.cache.NoCacheProvider
INFO  - SettingsFactory                - Optimize cache for minimal puts:
disabled
INFO  - SettingsFactory                - Structured second-level cache
entries: disabled
INFO  - SettingsFactory                - Echoing all SQL to stdout
INFO  - SettingsFactory                - Statistics: disabled
INFO  - SettingsFactory                - Deleted entity synthetic identifier
rollback: disabled
INFO  - SettingsFactory                - Default entity-mode: pojo
INFO  - SettingsFactory                - Named query checking : enabled
INFO  - SessionFactoryImpl             - building session factory
INFO  - SessionFactoryObjectFactory    - Not binding factory to JNDI, no
JNDI name configured
INFO  - SchemaUpdate                   - Running hbm2ddl schema update
INFO  - SchemaUpdate                   - fetching database metadata
INFO  - SchemaUpdate                   - updating schema
INFO  - TableMetadata                  - table found: public.logentry
INFO  - TableMetadata                  - columns: [correlationid,
logmessage, timestamp, id]
INFO  - TableMetadata                  - foreign keys: []
INFO  - TableMetadata                  - indexes: [logentry_pkey]
INFO  - SchemaUpdate                   - schema update complete



Now I am trying to create LogEntry datasets and this causes the following
error:


java.lang.ClassCastException: $Proxy6
        at net.compart.jbi.shared.CpEsbUtils.logToDB(CpEsbUtils.java:146)
        at
net.compart.jbi.shared.CpEsbUtils.logProcessingStepToHistory(CpEsbUtils.java:127)
        at
net.compart.shared.CpFileMarshaler.readMessage(CpFileMarshaler.java:86)
        at
org.apache.servicemix.file.FilePollerEndpoint.processFile(FilePollerEndpoint.java:281)
        at
org.apache.servicemix.file.FilePollerEndpoint.processFileAndDelete(FilePollerEndpoint.java:253)
        at
org.apache.servicemix.file.FilePollerEndpoint$1.run(FilePollerEndpoint.java:231)
        at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)
        at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:675)
        at java.lang.Thread.run(Thread.java:595)


This is the code snippet of the method which should do the trick:

------------------8<--------------------------
private static final void logToDB(final String logmsg, final String
correlationId)
        {
        LocalContainerEntityManagerFactoryBean emBean = null;
        
        try
            {
            Context initialContext = new InitialContext();
            Object o = initialContext.lookupLink(EM_KEY);
            logger.info("Looked up object: " + o.getClass().getName());
            emBean = (LocalContainerEntityManagerFactoryBean)o;
            EntityManager em = emBean.getObject().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();      

            LogEntry logEntry = new LogEntry();
            logEntry.setCorrelationId(correlationId);
            logEntry.setTimestamp(System.currentTimeMillis());
            logEntry.setLogMessage(logmsg);            
            em.persist(logEntry);
            
            tx.commit();
            em.close();
            }
        catch (NamingException ex)
            {
            logger.error("Error retrieving the JPA EM factory from JNDI
context.", ex);
            }
        }
------------------8<--------------------------


The output from line      
   logger.info("Looked up object: " + o.getClass().getName());
is 
   INFO  - CpEsbUtils                     - Looked up object: $Proxy6


Does anybody has an idea what is wrong here? My thinking was that the looked
up object is of type LocalContainerEntityManagerFactoryBean, but it isn't
actually.
How to solve this problem?

Regards,
Lars

-- 
View this message in context: 
http://www.nabble.com/JPA---Hibernate-problem-withing-smx-tp16848196p16848196.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.

Reply via email to