« Return to Thread: Code generation tool for persitence layer (from database)

Re: Code generation tool for persitence layer (from database)

by tch :: Rate this Message:

Reply to Author | View in Thread

Well, already by using an ORM like EclipseLink you don't have to write
SQL to do deletes/updates/inserts right?

So what code are you trying to generate for DAO's? a call to
entityManager.merge?

Your DAO is independent of any specific entities, so you don't need to
make 30 different implementations of it.

Here's an example of a SimpleDAO that I use on it's own or extend if I
need a special one.

(Ignore Spring annotations, and you may have to change you you obtain
your entity manager if you're not using spring dependency
injection...)



import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;

import org.eclipse.persistence.jpa.JpaEntityManager;
import org.eclipse.persistence.jpa.JpaHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repository
public class SimpleDao {
       
        @PersistenceContext
        private EntityManager entityManager;


        public EntityManager getEntityManager()
        {
                return entityManager;
        }
       
        @Transactional(propagation=Propagation.REQUIRED)
        public void insert(Object entity)
        {
                entityManager.persist(entity);
        }
       
        @Transactional(propagation=Propagation.REQUIRED)
        public <T> T save(T entity)
        {
                return entityManager.merge(entity);
        }
       
        @Transactional(propagation=Propagation.REQUIRED)
        public void delete(Object entity)
        {
                entityManager.remove(entity);
        }
       
        @Transactional(readOnly = true)
        public <T> T find(Class<T> entityClass, Object primaryKey) {
                return entityManager.find(entityClass, primaryKey);
        }


        @Transactional(readOnly = true)
        public <T> List<T> selectAll(Class<T> clazz) {
                return (List<T>)
JpaHelper.getEntityManager(entityManager).createQuery(null,
clazz).getResultList();
               
                JpaHelper.getEntityManager(entityManager).createQuery(null, clazz).set
        }
       


}



./tch



On Mon, Nov 24, 2008 at 9:18 AM, Chiovari Cristian Sergiu
<csergiu77@...> wrote:

> Sorry for asking you again:
>
> What do you mean by :"Code
>> gen seems like the wrong solution for DAO's.
>> "?
>
> Thx again
>
>
> --- On Mon, 11/24/08, Tim Hollosy <hollosyt@...> wrote:
>
>> From: Tim Hollosy <hollosyt@...>
>> Subject: Re: [eclipselink-users] Code generation tool for persitence layer (from database)
>> To: "EclipseLink User Discussions" <eclipselink-users@...>
>> Date: Monday, November 24, 2008, 4:15 PM
>> Why don't you just write your DAO once and extend it
>> when needed? Code
>> gen seems like the wrong solution for DAO's.
>>
>> ./tch
>>
>>
>>
>> On Mon, Nov 24, 2008 at 9:04 AM, csergiu77
>> <csergiu77@...> wrote:
>> >
>> >
>> > Thx.
>> >
>> > That is what i want to avoid :) (i'm lazy in write
>> code that repeats in
>> > logic and structure)
>> >
>> > There is something on the market called Firestorm DAO
>> but I wondered if
>> > there is no other magic toold that code all simple
>> persitance layer from
>> > database (only simple insert,update,delete,findAll) ?
>> >
>> >
>> >
>> >
>> >
>> > Tim Hollosy wrote:
>> >>
>> >> Well for JPA in Eclipse there is Dali (JPA Tools).
>> >>
>> >> After that you don't have to write code to
>> make inserts/updates/deletes :)
>> >>
>> >> Though, many people have a common "DAO"
>> class for selectAll, etc. That
>> >> really depends on the framework you're using
>> though -- how you make
>> >> your DAO class.
>> >>
>> >> ./tch
>> >>
>> >>
>> >>
>> >> On Sat, Nov 22, 2008 at 1:06 PM, csergiu77
>> <csergiu77@...> wrote:
>> >>>
>> >>>
>> >>> Do you know any good persistence layer code
>> generation tool ? (not only
>> >>> for
>> >>> JPA)
>> >>>
>> >>> To generate simple
>> inserts,update,delete,getAll...,findBy bla bla stuff
>> >>> and
>> >>> create the interfaces and necessary factories.
>> >>>
>> >>> THX !
>> >>>
>> >>>
>> >>>
>> >>> --
>> >>> View this message in context:
>> >>>
>> http://www.nabble.com/Code-generation-tool-for-persitence-layer-%28from-database%29-tp20639046p20639046.html
>> >>> Sent from the EclipseLink - Users mailing list
>> archive at Nabble.com.
>> >>>
>> >>>
>> _______________________________________________
>> >>> eclipselink-users mailing list
>> >>> eclipselink-users@...
>> >>>
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>> >>>
>> >> _______________________________________________
>> >> eclipselink-users mailing list
>> >> eclipselink-users@...
>> >>
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>> >>
>> >>
>> >
>> > --
>> > View this message in context:
>> http://www.nabble.com/Code-generation-tool-for-persitence-layer-%28from-database%29-tp20639046p20661083.html
>> > Sent from the EclipseLink - Users mailing list archive
>> at Nabble.com.
>> >
>> > _______________________________________________
>> > eclipselink-users mailing list
>> > eclipselink-users@...
>> >
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>> >
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@...
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
>
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@...
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

 « Return to Thread: Code generation tool for persitence layer (from database)