You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by bdh <bd...@yahoo.com> on 2008/03/26 16:46:07 UTC

Doing without Persistence.xml

I'm new to OpenJPA and have looked in the docs and have searched this forum.
Apologies if I missed other posting on this.  I'm working on an Eclipse RCP
application and would like to not bother with the persistence.xml file and
provide all the configuration through the properties provided to the
entitymanagerfactory.  

first question I have: is it possible to run without the persistence.xml
file?  

second, how do you specify the persistent entities you want openJPA to be
aware of?  I'm able to configure the database settings I want, but
specifying "class" and "foo.bar" doesn't seem to register it.  No
complaints, just doesn't do it.  Example:
props.put("class", "foo.Message");

I have the same question about setting the provider in the properties.  What
is the key value?

Thanks
bdh
-- 
View this message in context: http://www.nabble.com/Doing-without-Persistence.xml-tp16304021p16304021.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Doing without Persistence.xml

Posted by Brian H1 <bd...@yahoo.com>.
Hi Mike,

Thanks for this.  Adding the metadatafactory property has fixed running my
testcase as a JUnit test case.  I no longer need to add a runtime path
setting to the project directory that holds the META-INF directory (to find
the persistence.xml).  

I now have a new problem when running the testcase as a junit plugin test. 
I'll get to that in a sec.

As to your  question on whether I am dynamically or statically enhancing my
classes, the answer is neither.  I'm not enhancing at this time.  My
understanding from the documentation is that enhancement is optional.  I'm
willing to live with slower performance and no lazy load for now.  Do I
misunderstand that documentation, and is enhancement required?

As I said, the junit test works now.  I can rename the persistence.xml file
in the META-INF directory, and it runs fine.  When I run the test as a junit
plugin test, this is what the logging spits out:

0  WARN   [main] openjpa.Runtime - The property named "openjpa.Id" was not
recognized and will be ignored, although the name closely matches a valid
property called "openjpa.Id".
47  INFO   [main] openjpa.Runtime - Starting OpenJPA 1.0.1
1328  WARN   [main] openjpa.MetaData - The class "foo.Message" listed in the
openjpa.MetaDataFactory configuration property could not be loaded;
ignoring.
1422  WARN   [main] openjpa.MetaData - The class "foo.Message" listed in the
openjpa.MetaDataFactory configuration property could not be loaded;
ignoring.

And the testcase then fails due to the same "Attempt to cast
"foo.Message@4c47db" to PersistenceCapable failed.  Ensure..." as below.

So it looks like a class loading problem, which I would imagine I would have
had if I got the plugin testcase to find the persistence.xml file.  Have you
seen or anyone else seen this?

Thanks
Brian



