Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] How to create J2SE applications with no persistence.xml files

Ben, 
    Hi, Tim is right, you will need a minimal persistence.xml file in your META-INF directory for some of the non-property attributes/elements.

    1) The examples that use programmatic run-time properties replace the need for the complex-type element <properties><property/>..</properties> in your persistence.xml.
    However you will still need to define the following design-time attributes/elements off the root element /persistence
        - persistence-unit/name
        - provider -  without the provider you may rely on the container to assign the first JPA provider it finds (for example GlassFish will default to TopLink Essentials)
        - persistence-unit/transaction-type and/or the jta/non-jta-datasource
        - class - if you are deploying in a non-EE environment
        - exclude-unlisted-classes if you don't want the container to pick up entities on the classpath=false, if you want the container to ignore any entities not defined with <class>=true
      
       If you were using your provider in SE mode and wanted to supply your properties at run-time then you could use one of the following methods
            http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManagerFactory.html#createEntityManager(java.util.Map)
            EntityManager createEntityManager(Map map)
      
            http://java.sun.com/javaee/5/docs/api/javax/persistence/Persistence.html#createEntityManagerFactory(java.lang.String,%20java.util.Map)
            public static javax.persistence.EntityManagerFactory createEntityManagerFactory(java.lang.String persistenceUnitName, java.util.Map properties);

    2) As you mentioned, you may want to refer to Doug's jpa.dynamic example which illustrates how to define both the properties and the entities dynamically - it still requires a minimum persistence.xml
       http://wiki.eclipse.org/EclipseLink/Source
       http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/org.eclipse.persistence.example.jpa.dynamic/
       http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/org.eclipse.persistence.example.jpa.dynamic/src/META-INF/persistence.xml
       http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/org.eclipse.persistence.example.jpa.dynamic/src/testing/PUWithoutXML.java      

    3) You can refer to p.343 of Chapter 11 Packaging and Deployment of "Pro EJB 3 Java Persistence API" where it states (for an EE environment)...
       "When defining a persistence unit in an EJB JAR, the persistence.xml file is not optional."
       Or refer the the EclipseLink User Guide.
                   http://wiki.eclipse.org/Packaging_and_Deploying_EclipseLink_JPA_Applications_(ELUG)#What_You_May_Need_to_Know_About_Persistence_Unit_Packaging_Options

    thank you
    /michael


Tim Hollosy wrote:
You're going to need a persistence.xml somewhere I think. What do you
mean you have a requirement you can't put an xml file in the
src/META-INF dir? That's a pretty bizarre requirement...
./tch



On Thu, Jan 29, 2009 at 11:26 PM, krisusa123 <krisusa123@xxxxxxxxx> wrote:
  
Hi Ben
I am trying to use this which was developed by Tim for his comics example.
But, even this one is looking for a persistence.xml file in my meta-inf
directory. Otherwise there is an error like this shown after Tim's code. The
error was produced by having a blank persistence.xml file. If I dont have a
blank file, the project is throwing errors and does not run.

Appreciate your help EclipseTeam!

properties.put(PersistenceUnitProperties.TARGET_DATABASE, "Derby");
  properties.put(PersistenceUnitProperties.JDBC_DRIVER,
    "org.apache.derby.jdbc.ClientDriver");
  properties.put(PersistenceUnitProperties.JDBC_URL,
    "jdbc:derby://localhost:1527/sample;create=true");
  properties.put(PersistenceUnitProperties.JDBC_USER, "app");
  properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "app");
  properties
    .put(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MIN, "1");
  properties.put(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MIN,
    "1");
  // properties.put(PersistenceUnitProperties.CACHE_SHARED_DEFAULT,
  // "true");
  properties.put(PersistenceUnitProperties.BATCH_WRITING, "JDBC");
  properties.put(PersistenceUnitProperties.CLASSLOADER,
ComicsEntityManagerFactory.class.getClassLoader());
  properties.put("eclipselink.logging.level", "FINE");
  properties.put("eclipselink.logging.timestamp", "false");
  properties.put("eclipselink.logging.session", "false");
  properties.put("eclipselink.logging.thread", "false");
  emf = new PersistenceProvider().createEntityManagerFactory("comics",
    properties);







Local Exception Stack:

Exception [EclipseLink-30005] (Eclipse Persistence Services - 1.0.2 (Build
20081024)):

org.eclipse.persistence.exceptions.PersistenceUnitLoadingException

Exception Description: An exception was thrown while searching for
persistence archives with ClassLoader:
sun.misc.Launcher$AppClassLoader@133056f

Internal Exception: Exception [EclipseLink-30004] (Eclipse Persistence
Services - 1.0.2 (Build 20081024)):

org.eclipse.persistence.exceptions.PersistenceUnitLoadingException

Exception Description: An exception was thrown while processing
persistence.xml from URL: file:/C:/workspace/Prototype/classes/

Internal Exception:

(1. Premature end of file.)

at
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(

PersistenceUnitLoadingException.java:121)

at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(

PersistenceProvider.java:117)

at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(

PersistenceProvider.java:64)

at com.xx.entitymanager.EntityManagerProcessor.createEntityManagerFactory(

EntityManagerProcessor.java:41)

Caused by: Exception [EclipseLink-30004] (Eclipse Persistence Services -
1.0.2 (Build 20081024)):
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException

Exception Description: An exception was thrown while processing
persistence.xml from URL: file:/C:/workspace/Prototype/classes/

Internal Exception:

(1. Premature end of file.)

at
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionProcessingPersistenceXML(

PersistenceUnitLoadingException.java:112)

at
org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceXML(

PersistenceUnitProcessor.java:365)

at
org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceArchive(

PersistenceUnitProcessor.java:314)

at
org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.getPersistenceUnits(

PersistenceUnitProcessor.java:237)

at
org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.initPersistenceUnits(

JPAInitializer.java:146)

at
org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.initialize(

JPAInitializer.java:135)

at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(

PersistenceProvider.java:104)
... 6 more

--- On Thu, 1/29/09, Ben Horowitz <horowitz2@xxxxxxxx> wrote:

From: Ben Horowitz <horowitz2@xxxxxxxx>
Subject: Re: [eclipselink-users] How to create J2SE applications with no
persistence.xml files
To: krisusa123@xxxxxxxxx
Date: Thursday, January 29, 2009, 7:43 PM

You might want to try this (I haven't tried it, but I keep it in mind in
case I need to use it):

org.eclipse.persistence.jpa.PersistenceProvider has a method
createContainerEntityManagerFactory that takes a PersistenceUnitInfo as an
argument (as well as a map of properties).

PersistenceUnitInfo is an interface that allows the specification of a
list of managed class names.

Good luck,
Ben


krisusa123 wrote:
    
Hi
I came into this rare problem of my app admins not ready to put files in
      
META-INF directory to load persistence file.
    
 I found this link from Doug explaining how to do this programatically.
      
But he still used it finally to specify the following:
    
 <persistence-unit name="unit-name">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
 Is there any way we can specify these also programatically.
 Reason: My persistence unit names change as per my environments and hence
      
the requirement.
    
 Appreciate your inputs.
 Thanks in advance.



------------------------------------------------------------------------

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https:// dev.eclipse.org/mailman/listinfo/eclipselink-users
      

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


    
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
  


Back to the top