This discussion is archived
2 Replies Latest reply: Feb 28, 2009 10:45 AM by 843833 RSS

JPA: How to fetch primary key dynamically

843833 Newbie
Currently Being Moderated
Hello,

I have been working with JPA from past few months.
Recently, I am involved in writing EJB3 statless bean, Interceptor & JPA.

Before manager's (statless bean's) method gets invoked, I am intercepting it through interceptor.

In my manager method, I am annotating some arguments (entities) as
@RequiresPersistenceCheck
a custom annotation.

In my interceptor, i need to check if that argument (entity) exists in Data Base or not.

For normal entites, i know, i can perform it like this:
 @Entity
public class Product {
                        
     @Id
     public long id;

     public String name;

     // getters n setters here
}

@Stateless         
public class MyManager {

     @PersistenceContext(unitName=some_unit)
     javax.persistence.EntityManager em;
                     
     public void createItem(String name, Product p) {
          /*
                  First I need to check, if product exists or not
          */
          Product p_db = null;
          
         if(p != null) {   
                 p_db = em.find(Product.class, p.getId()); 
         }

          if(p_db == null) { 
                 // throw some Business exception
          }

          //create an item here
     }
}
But , I have more than 100 methods spread across some 10 - 15 managers ( with 30- 40 entities ) !_
+& I feel this will surely be a cross cutting concern ...+

I have decided to go like this :
@Stateless
@Interceptors(MyInterceptor.class)         
public class MyManager {

     @PersistenceContext(unitName=some_unit)
     javax.persistence.EntityManager em;
                     
     public void createItem(String name, @RequiresPersistenceCheck Product p) {
          //create an item here 
     }
}
In My interceptor though, I get only
java.lang.Object
and i need to check if that object (which is actually entity) actually exists in a data base or not
public class MyInterceptor {

    @PersistenceContext(unitName=some_unit)
     javax.persistence.EntityManager em;

     @AroundInvoke  
     public Object intercept(InnvocationContext context) throws Exception {
            //perform some logic here with reflection API
     }


    private boolean isEntityExist(Object entity) {

         Class c = entity.getClass();
         /**
          * How to fetch primary Key from entity (object) ????????????? 
          *   All entites may not have primary key as 'id'
          */
         Object primary_key = null;

         Object entity_db = em.find(c, primary_key);
        
         return entity_db != null;
   }
}
Hope I am clear with my requirement ...

My questions are following: Is there a way in JPA API,

1) To check if the
java.lang.Object
is an entity or not

2) To fetch the primary key dynamically from
java.lang.Object
as
java.lang.Object
3) If above 2 do not exist, a way to check if entity exists in DB, in some other manner, which solves my requirement

Thanks a lottt

Thanks in advance .....

Waiting for your valuable response ...

Regds,

Vikas