Jpos,Spring and Hibernate

1,194 views
Skip to first unread message

Kartik

unread,
Mar 16, 2009, 11:19:07 PM3/16/09
to jPOS Users
Hi All;

I am trying to use jpos with Spring and Hibernate. My code resides in
deploy directory along with the jpos configuration files which hold
information regarding Q2 server.I am able to receive the ISOMsg on my
configured port but as soon as Spring part is called via a class it
gives an error of could not initialize class as seen in Log report .
(Assume name of class is something then it says could not initialize
class Something)
I Call the Spring config files using as shown below
class something
{
static ClassPathXmlApplicationContext appContext = new
ClassPathXmlApplicationContext(
"com/../../Springmappings/spring.xml");
}
This file is present in the Jar file of the code in package as shown
above.This Spring file holds the hibernate mapping files also present
in the Jar file.
I assume one of the following is causing the issue:
1) It is unable to load the Spring config files
2) It cannot find the Spring Files
3) it is unable to load hibernate config files

Please provide me some assistance as database is one of my prime
priority Moreover is ther a way i can specify the Spring file outside
my jar code since i might change it time after time since it holds the
database Connection credentials

Alejandro Revilla

unread,
Mar 17, 2009, 8:25:31 AM3/17/09
to jpos-...@googlegroups.com
>
> I am trying to use jpos with Spring and Hibernate.
>
Have you considered using jPOS-EE? It uses Hibernate directly from Q2.

>
> My code resides in deploy directory along with the jpos configuration
> files which hold information regarding Q2 server.
>

Why do you put your code in the deploy directory? Are you talking about
the deploy/lib directory? is your code placed inside a jar? is Spring
starting Q2 or the other way around? Where are the Spring-related jars
located?

If you are building your application using jPOS' or jPOS-EE's ant build
file, you can create a modules/spring/lib and place all your jars there
before calling 'ant'.


Kartikeya Bhatt

unread,
Mar 18, 2009, 11:05:18 PM3/18/09
to jpos-...@googlegroups.com
1)Sorry Alejandro for being  less descriptive  but my code is in jar format and it is placed in deploy/lib directory
2)I was hoping Q2 would trigger spring. I am simply trying to call a class which calls the Spring XML file
static ClassPathXmlApplicationContext appContext = new
ClassPathXmlApplicationContext
(
                       "com/../../Springmappings/spring.xml");
}
in this spring file i am  hibernate config files are called as hibernate properties
3)I have placed the Spring jar along with spring -hibernate3.0 jar and all other related jars in the lib directorey and not deploy/lib deriectory
4) Could you please help me out on this and if possibel direct me in the right direction.

Kartik

unread,
Mar 19, 2009, 1:30:02 AM3/19/09
to jPOS Users
Hi all;
Can anyone tell me whats the reason behind such an error
Cannot convert value of type
[org.springframework.orm.hibernate3.LocalSessionFactoryBean] to
required type [org.hibernate.SessionFactory] for property
'sessionFactory

Would require help here this issue has been there with me for past a
week or so


On Mar 19, 8:05 am, Kartikeya Bhatt <kartikeya.payautom...@gmail.com>
wrote:

Alejandro Revilla

unread,
Mar 19, 2009, 8:10:50 AM3/19/09
to jpos-...@googlegroups.com
I think that our MBeanServer based classloader could have some conflict
with Spring's so I would stay away from putting spring's files in the
deploy/lib directory.

If you place the spring related jars in the lib directory you should
either add them manually to the classpath or place them in a module (i.e.:
modules/spring/lib) at compile time so jPOS' build would create the
proper MANIFEST.MF.

