Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Gemini » Using Gemini DBAccess/JPA in services
Using Gemini DBAccess/JPA in services [message #989227] Wed, 05 December 2012 10:02 Go to next message
Ronald Joseph is currently offline Ronald JosephFriend
Messages: 10
Registered: December 2012
Junior Member
Hello,

I want to use Gemini DBAccess/JPA in a service.

For that I have created a plugin which will act as a service.

Dependencies as I have
 org.eclipse.gemini.jpa;bundle-version="1.1.0",
 org.eclipse.gemini.dbaccess.derby;bundle-version="1.1.0",
 org.apache.derby;bundle-version="10.5.1",
 com.hds.persistence.bundle;bundle-version="1.0.0",
 org.eclipse.gemini.ext.di;bundle-version="1.0.0"
.

In the META-INF/MANIFEST.MF file
Meta-Persistence: META-INF/persistence.xml
JPA-PersistenceUnits: hds

are registered.
The file META-INF/persistence.xml exists. The EMF also be created accordingly.

In my service implementation, I use the following to get a reference to the EentityManager:
@Inject 
@GeminiPersistenceContext (unitName = "hds")
protected EntityManager em;



In the file OSGI-INF/component.xml I've also added a reference to the EMF.
<reference name="EMF"
  interface="javax.persistence.EntityManagerFactory"
  cardinality="1..1"
  target="(osgi.unit.name=hdsPersistence)"
  policy="dynamic"/>


See http://www.eclipse.org/forums/index.php/m/837592/?srch=gemini+jpa+service#msg_837592.

When I start the service, however, is no EM was injected.

If I
@Inject 
@GeminiPersistenceContext (unitName = "hds")
protected EntityManager em;

for example, in a handler use, coupled with a CMD, the EM is injected correctly.

Can anyone tell me what needs to be done in order to use Gemini in a service?

The service is to be used in UI parts to encapsulate the access to the data.


Sorry for my English.

Greeting for Germany.
Ronald
Re: Using Gemini DBAccess/JPA in services [message #989328 is a reply to message #989227] Wed, 05 December 2012 16:21 Go to previous messageGo to next message
Michael Keith is currently offline Michael KeithFriend
Messages: 243
Registered: July 2009
Senior Member
Hi Ronald,

1) You should not have JPA-PersistenceUnits header in the manifest file. This is an obsolete EclipseLink header.

2) Are you using the code from Filipp in http://www.eclipse.org/forums/index.php/t/290891 ? He had gotten it working using custom annotations like @GeminiPersistenceContext.

-Mike
Re: Using Gemini DBAccess/JPA in services [message #989344 is a reply to message #989328] Wed, 05 December 2012 18:34 Go to previous messageGo to next message
Ronald Joseph is currently offline Ronald JosephFriend
Messages: 10
Registered: December 2012
Junior Member
Hello Mike,

thank you for your answer.

Quote:
1) You should not have JPA-PersistenceUnits header in the manifest file. This is an obsolete EclipseLink header.


Ok. Thanks.

Quote:
2) Are you using the code from Filipp in http://www.eclipse.org/forums/index.php/t/290891 ?


Are you sure that you really mean this thread? The Topic is "CDT on HP UX platform" from February 2004. I think no. Smile

