You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by KK <ka...@persistent.co.in> on 2008/08/13 11:07:41 UTC

Possible reasons for Persistence.createEntityManagerFactory returning null

Hi,
I am creating a stand alone java program with OpenJPA. I have placed my
persistence.xml in bin\META-INF\. But when I do
Persistence.createEntityManagerFactory, it is returning null. When I used
the same folder structure with Toplink JPA, it is working fine.

The method is just returning null and there is no exception which can
indicate the possible reasons. Please tell me what can be the possible
reasons for the Persistence.createEntityManagerFactory("persistence-unit
name") method to return null.

Regards,
KK

My persistence.xml is as given below:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
	<persistence-unit name="ibm" transaction-type="RESOURCE_LOCAL">
		<provider>
			org.apache.openjpa.persistence.PersistenceProviderImpl
		</provider>
		<mapping-file>mapping/orm.xml</mapping-file>
		<class>com.entity.Customer</class>
		<class>com.entity.Organization</class>
		<class>com.BillingAddress</class>
		<properties>
			<property name="openjpa.Log"
				value="DefaultLevel=WARN, Tool=INFO" />
			<property name="openjpa.ConnectionURL"
				value="jdbc:mysql://localhost:3306/wdp_jpa" />
			<property name="openjpa.ConnectionUserName" value="root" />
			<property name="openjpa.ConnectionDriverName"
				value="com.mysql.jdbc.Driver" />
			<property name="openjpa.ConnectionPassword" value="admin" />
			<property name="openjpa.jdbc.SynchronizeMappings"
				value="buildSchema(SchemaAction=add,deleteTableContents)" />
		</properties>

	</persistence-unit>
</persistence>

-- 
View this message in context: http://n2.nabble.com/Possible-reasons-for-Persistence.createEntityManagerFactory-returning-null-tp721529p721529.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Possible reasons for Persistence.createEntityManagerFactory returning null

Posted by Kevin Sutter <kw...@gmail.com>.
KK,
As you have already surmised, the most common reason for returning null is
when the requested EMF can't be found using your classpath and packaging.
You don't mention this, but I'm assuming your createEMF invocation is like
this (based on your persistence.xml):

Persistence.createEntityManagerFactory("ibm");

The other possibility is that you don't have openjpa properly "installed".
If the JPA Persistence class can't find the provider that you mentioned in
your persistence.xml, then you might get a null returned.  (I can't remember
exactly how the Persistence class processes this case.)

You might want to try turning on TRACE for openjpa and see if we are getting
involved in the processing at all.  If we are, then this might help explain
why the EMF couldn't be created.

The JPA spec outlines the requirements for the location of the
persistence.xml file.  It's very explicit for the EE packaging
requirements.  For the SE environment, it just states that the
META-INF/persistence.xml must be found at the root of the persistence unit.

Hope this information helps.
Kevin
On Wed, Aug 13, 2008 at 4:07 AM, KK <ka...@persistent.co.in> wrote:

>
> Hi,
> I am creating a stand alone java program with OpenJPA. I have placed my
> persistence.xml in bin\META-INF\. But when I do
> Persistence.createEntityManagerFactory, it is returning null. When I used
> the same folder structure with Toplink JPA, it is working fine.
>
> The method is just returning null and there is no exception which can
> indicate the possible reasons. Please tell me what can be the possible
> reasons for the Persistence.createEntityManagerFactory("persistence-unit
> name") method to return null.
>
> Regards,
> KK
>
> My persistence.xml is as given below:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence"
>        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> version="1.0">
>        <persistence-unit name="ibm" transaction-type="RESOURCE_LOCAL">
>                <provider>
>
>  org.apache.openjpa.persistence.PersistenceProviderImpl
>                </provider>
>                <mapping-file>mapping/orm.xml</mapping-file>
>                <class>com.entity.Customer</class>
>                <class>com.entity.Organization</class>
>                <class>com.BillingAddress</class>
>                <properties>
>                        <property name="openjpa.Log"
>                                value="DefaultLevel=WARN, Tool=INFO" />
>                        <property name="openjpa.ConnectionURL"
>                                value="jdbc:mysql://localhost:3306/wdp_jpa"
> />
>                        <property name="openjpa.ConnectionUserName"
> value="root" />
>                        <property name="openjpa.ConnectionDriverName"
>                                value="com.mysql.jdbc.Driver" />
>                        <property name="openjpa.ConnectionPassword"
> value="admin" />
>                        <property name="openjpa.jdbc.SynchronizeMappings"
>
>  value="buildSchema(SchemaAction=add,deleteTableContents)" />
>                </properties>
>
>        </persistence-unit>
> </persistence>
>
> --
> View this message in context:
> http://n2.nabble.com/Possible-reasons-for-Persistence.createEntityManagerFactory-returning-null-tp721529p721529.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Re: Possible reasons for Persistence.createEntityManagerFactory returning null

Posted by BluesBrothers <jo...@objectivelogic.com>.
Heiko,

I'm also using 1.2.1, which is the latest stable release (1.3.x is  
available as a snapshot, and 2.x is in the wings).

Yes, as nearly as I can tell, the absence of the OpenJPA JAR and the  
class(es) it contains causes a createEntityManagerFactory() call to be  
short-circuited and simply return null, rather than throwing an  
exception or attempting to log a message.

I agree: surely they could have at least thrown a runtime exception of  
some sort -- they could have at least given the hapless user some  
useful feedback. I think this is a bug, but apparently, it's been this  
way for a long time, and I doubt they're interested in addressing the  
problem.

If one had the time and inclination, one could of course open up the  
source code and find out "why" it's the way it is. I'm just thankful  
to have gotten past it! ;-)

