Giter VIP home page Giter VIP logo

generic-injectable-dao's Introduction

generic-injectable-dao

JPA Generic DAOs that are injectable using the pattern described in the page http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/

#Configuration This library will not work if the project is not inside a JEE container (JBoss, Glassfish, TomEE, etc).

To first use this library the component (EJB, WAR) that has the persistence.xml file should have one stateless session bean that extends DaoDataSource and is annotated with @Manager

@Stateless
@Manager(lookup = "java:global/jndi", defaultManager = true, name = "manager")
public class GenericDataSource implements DaoDataSource {

    @PersistenceContext
    private EntityManager genericEm;

    @Override
    public EntityManager getEntityManager() {
        return genericEm;
    }
}

#Usage The generic dao can be injected using CDI and a custom annotation that has the Class literal of the entity that the generic dao represents. The generic types are the Entity type and the Type of the primary key (PK) of the entity.

For example, having an entity of type 'Entity' and PK of type 'Integer' the resulting GenericDao should be injected like so:

@Inject
@GenericDao(Entity.class)
private JpaDao<Entity, Integer> entityDao;

There is a ParameterBuilder helper class that can be used instead of a Map<String, Object> (Probably useless, but whatever).

ParameterBuilder builder = ParameterBuilder.with("param1", param1)
                    .add("param2", param2);
            
List<Entity> list = entityDao.findWithNamedQuery("Entity.namedQuery", builder.build());

Can also be used as

import static com.gammapeit.generic.injected.dao.jpa.ParameterBuilder.*;

List<Entity> list = entityDao.findWithNamedQuery("Entity.namedQuery", with("param1", param1).add("param2", param2).build());

The DAOs have the most common methods needed, CRUD, Batch CRUD, Count, NamedQueries, NativeQueries, and Pagination (Filters, Sorts and uses hints).

The generic DAO can also be used with more than one persistence unit:

@Inject
@GenericDao(value = Entity.class, managerName = "manager")
private JpaDao<Entity, Integer> entityDao;

When using the optional managerName tag the library will look for the @Manager classes that have a name (attribute of Manager annotation) that is the same as the string supplied.

generic-injectable-dao's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.