The jPOS build system picks all libs available in modules/*/lib/*.jar
and create a Class-Path that ends up in the build/jpos.jar's
META-INF/MANIFEST.MF, so when you call java -jar jpos.jar it knows which
jars to use.

PS.- Say hi to Sharad and Stephen

Kartik

unread,
Mar 20, 2009, 3:22:34 AM3/20/09
to jPOS Users
Hi Alejandro ;

If i want to add the Spring related jars directly into the lib
directory then what is the process am i supposed to follow because the
solution doesn't seem to be working i again get the same old message
in my log files.Is there any other way do this

P.S-Your hi message was conveyed :)

Alejandro Revilla

unread,
Mar 20, 2009, 7:14:43 AM3/20/09
to jpos-...@googlegroups.com
>
> If i want to add the Spring related jars directly into the lib
> directory then what is the process am i supposed to follow because the
> solution doesn't seem to be working i again get the same old message
> in my log files.Is there any other way do this
>
You can create a module specific for your needs, for example:

in your jPOS or jPOS-EE root directory, try

mkdir -p modules/payautomata/lib
place all your addon jars into that lib directory (i.e. Hibernate, Spring, etc.)
call 'ant'

Ant will move all the jars from all modules into build/lib, and it will
create a build/jpos.jar (or build/jposee.jar). The trick is that inside
that jar, the MANIFEST.MF will contain the proper Class-Path.

>
> P.S-Your hi message was conveyed :)
>

:) Great

Kartik

unread,
Mar 25, 2009, 7:06:50 AM3/25/09
to jPOS Users
Sorry Alejandro for taking so long to respond
I applied the changes as suggested by you and the error regarding the
spring hibernate doesn't exist but it seems still spring is not
getting configured
It simply doesn't trigger the spring .XML file
Can you please help me with this and is there a way to specify the
Spring.XML file in the jpos config files itself so that whenever the
Q2 server starts it configures the spring file
Thanks Kartik

On Mar 20, 4:14 pm, Alejandro Revilla <a...@jpos.org> wrote:
> > If i want to add theSpringrelated jars directly into the lib
> > directory then what is the process am i supposed to follow because the
> > solution doesn't seem to be working i again get the same old message
> > in my log files.Is there any other way do this
>
> You can create a module specific for your needs, for example:
>
> in your jPOS or jPOS-EE root directory, try
>
>    mkdir -p modules/payautomata/lib
>    place all your addon jars into that lib directory (i.e.Hibernate,Spring, etc.)

Alejandro Revilla

unread,
Mar 25, 2009, 7:29:52 AM3/25/09
to jpos-...@googlegroups.com
I'm sorry but I'm not familiar with Spring.

suman gona

unread,
Mar 25, 2009, 7:58:03 AM3/25/09
to jpos-...@googlegroups.com
Kartik,
 
You Need to Configure that XML File as follows in 03_spring.xml(place it in Deploy Directory)

<spring-context class="org.jpos.q2.spring.Spring">
   <property name="xxx" value="xxx"/>
 </spring-context>

Thank You,
Suman

Kartik

unread,
Mar 26, 2009, 4:26:13 AM3/26/09
to jPOS Users
Can you be more specific i have a spring .xml file which has
defintions of hibernate mappings ,hibernatesession, as well as
transactiomanager specification. I call this xml file vial
classpathxmlapplicationcontext (in one of the classes) and then use
the context variable to get bean definitions in one of the classes.
eg
DaoImpl dao = (DaoImpl) appContext
.getBean("DaoTarget");
then i use the dao varibale to call the necessary functions that
interact with hibernate to perform data processing.

but the problem is i get an error of class cast exception that cannot
convert DaoImpl to DaoImpl (as seen above i use a cast)
It simnply means none of the bean defintion are getting read from
spring config file.

Moreover when i don't run the app in Q2 from but from my IDE it runs
absolutely fine and dosent't give any error it has to be a problem
with some configs

Require some assistance here :) ----> :( (Since a long time)

Mark Salter

unread,
Mar 26, 2009, 5:13:56 AM3/26/09
to jpos-...@googlegroups.com
Kartik wrote:
>
> Moreover when i don't run the app in Q2 from but from my IDE it runs
> absolutely fine and dosent't give any error it has to be a problem
> with some configs
This is - I would think - a classpath problem.
Your IDE is possibly generating a classpath to meet your needs. May I
suggest you find out what the classpath looks like when your code is
invoked from your IDE and that you should seek to replicate the same
from your Q2 setup?

You are on your own here, but I would first try turning verbose logging
on in java to see what is being loaded when under your IDE and then
compare the output generated out of java under Q2. There must be a
difference, but it will be hard to spot - you have some work to do.

Find out first where the 'missing' class(es) are loaded from when run on
your IDE, and check that directory or jar is available to Q2.

You might need to raise the issue on a Spring mailing list, but if the
issue is your classpath or environment, they are not going to be able to
help you either.

>
> Require some assistance here :) ----> :( (Since a long time)

We can all see that, unfortunately you may not be seeking assistance in
the correct place and only you have your environment.

--
Mark

Kartik

unread,
Mar 26, 2009, 10:56:17 PM3/26/09
to jPOS Users
Yes mark i have checked out the class path in both my IDE and and the
classpath Q2 follows both are same the problem is that the spring
config files are not getting loaded that is why the above error occurs

Has anybody out there tried out Spring integration with jpos if yes
please help

P.S:From one jpos user to another

Kartik

unread,
Mar 27, 2009, 5:19:48 AM3/27/09
to jPOS Users
i have implemented as suggested by suman above and the 03_spring.xml
is getting deployed moreover i have also rechecked by making mistakes
in the original spring.xml file which is set as a property in
03_spring.xml residing in deploy.Its show an exception means
spring.xml is getting triggered and set as property in config in
03_spring.xml.

But when i call this config property in my
class="org.jpos.q2.spring.Spring as mentioned by suman i still get an
error
as mentioned later
{
DaoImpl dao = (DaoImpl) appContext
.getBean("DaoTarget");
then i use the dao varibale to call the necessary functions that
interact with hibernate to perform data processing.

but the problem is i get an error of class cast exception that cannot
convert DaoImpl to DaoImpl (as seen above i use a cast)
It simply means none of the bean definition are getting read from
spring config file. }

The config propert is called as shown below
public void initService () throws ConfigurationException {
configFiles = cfg.getAll("config");
if ( configFiles.length < 1 )
throw new ConfigurationException ("config property not
specified");

appContext = new FileSystemXmlApplicationContext
( configFiles );
}
My class implements QbeanSupport too it seems i am just stuck at some
point can someone help me with this

Mark Salter

unread,
Mar 27, 2009, 6:42:17 AM3/27/09
to jpos-...@googlegroups.com
Kartik wrote:

> It simply means none of the bean definition are getting read from
> spring config file.

I know nothing about what you are doing, but perhaps that will help...

I googled for "+spring +FileSystemXmlApplicationContext" :-

http://tinyurl.com/df9q69

The 3rd hit (out over 12000) points to example code for "Spring Load
Resource from file, classpath and URL" :-


http://www.java2s.com/Code/Java/Spring/SpringLoadResourcefromfileclasspathandURL.htm
==
http://tinyurl.com/5enhjg

The FileSystemXmlApplicationContext expects a String or String[] of
config locations to load and monitor.


*If* you are using relative paths, then perhaps the active runtime
directory under your IDE is different to that when running under Q2 -
this is very likely!


I suggest you try and specify fully qualified file names into your
FileSystemXmlApplicationContext constructor. Otherwise you are
dependant on the directory in which java is asked to run your code.

To see what is perhaps wrong, I would include some code like :-

public void initService () throws ConfigurationException {
configFiles = cfg.getAll("config");
if ( configFiles.length < 1 )
throw new ConfigurationException ("config property
not specified");

// Dump file actual file locations.
for (String c:configFiles) {
File tf = new File(c);
System.out.println("Spring config file expected at
:"+tf.getAbsolutePath());
}

appContext = new FileSystemXmlApplicationContext( configFiles );
}

compile and run it under your IDE and Q2, see in System.out what files
you are actually trying to use and then make sure they actually exist.

The only configuration location you have shown us so far is :-

"com/../../Springmappings/spring.xml"

so I think this might be your problem. If it is, where do you want me
to send my invoice? I can also accept paypal too.

8)

I am surprised that if FileSystemXmlApplicationContext is passed a
missing file it doesn't throw an Exception. I can't see one mentioned
in this thread though?

--
Mark

Kartik

unread,
Mar 31, 2009, 12:50:40 AM3/31/09
to jPOS Users
Hi everyone i have removed the error the config files are getting
read but the problem is that still the app is not connecting to
database and retrieving data from it .

I have given the data source connection credentials in the spring.xml
file which does get configured since i am able to get the bean
definitions from the spring.xml file but it seems that on trying to
execute a query using hibernate Template return a null result so there
seems to be a problem does anyone has more ideas

Thanks mark your views did help BTW paypal is a good option :)

On Mar 27, 3:42 pm, Mark Salter <marksal...@talktalk.net> wrote:
> Kartik wrote:
> > It simply means none of the bean definition are getting read from
> > spring config file.
>
> I know nothing about what you are doing, but perhaps that will help...
>
> I googled for "+spring +FileSystemXmlApplicationContext" :-
>
>        http://tinyurl.com/df9q69
>
> The 3rd hit (out over 12000) points to example code for "Spring Load
> Resource from file, classpath and URL" :-
>
> http://www.java2s.com/Code/Java/Spring/SpringLoadResourcefromfileclas...
> ==http://tinyurl.com/5enhjg

suman gona

unread,
Mar 31, 2009, 2:05:41 AM3/31/09
to jpos-...@googlegroups.com
Kartik,

This post is getting iterated.It's purely relative to Spring.But the people who ever here they are relative to jpos.Mark already mentioned and gave you some URLs.Basing on those URL you can get the things.

Connecting to DB using Spring there are different ways:Here i am giving you the I/P basing on my way.If it's suitable to You ,then it can be used  for your application.
In spring we will configure the applcationconetext.xml as follows.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>

  <bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"><value>WEB-INF/hibernate.properties</value></property>
  </bean>
  
   <bean id="fileUploadMultipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000"/>
    </bean>
   
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"><value>${hibernate.connection.driver_class}</value></property>
    <property name="url"><value>${hibernate.connection.url}</value></property>
    <property name="username"><value>${hibernate.connection.username}</value></property>
    <property name="password"><value>${hibernate.connection.password}</value></property>  
  </bean>
  
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource"><ref local="dataSource"/></property>
       <property name="mappingResources">
            <list>
                <value>hbm location</value>
             </list>
        </property>
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
        </property>
  </bean> 

Hibernate.properties as
 ### Change this property to reflect the location of your
### database.
hibernate.connection.url=jdbc:mysql://localhost/dbname
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false

If it's a Web Application then web.xml as follwos

<web-app>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet>
    <servlet-name>zerofiweb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
</web-app>

***NOTE:From Next time i want to see the Spring related issues in Spring Forum.
http://forum.springframework.org/

Thanks,
Gona Suman

Kartik

unread,
Mar 31, 2009, 11:18:09 PM3/31/09
to jPOS Users
Thanks gona even i understand the issue is getting iterated all the
steps you mentioned above have been already done moreover the bean def
are getting initialized but the problem it seems is not spring related
beacuse i am alreday running the app from my ide and it works
perfectly the problem arises whee i try it out using jpos . As soon as
i deploy my code (present in run time directory which contain the
necessary lib files my code in deploy/lib and other config files in
cfg fdolder ) all the config files in deploy are getting initialized
as seen from logs even the 03_spring.xml you mentioned in previous
posts. I also able to send an ISOmsg via ssl to my configured port(:)
Thanks to mark and alejandro) but as soon as i pass the ISOMsg to be
saved in database it raises issue. As i pass the ISOMsg convert it
into a bean and the call the bean from spring.xml file declared in
spring-context it raises this wierd error which states class cast
exception cannot cast DaoImpl to DaoImpl though both are same
classes.,

Hence it is definetely not a spring issue it seems someway or other it
is jpos and spring not working in a cordial manner

Anyway thanks you guys have really helped me with this wothout doubt i
am greatfull to all of you
Hope i get will solution and if i do i will start a thread and jot
down my experience so that anyone in our group with similar problem
will be able to avoid it

On Mar 31, 11:05 am, suman gona <gona.su...@gmail.com> wrote:
> Kartik,
>
> This post is getting iterated.It's purely relative to Spring.But the people
> who ever here they are relative to jpos.Mark already mentioned and gave you
> some URLs.Basing on those URL you can get the things.
>
> Connecting to DB using Spring there are different ways:Here i am giving you
> the I/P basing on my way.If it's suitable to You ,then it can be used  for
> your application.
> In spring we will configure the *applcationconetext.xml* as follows.
> *Hibernate.properties* as
>  ### Change this property to reflect the location of your
> ### database.
> hibernate.connection.url=jdbc:mysql://localhost/dbname
> hibernate.connection.driver_class=com.mysql.jdbc.Driver
> hibernate.connection.username=root
> hibernate.connection.password=root
> hibernate.dialect=org.hibernate.dialect.MySQLDialect
> hibernate.hbm2ddl.auto=update
> hibernate.show_sql=false
>
> If it's a *Web Application* then web.xml as follwos
>
> <web-app>
>
>   <context-param>
>     <param-name>contextConfigLocation</param-name>
>     <param-value>WEB-INF/applicationContext.xml</param-value>
>   </context-param>
>
>   <servlet>
>     <servlet-name>context</servlet-name>
>
> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
>     <load-on-startup>1</load-on-startup>
>   </servlet>
>
>   <servlet>
>     <servlet-name>zerofiweb</servlet-name>
>
> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
>     <load-on-startup>1</load-on-startup>
>   </servlet>
> </web-app>
>
> ****NOTE*:From Next time i want to see the Spring related issues in Spring
> Forum.http://forum.springframework.org/
>
> Thanks,
> Gona Suman

Mark Salter

unread,
Apr 1, 2009, 2:47:07 AM4/1/09
to jpos-...@googlegroups.com
Kartik wrote:

> As i pass the ISOMsg convert it
> into a bean and the call the bean from spring.xml file declared in
> spring-context it raises this wierd error which states class cast
> exception cannot cast DaoImpl to DaoImpl though both are same
> classes.,

Are they both loaded by the same Classloader though? If not, they will
be seen as different classes?

Perhaps tracking (java -verbose) you can see if the DaoImpl (or
whatever) is load twice by two Classloaders?

Can the Classloader used to load a class be determined at runtime? I
would think so, but do not know the steps. Find out how and log the
detail for the class you are setting and the same detail for the object
you are trying to cast. See if they are the same!

>
> Hence it is definetely not a spring issue it seems someway or other it
> is jpos and spring not working in a cordial manner

I don't think it is that clear. This is an environmental or java 'rule'
that is stopping it working because there is a rule you are breaking.
The java behaviour is 'by design' you need to work out how to conform to
all the rules.

http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html

discusses the intricacies of the process and the bottom of the first
page describes the 'issue' you might be experiencing.

Of course the solution is yours to work out.

--
Mark

Mark Salter

unread,
Apr 1, 2009, 2:49:12 AM4/1/09
to jpos-...@googlegroups.com
Mark Salter wrote:
>
> Of course the solution is yours to work out.
>

...Because you have the environment and set-up in which the issue is
occurring.

I wanted to qualify my statement, it sounded too harsh.

8)


--
Mark

Reply all
Reply to author
Forward
0 new messages