RE: Adding a new Hibernate DataSource with annotations

2996624, modified 16 Years ago. New Member Posts: 9 Join Date: 5/1/09 Recent Posts
Hi all,

i need some hints on how to proceed to build my application.

I've an already existing data layer comprised of:
- domain model POJOs classes annotated with JPA annotation (plus some hibernate3 specifics)
- a generic DAO layer
- some components using the DAOs to implement higher level business intelligence.

I would like to integrate these in liferay but all the guides end up to use service.xml to define entities, tables and so on.

What i would do is to expose a new data source, the annotated POJOs classes and the DAOs as spring beans towards the portlet layer.

Till now i've done the following:

In the portal-ext.properties i've defined the new data source "myds"

file: /ext/ext-impl/portal-ext.properties
# jdbc.myds.driverClassName=org.postgresql.Driver
# jdbc.myds.url=jdbc:postgresql://localhost:5432/myds
# jdbc.myds.username=myds
# jdbc.myds.password=myds

In the file ext-spring.xml i've put the following beans definitions:

<bean id="mydsDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://localhost/myds</value>
</property>
<property name="username">
<value>myds</value>
</property>
<property name="password">
<value>myds</value>
</property>
</bean>

<bean id="mydsNamingStrategy"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField">
<value>org.hibernate.cfg.ImprovedNamingStrategy.INSTANCE</value>
</property>
</bean>

<bean id="mydsSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="namingStrategy">
<ref bean="mydsNamingStrategy" />
</property>
<property name="annotatedClasses">
<list>
<value>it.myds.datalayer.domainmodel.DSUser</value>
.... other annotated classes....
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="dataSource">
<ref bean="mydsDataSource" />
</property>
</bean>

I've put data model POJOs (those in ns "it.myds.datalayer.domainmodel.*") in ext-services/src.
I've also added some libraries to ext-lib/development:
• /ext/ext-lib/development/commons-lang-2.4.jar
• /ext/ext-lib/development/ejb3-persistence.jar
• /ext/ext-lib/development/hibernate-annotations.jar
• /ext/ext-lib/development/hibernate-core.jar
• /ext/ext-lib/development/hibernate-entitymanager.jar
• /ext/ext-lib/development/hibernate-validator.jar

I've build and redeployed ext and all went successfully at compile time.
(As a side note, do i'll need to put (eventually some of) these libs also in global or portal dirs to be accessed at runtime?)

Now before integrating DAOs, i would like to obtain "mydsSessionFactory" bean from a portlet to test the access to the new datasource. How i can refer to those beans from the portlet context?

The fact is that my data layer uses annotations to define a quite complex schema. This is the reason i choosed integration instead of migrating it towards service builder.
Is this the right way to proceed in order to add a pre-existing datalayer based on Hibernate annotations, not using Service Builder ?

Please, can you also comment on what i'm doing so if there is something done wrong i can restart doing things in right way ?

Sorry for such long mail but i wasn't able to find answers around.

Thank you all,
Luca
2996624, modified 16 Years ago. New Member Posts: 9 Join Date: 5/1/09 Recent Posts
Luca Bernard:
Hi all,

I've put data model POJOs (those in ns "it.myds.datalayer.domainmodel.*") in ext-services/src.
I've also added some libraries to ext-lib/development:
• /ext/ext-lib/development/commons-lang-2.4.jar
• /ext/ext-lib/development/ejb3-persistence.jar
• /ext/ext-lib/development/hibernate-annotations.jar
• /ext/ext-lib/development/hibernate-core.jar
• /ext/ext-lib/development/hibernate-entitymanager.jar
• /ext/ext-lib/development/hibernate-validator.jar

I've build and redeployed ext and all went successfully at compile time.



Just an update; when i start the portal i get the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [persistence.hibernate.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: configurationClass must be assignable to [org.hibernate.cfg.Configuration]
Caused by:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: configurationClass must be assignable to [org.hibernate.cfg.Configuration]

I suspect that this is a classpath clash. I've used hibernate-core-3.3.2, hibernate-annotations-3.4.0, hibernate-validator-3.1.0.

Any hints ?
2996624, modified 16 Years ago. New Member Posts: 9 Join Date: 5/1/09 Recent Posts
Luca Bernard:
Hi all,

Just an update; when i start the portal i get the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [persistence.hibernate.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: configurationClass must be assignable to [org.hibernate.cfg.Configuration]
Caused by:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: configurationClass must be assignable to [org.hibernate.cfg.Configuration]

I suspect that this is a classpath clash. I've used hibernate-core-3.3.2, hibernate-annotations-3.4.0, hibernate-validator-3.1.0.



Ok to fix this i needed some of the dev libs in portal. Moreover seems that annotation are required to be 3.2.x.

Now portal runs and registers all my beans.

But still trying to figure out how to access them from a portlet.

Cheers,
Luca