Michael Dick wrote:
> 
> Hi Brian,
> 
> You can specify which entities are part of the persistence context with
> the
> following property :
> 
> openjpa.MetaDataFactory=jpa(Types=org.foo.Foo;org.foo.Bar)
> 
> The unit tests for OpenJPA use this mechanism instead of specifying
> classes
> in persistence.xml, the relevant code looks like this assuming types is a
> List containing your entity classes :
> 
> if (!types.isEmpty()) {
>             StringBuffer buf = new StringBuffer();
>             for (Class c : types) {
>                 if (buf.length() > 0)
>                     buf.append(";");
>                 buf.append(c.getName());
>             }
>             map.put("openjpa.MetaDataFactory",
>                 "jpa(Types=" + buf.toString() + ")");
> }
> 
> I don't know whether that alone will resolve your problem though. It looks
> like OpenJPA found the class, but it hasn't been enhanced. Are you using
> dynamic or static enhancement for your entities?
> 
> -Mike
> 
> On Thu, Mar 27, 2008 at 8:56 AM, Brian H1 <bd...@yahoo.com> wrote:
> 
>>
>> IRGeek,
>> You can specify the database configuration settings outside of the
>> persistence.xml file.  I'm doing that now.  You provide the
>> Persistence.createConfigurationFactory with the properties like below:
>>
>> props = new Properties();
>> props.put("openjpa.ConnectionURL",
>>
>>
>> "jdbc:sqlserver://localhost:1433;DatabaseName=OpenJPA_Test;selectMethod=cursor;create=true");
>> props.put("openjpa.ConnectionDriverName","
>> com.microsoft.sqlserver.jdbc.SQLServerDriver");
>> props.put("openjpa.ConnectionUserName", "sa");
>> props.put("openjpa.ConnectionPassword", "sa");
>> props.put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
>> props.put("openjpa.Log", "DefaultLevel=WARN, Runtime=INFO, Tool=INFO,
>> SQL=TRACE");
>> EntityManagerFactory factory =
>> Persistence.createEntityManagerFactory("haven", configurationProps);
>> this.entityMgr = factory.createEntityManager();
>>
>> My question is how do you set the entities to be persisted?  The entities
>> are not specified as properties in the persistence.xml file, but are
>> under
>> the "class" tag.
>>
>> I have the persistence.xml file sitting in the META_INF directory under
>> the
>> plugin project, where it's supposed to be.  The only I can get a
>> non-plugin
>> test case to run in eclipse is to specify the directory that contains the
>> META_INF directory (that is, the plugin project directory).  That's not
>> what
>> I want, because that only works then on my machine.  Then, when I run it
>> as
>> a plugin testcase, it again doesn't work.  I get the
>> <openjpa-1.0.1-r420667:592145 nonfatal user error>
>> org.apache.openjpa.persistence.ArgumentException: Attempt to cast
>> instance
>> "foo.Message@4c47db" to PersistenceCapable failed.  Ensure that it has
>> been
>> enhanced.
>> FailedObject: foo.Message@4c47db
>>        at
>> org.apache.openjpa.kernel.BrokerImpl.assertPersistenceCapable(
>> BrokerImpl.java:4285)
>>        at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java
>> :2364)
>>        at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java
>> :2224)
>>        at
>> org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java
>> :1005)
>>        at
>> org.apache.openjpa.persistence.EntityManagerImpl.persist(
>> EntityManagerImpl.java:541)
>>        at foo.HavenSession.save(HavenSession.java:38)
>>        at foo.HavenSessionTest.testSaveMessage(HavenSessionTest.java:30)
>>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>        at java.lang.reflect.Method.invoke(Unknown Source)
>>        at junit.framework.TestCase.runTest(TestCase.java:164)
>>        at junit.framework.TestCase.runBare(TestCase.java:130)
>>        at junit.framework.TestResult$1.protect(TestResult.java:106)
>>        at junit.framework.TestResult.runProtected(TestResult.java:124)
>>        at junit.framework.TestResult.run(TestResult.java:109)
>>        at junit.framework.TestCase.run(TestCase.java:120)
>>        at junit.framework.TestSuite.runTest(TestSuite.java:230)
>>        at junit.framework.TestSuite.run(TestSuite.java:225)
>>        at
>> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(
>> JUnit3TestReference.java:130)
>>        at
>> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java
>> :38)
>>        at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
>> RemoteTestRunner.java:460)
>>        at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
>> RemoteTestRunner.java:673)
>>        at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
>> RemoteTestRunner.java:386)
>>        at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
>> RemoteTestRunner.java:196)
>>
>> It appears that it can't find the persistence.xml file.  I either need to
>> find out how to get eclipse to find it, or get rid of it.
>>
>> Thanks for any help you can provide
>> Brian
>>
>>
>>
>>
>> IRGeek wrote:
>> >
>> >
>> >
>> > bdh wrote:
>> >>
>> >> first question I have: is it possible to run without the
>> persistence.xml
>> >> file?
>> >>
>> >
>> > I have the same question. I have had great success using JPA with my
>> web
>> > apps where I can easily control the backend. However, for the swing app
>> I
>> > am writing now the user needs to be able to specify the database
>> server,
>> > user name, password when they install the application. Expecting to
>> modify
>> > persistence.xml and update in the installed JAR file simply isn't
>> > feasible. There has to be a semi-elegant way to specify the database
>> > access information external to persistence.xml.
>> >
>> > thx for any info anyone can provide!
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Doing-without-Persistence.xml-tp16304021p16325035.html
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/Doing-without-Persistence.xml-tp16304021p16349671.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Doing without Persistence.xml

Posted by Michael Dick <mi...@gmail.com>.
Hi Brian,

You can specify which entities are part of the persistence context with the
following property :

openjpa.MetaDataFactory=jpa(Types=org.foo.Foo;org.foo.Bar)