Thanks for the feedback.



John

Mac: It just works.

"If there are no dogs in Heaven, then when I die I want to go where  
they went."  Will Rogers

"The democracy will cease to exist when you take from those who are  
willing to work and give to those who would not."  Thomas Jefferson

John N. Armstrong
Sr. Partner
Objective Logic, L.L.P.
3505 Terry Drive
Plano, TX 75023
john@objectivelogic.com
Office:  972-208-0032
Mobile: 972-529-0195
www.objectivelogic.com

On Nov 12, 2009, at 1:36 AM, Heiko [via OpenJPA] wrote:

> Hello,
>
> this problem even occurs if you try to create a factory with a  
> persistence
> unit that is not listed in the persistence.xml. I once had the problem
> that I called my persistent unit name 'Foo', and by mistake used 'FOO'
> when creating the factory.
>
> Again, this is silently ignored and null is returned. In such a severe
> thing, I'd prefer to have an Exception being thrown. Has this  
> changed in
> newer releases? Currently, I'm using 1.2.1.
>
> Regards,
>
> Heiko
>
> -- 
> Dr.-Ing. Heiko Kopp
>
> [hidden email] schrieb am 12.11.2009 04:20:34:
>
> >
> > KK,
> >
> > I realize that this thread is over a year old, but I had to thank  
> you
> for
> > posting the resolution to your problem. I had the same issue
> (createEMF()
> > silently returned null) and had searched for a long time before
> happening
> > upon this thread. I did the same thing: I overlooked the obvious:  
> the
> > openjpa.jar file is located in a top-level directory above the other
> > dependent jars, and I had neglected to copy it to my lib/ folder.
> Without
> > any exception or messages to go by, I was completely stumped as to  
> why
> > OpenJPA seemed to ignore my persistence.xml file and return null  
> for the
> > EMF. Anyway, I found and copied the missing JAR file, and voila! I  
> put
> an
> > end to several hours of wasted time. ;-)
> >
> >
> > Kamlesh123 wrote:
> > >
> > > Thanks Kevin,
> > >
> > > Sorry for replying late. The emf was coming as null because I  
> have not
> set
> > > the jar openjpa-1.1.0.jar in the classpath. Actually in OpenJPA
> > > distribution there is a lib folder containg some other jars. I  
> have
> > > assumed that all the required libraries are in the lib folder  
> and did
> not
>
> > > bother to see that there is one more jar lying on the root.
> > >
> > > Regards,
> > > KK
> > >
> >
> > --
> > View this message in context: http://n2.nabble.com/Possible-reasons-
> > for-Persistence-createEntityManagerFactory-returning-null-
> > tp721529p3990562.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>
> If you are not the intended addressee, please inform us immediately  
> that you have received this e-mail in error, and delete it. We thank  
> you for your cooperation.
>
> View message @ http://n2.nabble.com/Possible-reasons-for-Persistence-createEntityManagerFactory-returning-null-tp721529p3991184.html
> To unsubscribe from Re: Possible reasons for  
> Persistence.createEntityManagerFactory returning null, click here.
>