- Ronald
Re: Using Gemini DBAccess/JPA in services [message #989354 is a reply to message #989344] Wed, 05 December 2012 19:42 Go to previous messageGo to next message
Michael Keith is currently offline Michael KeithFriend
Messages: 243
Registered: July 2009
Senior Member
Oops. Missing slash Smile

Try:
http://www.eclipse.org/forums/index.php/t/290891/
Re: Using Gemini DBAccess/JPA in services [message #989360 is a reply to message #989354] Wed, 05 December 2012 20:23 Go to previous messageGo to next message
Ronald Joseph is currently offline Ronald JosephFriend
Messages: 10
Registered: December 2012
Junior Member
Hello Mike,

thank you. A little evil / .... Smile

I'll try it tomorrow.

- Ronald
Re: Using Gemini DBAccess/JPA in services [message #989476 is a reply to message #989360] Thu, 06 December 2012 11:53 Go to previous messageGo to next message
Ronald Joseph is currently offline Ronald JosephFriend
Messages: 10
Registered: December 2012
Junior Member
Hello Mike,

it's exasperating. Crying or Very Sad

I have looked at the thread and think that it does not fit. It is the basis of Gemini.Ext.DI implementation. The way I see it, I can here all the possible values ​​that I can use in the persistence.xml.

I do not think it helps me more.


I want to try again to outline my problem.

First: I have a plugin that provides @Entity. It also contains the META-INF/persistence.xml and the matching entry in the MANIFEST.MF.

Second: I want to implement a plugin that provides services to the application (read, write, search for Entity, etc.). The interface of the application are provided so that this plugin interface methods.

Thirdly, when the activation of the service Plugings I would get the reference to the javax.persistence.EntityManager(Factory).

The first plugin (the one with the @Entity) is found when the application and it seems that the appropriate services are registered.

The second plugin I implemented a bundle Activator, which has a service tracker for javax.persistence.EntityManagerFactory. The method addingService (ServiceReference reference) returns the reference.
In the service implementation I have a method activate (Component context) implemented, the service searches for a reference, which I have set in the OSGI-INF/compoments.xml:

<reference name="EMF"
     interface="javax.persistence.EntityManagerFactory"
     cardinality="1..1"
     target="(osgi.unit.name=hdsPersistence)"
     policy="dynamic"/>


Here is a fragment from the Bundle-Activator:

 @Override
  public void start (BundleContext context) throws Exception {

    log.info ("HDS Service DBAccess starting");
    ctx = context;

    
    dsfTracker = new ServiceTracker (
      ctx, 
      EntityManagerFactory.class.getName (),
      this);
    dsfTracker.open ();
    log.info (
      "HDS Service DBAccess started ctx:=" + ctx + ", dsfTracker:=" + dsfTracker);
     
    log.info ("HDS Service DBAccess started");
   
  }


  @Override
  public Object addingService (ServiceReference reference) {
    
    log.info ("addingService (" + reference + ") ...");
    Bundle b = reference.getBundle();
    Object service = b.getBundleContext().getService(reference);
    String unitName = (String) reference.getProperty(EntityManagerFactoryBuilder.JPA_UNIT_NAME);
    
    log.info ("b:=" + b);
    log.info ("service:=" + service);
    log.info ("unitName:=" + unitName);
    
    return service;
  }



Here is a fragment from the Service-Activator:

  protected void activate (ComponentContext context) {
    System.out.println (getClass ().getName () + ".activate (" + context + ")...");
    
    Object obj = context.locateService ("EMF");
    System.out.println ("  locateService (\"EMF\"):=" + obj);
    
    if (obj instanceof EMFServiceProxyHandler) {  // <== ClassNotFoundException here
      em = (EntityManager) ((EMFServiceProxyHandler) obj).createEMF (null);
    }
    if (obj instanceof EntityManagerFactory) {
      em = ((EntityManagerFactory) obj).createEntityManager ();
    }
    System.out.println ("  em:=" + em);
    
    System.out.println (getClass ().getName () + ".activate (" + context + ") FINISHED");
  }


When I start the application I receive in this method a ClassNotFoundException (see mark).

I have somewhere a mistake? Or a trifle forgotten?

Is there a way, the EntityManager (Factory) in the activation of the service implementation to resolve somehow the OSGI framework?

If so how?

Greetings form Germany.
Ronald
Re: Using Gemini DBAccess/JPA in services [message #989560 is a reply to message #989227] Thu, 06 December 2012 17:55 Go to previous messageGo to next message
Nepomuk Seiler is currently offline Nepomuk SeilerFriend
Messages: 88
Registered: December 2010
Member
Hi,

There's is a missunderstanding of the gemini.ext.di plugin. It is design to inject EntityManager or EntityManagerFactory instances into e4 components.

If you want to use di in OSGi services, than use declarative services. This service can be injected via the normal e4 di mechanism, as e4 searches in the OSGi service registry for matching services.

cheers,
Muki
Re: Using Gemini DBAccess/JPA in services [message #989749 is a reply to message #989560] Fri, 07 December 2012 14:14 Go to previous messageGo to next message
Michael Keith is currently offline Michael KeithFriend
Messages: 243
Registered: July 2009
Senior Member
Hi Ronald,

I guessed that since you had @GeminiPersistenceContext in your code that you were already using e4 or had seen the e4 example from Filippe somewhere. It sounds like it was rather just by chance that you picked the same name, though. In any case, my understanding is that you can use DS with e4 if you want to.

For the CNF problem, the EMFServiceProxyHandler class is private to the impl, and the impl packages are not exported for applications to use. Why is it that you think you need to use it? You should only be accessing the EMF[Builder] and other public JPA and OSGi types.

-Mike
Re: Using Gemini DBAccess/JPA in services [message #989767 is a reply to message #989560] Fri, 07 December 2012 16:19 Go to previous message
Ronald Joseph is currently offline Ronald JosephFriend
Messages: 10
Registered: December 2012
Junior Member
Hello Muki,
Hello Mike,

@Muki
Quote:
If you want to use di in OSGi services, than use declarative services. This service can be injected via the normal e4 di mechanism, as e4 searches in the OSGi service registry for matching services.


That was the missing link. Smile

@Mike
Thanks for your help.

Have a nice weekend...

- Ronald
Previous Topic:Context initialisation failed
Next Topic:Gemini Web 2.2.0 RC1 is now available
Goto Forum:
  


Current Time: Thu Oct 10 21:31:55 GMT 2024

Powered by FUDForum. Page generated in 0.04471 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top