The unit tests for OpenJPA use this mechanism instead of specifying classes
in persistence.xml, the relevant code looks like this assuming types is a
List containing your entity classes :

if (!types.isEmpty()) {
            StringBuffer buf = new StringBuffer();
            for (Class c : types) {
                if (buf.length() > 0)
                    buf.append(";");
                buf.append(c.getName());
            }
            map.put("openjpa.MetaDataFactory",
                "jpa(Types=" + buf.toString() + ")");
}

I don't know whether that alone will resolve your problem though. It looks
like OpenJPA found the class, but it hasn't been enhanced. Are you using
dynamic or static enhancement for your entities?

-Mike

On Thu, Mar 27, 2008 at 8:56 AM, Brian H1 <bd...@yahoo.com> wrote:

>
> IRGeek,
> You can specify the database configuration settings outside of the
> persistence.xml file.  I'm doing that now.  You provide the
> Persistence.createConfigurationFactory with the properties like below:
>
> props = new Properties();
> props.put("openjpa.ConnectionURL",
>
>
> "jdbc:sqlserver://localhost:1433;DatabaseName=OpenJPA_Test;selectMethod=cursor;create=true");
> props.put("openjpa.ConnectionDriverName","
> com.microsoft.sqlserver.jdbc.SQLServerDriver");
> props.put("openjpa.ConnectionUserName", "sa");
> props.put("openjpa.ConnectionPassword", "sa");
> props.put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
> props.put("openjpa.Log", "DefaultLevel=WARN, Runtime=INFO, Tool=INFO,
> SQL=TRACE");
> EntityManagerFactory factory =
> Persistence.createEntityManagerFactory("haven", configurationProps);
> this.entityMgr = factory.createEntityManager();
>
> My question is how do you set the entities to be persisted?  The entities
> are not specified as properties in the persistence.xml file, but are under
> the "class" tag.
>
> I have the persistence.xml file sitting in the META_INF directory under
> the
> plugin project, where it's supposed to be.  The only I can get a
> non-plugin
> test case to run in eclipse is to specify the directory that contains the
> META_INF directory (that is, the plugin project directory).  That's not
> what
> I want, because that only works then on my machine.  Then, when I run it
> as
> a plugin testcase, it again doesn't work.  I get the
> <openjpa-1.0.1-r420667:592145 nonfatal user error>
> org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance
> "foo.Message@4c47db" to PersistenceCapable failed.  Ensure that it has
> been
> enhanced.
> FailedObject: foo.Message@4c47db
>        at
> org.apache.openjpa.kernel.BrokerImpl.assertPersistenceCapable(
> BrokerImpl.java:4285)
>        at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java
> :2364)
>        at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java
> :2224)
>        at
> org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java
> :1005)
>        at
> org.apache.openjpa.persistence.EntityManagerImpl.persist(
> EntityManagerImpl.java:541)
>        at foo.HavenSession.save(HavenSession.java:38)
>        at foo.HavenSessionTest.testSaveMessage(HavenSessionTest.java:30)
>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at junit.framework.TestCase.runTest(TestCase.java:164)
>        at junit.framework.TestCase.runBare(TestCase.java:130)
>        at junit.framework.TestResult$1.protect(TestResult.java:106)
>        at junit.framework.TestResult.runProtected(TestResult.java:124)
>        at junit.framework.TestResult.run(TestResult.java:109)
>        at junit.framework.TestCase.run(TestCase.java:120)
>        at junit.framework.TestSuite.runTest(TestSuite.java:230)
>        at junit.framework.TestSuite.run(TestSuite.java:225)
>        at
> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(
> JUnit3TestReference.java:130)
>        at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java
> :38)
>        at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> RemoteTestRunner.java:460)
>        at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> RemoteTestRunner.java:673)
>        at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
> RemoteTestRunner.java:386)
>        at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
> RemoteTestRunner.java:196)
>
> It appears that it can't find the persistence.xml file.  I either need to
> find out how to get eclipse to find it, or get rid of it.
>
> Thanks for any help you can provide
> Brian
>
>
>
>
> IRGeek wrote:
> >
> >
> >
> > bdh wrote:
> >>
> >> first question I have: is it possible to run without the
> persistence.xml
> >> file?
> >>
> >
> > I have the same question. I have had great success using JPA with my web
> > apps where I can easily control the backend. However, for the swing app
> I
> > am writing now the user needs to be able to specify the database server,
> > user name, password when they install the application. Expecting to
> modify
> > persistence.xml and update in the installed JAR file simply isn't
> > feasible. There has to be a semi-elegant way to specify the database
> > access information external to persistence.xml.
> >
> > thx for any info anyone can provide!
> >
>
> --
> View this message in context:
> http://www.nabble.com/Doing-without-Persistence.xml-tp16304021p16325035.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Re: Doing without Persistence.xml