-- 
View this message in context: http://n2.nabble.com/Possible-reasons-for-Persistence-createEntityManagerFactory-returning-null-tp721529p3991256.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Re: Possible reasons for Persistence.createEntityManagerFactory returning null

Posted by it...@daimler.com.
Hello,

this problem even occurs if you try to create a factory with a persistence 
unit that is not listed in the persistence.xml. I once had the problem 
that I called my persistent unit name 'Foo', and by mistake used 'FOO' 
when creating the factory.

Again, this is silently ignored and null is returned. In such a severe 
thing, I'd prefer to have an Exception being thrown. Has this changed in 
newer releases? Currently, I'm using 1.2.1.

Regards,

Heiko

--
Dr.-Ing. Heiko Kopp

john@objectivelogic.com schrieb am 12.11.2009 04:20:34:

> 
> KK,
> 
> I realize that this thread is over a year old, but I had to thank you 
for
> posting the resolution to your problem. I had the same issue 
(createEMF()
> silently returned null) and had searched for a long time before 
happening
> upon this thread. I did the same thing: I overlooked the obvious: the
> openjpa.jar file is located in a top-level directory above the other
> dependent jars, and I had neglected to copy it to my lib/ folder. 
Without
> any exception or messages to go by, I was completely stumped as to why
> OpenJPA seemed to ignore my persistence.xml file and return null for the
> EMF. Anyway, I found and copied the missing JAR file, and voila! I put 
an
> end to several hours of wasted time. ;-)
> 
> 
> Kamlesh123 wrote:
> > 
> > Thanks Kevin,
> > 
> > Sorry for replying late. The emf was coming as null because I have not 
set
> > the jar openjpa-1.1.0.jar in the classpath. Actually in OpenJPA
> > distribution there is a lib folder containg some other jars. I have
> > assumed that all the required libraries are in the lib folder and did 
not
> > bother to see that there is one more jar lying on the root.
> > 
> > Regards,
> > KK
> > 
> 
> -- 
> View this message in context: http://n2.nabble.com/Possible-reasons-
> for-Persistence-createEntityManagerFactory-returning-null-
> tp721529p3990562.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.


If you are not the intended addressee, please inform us immediately that you have received this e-mail in error, and delete it. We thank you for your cooperation.  

Re: Possible reasons for Persistence.createEntityManagerFactory returning null

Posted by BluesBrothers <jo...@objectivelogic.com>.
KK,

I realize that this thread is over a year old, but I had to thank you for
posting the resolution to your problem. I had the same issue (createEMF()
silently returned null) and had searched for a long time before happening
upon this thread. I did the same thing: I overlooked the obvious: the
openjpa.jar file is located in a top-level directory above the other
dependent jars, and I had neglected to copy it to my lib/ folder. Without
any exception or messages to go by, I was completely stumped as to why
OpenJPA seemed to ignore my persistence.xml file and return null for the
EMF. Anyway, I found and copied the missing JAR file, and voila! I put an
end to several hours of wasted time. ;-)


Kamlesh123 wrote:
> 
> Thanks Kevin,
> 
> Sorry for replying late. The emf was coming as null because I have not set
> the jar openjpa-1.1.0.jar in the classpath. Actually in OpenJPA
> distribution there is a lib folder containg some other jars. I have
> assumed that all the required libraries are in the lib folder and did not
> bother to see that there is one more jar lying on the root.
> 
> Regards,
> KK
> 

-- 
View this message in context: http://n2.nabble.com/Possible-reasons-for-Persistence-createEntityManagerFactory-returning-null-tp721529p3990562.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Possible reasons for Persistence.createEntityManagerFactory returning null

Posted by KK <ka...@persistent.co.in>.
Thanks Kevin,

Sorry for replying late. The emf was coming as null because I have not set
the jar openjpa-1.1.0.jar in the classpath. Actually in OpenJPA distribution
there is a lib folder containg some other jars. I have assumed that all the
required libraries are in the lib folder and did not bother to see that
there is one more jar lying on the root.

Regards,
KK
-- 
View this message in context: http://n2.nabble.com/Possible-reasons-for-Persistence.createEntityManagerFactory-returning-null-tp721529p735684.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.