Giter Club home page Giter Club logo

bcjpadao's Introduction

bcjpadao

Light weight (18 classes) JPA helper library - syntatic sugar (elegantly expressive) and more

Define variables

        int firstResult = 0;
        int maxResults = 100;
        
        EntityManager em;
        
        Class<E> entityType;
        
        Object toPersist;

Simple use-case (Generic Dao)

        try(Dao dao = new DaoImpl(em)) {
            dao.begin().persist(toPersist).commit();
        }

DeleteDao

        // Call Dao#forDelete to get DeleteDao instance, forSelect to get SelectDao... etc
        //
        DeleteDao<E> forDelete = dao.forDelete(entityType);

        // finish() method calls executeUpdate(), commit(), close()
        //
        int updateCount = forDelete.begin().from(entityType).where("col_0", "val_0").finish(); 

SelectDao

        List<E> resultList = dao.forSelect(entityType).getCriteria()
                .from(entityType)
                .select("col_0", "col_1")
                .where()
                .getResultsAndClose(firstResult, maxResults);

Using Builders makes life easier

        // (Builders implement CriteriaDao so no need to call getCriteria())

        // SELECT COUNT(*) WHERE col = 'val'
        
        Long count = dao.builderForSelect(Long.class)
                .where(entityType, "col", "val").count().getSingleResultAndClose();
        
        // SELECT col_0, col_1 FROM table WHERE ... ... ORDER BY col_2 ASC col_1 ASC

        List<String[]> resultList = new DaoImpl(em).builderForSelect(String[].class)
                .from(entityType)
                .where("col_0", "val_0")
                .or().where("col_1", Criteria.LIKE, "val_1")
                .and().where("col_2", Criteria.LESS_THAN, "val_2")
                .select(columnsToSelect)
                .ascOrder("col_2", "col_1")
                .getResultsAndClose(firstResult, maxResults);

Reusing Dao / Builder(s)

        // Dao / BuilderFor may be reused if #close() has not been called. Simply call #reset() before reuse.

        try(BuilderForUpdate<E> reused = new DaoImpl(em).builderForUpdate(entityType)) {
            
            reused.begin();
            
            updateCount = reused.from(entityType).where("col_0", "val_0").set("col_0", "val_1").executeUpdate();
            
            reused.reset();
            
            updateCount = reused.from(entityType).where("col_0", "val_1").set("col_0", "val_0").executeUpdate();
            
            reused.commit();
        }

bcjpadao's People

Contributors

poshjosh avatar

Watchers

James Cloos 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.