Posted by Brian H1 <bd...@yahoo.com>.
IRGeek,
You can specify the database configuration settings outside of the
persistence.xml file.  I'm doing that now.  You provide the
Persistence.createConfigurationFactory with the properties like below:

props = new Properties();
props.put("openjpa.ConnectionURL", 
        
"jdbc:sqlserver://localhost:1433;DatabaseName=OpenJPA_Test;selectMethod=cursor;create=true");
props.put("openjpa.ConnectionDriverName","com.microsoft.sqlserver.jdbc.SQLServerDriver");
props.put("openjpa.ConnectionUserName", "sa");
props.put("openjpa.ConnectionPassword", "sa");
props.put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
props.put("openjpa.Log", "DefaultLevel=WARN, Runtime=INFO, Tool=INFO,
SQL=TRACE");
EntityManagerFactory factory =
Persistence.createEntityManagerFactory("haven", configurationProps);
this.entityMgr = factory.createEntityManager();

My question is how do you set the entities to be persisted?  The entities
are not specified as properties in the persistence.xml file, but are under
the "class" tag.

I have the persistence.xml file sitting in the META_INF directory under the
plugin project, where it's supposed to be.  The only I can get a non-plugin
test case to run in eclipse is to specify the directory that contains the
META_INF directory (that is, the plugin project directory).  That's not what
I want, because that only works then on my machine.  Then, when I run it as
a plugin testcase, it again doesn't work.  I get the 
<openjpa-1.0.1-r420667:592145 nonfatal user error>
org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance
"foo.Message@4c47db" to PersistenceCapable failed.  Ensure that it has been
enhanced.
FailedObject: foo.Message@4c47db
	at
org.apache.openjpa.kernel.BrokerImpl.assertPersistenceCapable(BrokerImpl.java:4285)
	at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2364)
	at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2224)
	at
org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1005)
	at
org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:541)
	at foo.HavenSession.save(HavenSession.java:38)
	at foo.HavenSessionTest.testSaveMessage(HavenSessionTest.java:30)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at junit.framework.TestCase.runTest(TestCase.java:164)
	at junit.framework.TestCase.runBare(TestCase.java:130)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:120)
	at junit.framework.TestSuite.runTest(TestSuite.java:230)
	at junit.framework.TestSuite.run(TestSuite.java:225)
	at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
	at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

It appears that it can't find the persistence.xml file.  I either need to
find out how to get eclipse to find it, or get rid of it.

Thanks for any help you can provide
Brian




IRGeek wrote:
> 
> 
> 
> bdh wrote:
>> 
>> first question I have: is it possible to run without the persistence.xml
>> file?  
>> 
> 
> I have the same question. I have had great success using JPA with my web
> apps where I can easily control the backend. However, for the swing app I
> am writing now the user needs to be able to specify the database server,
> user name, password when they install the application. Expecting to modify
> persistence.xml and update in the installed JAR file simply isn't
> feasible. There has to be a semi-elegant way to specify the database
> access information external to persistence.xml.
> 
> thx for any info anyone can provide!
> 

-- 
View this message in context: http://www.nabble.com/Doing-without-Persistence.xml-tp16304021p16325035.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Doing without Persistence.xml

Posted by IRGeek <ky...@yahoo.com>.


bdh wrote:
> 
> first question I have: is it possible to run without the persistence.xml
> file?  
> 

I have the same question. I have had great success using JPA with my web
apps where I can easily control the backend. However, for the swing app I am
writing now the user needs to be able to specify the database server, user
name, password when they install the application. Expecting to modify
persistence.xml and update in the installed JAR file simply isn't feasible.
There has to be a semi-elegant way to specify the database access
information external to persistence.xml.

thx for any info anyone can provide!
-- 
View this message in context: http://www.nabble.com/Doing-without-Persistence.xml-tp16304021p16321059.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.