-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: JPA and CRUD generalization
PostPosted: Thu Jul 17, 2008 12:09 am 
Newbie

Joined: Fri Mar 23, 2007 9:46 pm
Posts: 6
Location: Sao Paulo, Brazil
Hello,

JPA allows CRUD code to be written only once; in other words, you can now avoid the huge amount of code duplication caused by the proliferation of DAOs. That's why I'm using the code below (CRUDService.java) as a base class for services built on domain objects. The code has been working fine so far. I'm able to use it as follows, for example:

Code:
Job flightAtt = new Job();
...
SERVICE.create(flightAtt);
...
Runway r = (Runway) SERVICE.read(Runway.class, 2);
...
Employee e = (Employee) SERVICE.read(Employee.class, 4);
...
SERVICE.update(e);
...
SERVICE.delete(r);


SERVICE is a Singleton instance of CRUDService (or its subclasses). Still, when I compile the code I get the following warnings:

CRUDService.java:43: warning: [unchecked] unchecked conversion
found : java.lang.Class
required: java.lang.Class<T>
return em.find(type, id);

CRUDService.java:43: warning: [unchecked] unchecked method invocation: <T>find(java.lang.Class<T>,java.lang.Object) in javax.persistence.EntityManager is applied to (java.lang.Class,java.lang.Object)
return em.find(type, id);

As far as I can see, they are complaining that I'm not using generics in the read() method. Even though they are just warnings, would someone please take a look at the code and criticize it? The code is very short and simple.

Code:
public class CRUDService {
   
    protected static final Logger LOGGER = Logger.
        getLogger(Logger.GLOBAL_LOGGER_NAME);
    protected EntityManager em;
    protected EntityTransaction tx;

    private enum TransactionType { CREATE, UPDATE, DELETE };
   
    protected CRUDService(String persistenceUnit) {
        em = Persistence.createEntityManagerFactory(persistenceUnit).
            createEntityManager();
        tx = em.getTransaction();
    }

    public boolean create(Object obj) {
        return execTransaction(obj, TransactionType.CREATE);
    }

    public Object read(Class type, Object id) {
        return em.find(type, id);
    }
   
    public boolean update(Object obj) {
        return execTransaction(obj, TransactionType.UPDATE);
    }
   
    public boolean delete(Object obj) {
        return execTransaction(obj, TransactionType.DELETE);
    }
   
    private boolean execTransaction(Object obj, TransactionType txType) {
        try {
            tx.begin();
            if (txType.equals(TransactionType.CREATE))
                em.persist(obj);
            else if (txType.equals(TransactionType.UPDATE))
                em.merge(obj);
            else if (txType.equals(TransactionType.DELETE))
                em.remove(obj);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                LOGGER.severe(txType + " FAILED: ROLLING BACK!");
                tx.rollback();
                return false;
            } else {
                LOGGER.info(txType + " SUCCESSFUL.");
                return true;
            }
        }
    }
}


Thanks so much,

Cristina


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 19, 2008 10:23 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This is all it is, the JVM wanting you to use generics and explicitly state the type.

The @Supresswarnings annotation is a wonderful thing. You should use it!

;)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 12:05 am 
Newbie

Joined: Fri Mar 23, 2007 9:46 pm
Posts: 6
Location: Sao Paulo, Brazil
Hi Cameron,

thanks for your reply. Still, wouldn't you let us know your opinion about the code? Your site offers, among other things, many tutorials on DAO development... So, do you agree with me when I say that JPA allows CRUD code to be written only once, thus avoiding the code duplication caused by the proliferation of DAOs?

Thanks again,

Cristina


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.