How to get PostgreSQL to work with OSGi using EclipseLink

I need to develop an OSGi package with EclipseLink that should connect to PostgreSQL, but I spent a lot of time and couldn't get it to work. I get this message "javax.persistence.PersistenceException: No Persistence for EntityManager names".

Basically, I followed the instructions that I found on the Internet. I am using Eclipse Indigo

Manifest file : Manifest-Version: 1.0 Meta-Persistence: persistence.xml JPA-PersistenceUnits: rooms Bundle-ClassPath: ., META-INF/persistence.xml Bundle-ManifestVersion: 2 Bundle-Name: PMSTestTask Bundle-SymbolicName: PMSTestTask Bundle-Version: 1.0.0.qualifier Bundle-Activator: pmstesttask.Activator Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Import-Package: javax.persistence;version="2.0.3", org.osgi.framework;version="1.3.0", org.postgresql Require-Bundle: org.eclipse.persistence.jpa;bundle-version="2.1.0", org.eclipse.persistence.jpa.osgi;bundle-version="2.3.0" 

persistence.xml

  <?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="rooms" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>test_task.domain.Room</class> <properties> <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/Hotel_test_task" /> <property name="javax.persistence.jdbc.user" value="" /> <property name="javax.persistence.jdbc.password" value="" /> </properties> </persistence-unit> </persistence> 

Activator.java

  EntityManagerFactory emf = Persistence .createEntityManagerFactory("rooms"); EntityManager em = emf.createEntityManager(); 

Update 0

Added the required package for manifestation. And got this

 org.osgi.framework.BundleException: The bundle "org.eclipse.persistence.jpa.osgi_2.3.0.v20110604-r9504 [26]" could not be resolved. Reason: Missing Constraint: Require-Bundle: org.eclipse.persistence.core; bundle-version="2.3.0" 
+4
source share
1 answer

Since you create the EMF instance yourself (and do not use, for example, Enterprise OSGi to enter EMF), you must add the EclipseLink classes to the package class path, otherwise Java will not be able to instantiate the specified <provider> class that can go into this message about an error.

See the LazyLoadingRCP example, especially the classloader notes for the org.eclipse.persistence.example.jpa.rcp.comics package, or the specific MANIFEST.MF of this package (EclipseLink classes are included in the Require-Bundle ).

+1
source

Source: https://habr.com/ru/post/1396053/


All Articles