Saturday, December 7, 2013

    Mentabean - Simple ORM and Query Buider:

    http://mentabean.soliveirajr.com/mtw/Page/Intro/en/mentabean-simple-orm-and-query-builder

    MentaBean is a straightforward ORM (Object-Relational Mapping) framework for those who want to work with SQL without the JDBC boilerplate. Differently than other ORMs it keeps the magic to a minimum so you always know what is going on under the hood.
    With MentaBean you are in control not the framework!
    MentaBean automates CRUD operations without any SQL, but it will not make you learn a new language when you need to query. Instead it helps you build your queries using SQL without the boring parts.
    With MentaBean you do not have to learn another query language on top of SQL!
    It also does the object-to-database mappings programmatically through a fluent API. With MentaBean you don't have to worry about XML programming or Annotation hell because the mappings are done with Java code.
    With MentaBean you do not have to do the mappings using XML or Annotations "logic"!
    So if you like and understand SQL and are tired of the huge learning curve and unpredictable behavior offered by other complex ORM frameworks you should try MentaBean for a simplicity break.

Quick Start

    Notice how clean your bean is. There is no framework dependency and no annotations polluting your entity:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    public class User {
     
        public static enum Status {
            BASIC, PREMIUM, GOLD
        }
     
        private long id;
        private String username;
        private Date birthdate;
        private Status status;
        private boolean deleted;
        private Date insertTime;
     
        public User() {
            // mandatory
        }
     
        public User(long id) {
            this.id = id;
        }
     
        public User(String username, Date birthdate, Status status) {
            this.username = username;
            this.birthdate = birthdate;
            this.status = status;
        }
     
        // getters and setters
    }

    The database table:
    create table Users(
            id integer primary key auto_increment,
            username varchar(25),
            bd datetime,
            status varchar(20),
            deleted tinyint,
            insert_time timestamp
    );

    Now the mapping done with Java code through a fluent API. Again no annotation black magic or XML hell:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    BeanManager mgr = new BeanManager();
     
    BeanConfig userCfg = new BeanConfig(User.class, "Users") // "Users" is the table name
        .pk("id", DBTypes.AUTOINCREMENT) // the primary key!
        .field("username", DBTypes.STRING)
        .field("birthdate", "bd", DBTypes.DATE) // note that the database column name is different
        .field("status", DBTypes.ENUMVALUE.from(Status.class)) // status will be saved as a varchar in the database
        .field("deleted", DBTypes.BOOLEANINT) // 0 for false and 1 for true in the database
        .field("insertTime", "insert_time", DBTypes.NOW_ON_INSERT_TIMESTAMP); // again database column name is different than property name
     
    mgr.addBeanConfig(userCfg);
    And you can also map using a properties proxy to support refactoring:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    BeanManager mgr = new BeanManager();
     
    User userProps = PropertiesProxy.create(User.class);
     
    BeanConfig userCfg = new BeanConfig(User.class, "Users")
        .pk(userProps.getId(), DBTypes.AUTOINCREMENT)
        .field(userProps.getUsername(), DBTypes.STRING)
        .field(userProps.getBirthdate(), "bd", DBTypes.DATE) // note that the database column name is different
        .field(userProps.getStatus(), DBTypes.ENUMVALUE.from(Status.class))
        .field(userProps.isDeleted(), DBTypes.BOOLEANINT)
        .field(userProps.getInsertTime(), "insert_time", DBTypes.NOW_ON_INSERT_TIMESTAMP);
     
    mgr.addBeanConfig(userCfg);

    Simple CRUD operations without SQL:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    Connection conn = getConnection();
    BeanSession session = new MySQLBeanSession(mgr, conn);
     
    // INSERT:
     
    User u = new User("saoj", toDate(1983, 1, 20), Status.BASIC);
    session.insert(u);
     
    System.out.println("The new ID created for the user: " + u.getId());
     
    // SELECT:
     
    User loadedUser = new User(u.getId());
    if (session.load(loadedUser)) {
        System.out.println("User was loaded: " + loadedUser.getUsername());
    } else {
        System.err.println("User was not found in the database: " + loadedUser.getId());
    }
     
    // UPDATE:
     
    loadedUser.setUsername("soliveira");
    session.update(loadedUser); // only the username field is updated with the new value...
     
    // DELETE:
     
    session.delete(loadedUser); // bye

    Building any query with SQL without the boring JDBC boilerplate:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    Connection conn = getConnection();
    BeanSession session = new MySQLBeanSession(mgr, conn);
     
    PreparedStatement stmt = null;
    ResultSet rset = null;
     
    try {
         
        StringBuilder sql = new StringBuilder(1024);
        sql.append("select ");
        sql.append(session.buildSelect(User.class));
        sql.append(" from Users");
        sql.append(" where status = ? and deleted = ?"); // the interesting part...
         
        stmt = SQLUtils.prepare(conn, sql.toString(), Status.GOLD.toString(), 1); // varargs for params
     
        rset = stmt.executeQuery();
         
        List users = new LinkedList();
         
        while(rset.next()) {
            User u = new User();
            session.populateBean(rset, u);
            users.add(u);
        }
     
        System.out.println("Number of users loaded: " + users.size());
         
    } finally {
        SQLUtils.close(rset, stmt, conn); // you can also statically import SQLUtils.*
    }
    NOTE: Simple queries like the one above can also be done without SQL using the session.loadList(Object bean) method. Refer toLoading Lists for more details.

    You can also use a proxy to build a fully refactorable query:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    Connection conn = getConnection();
    BeanSession session = new MySQLBeanSession(mgr, conn);
      
    PreparedStatement stmt = null;
    ResultSet rset = null;
      
    try {
         
        TableAlias userAlias = session.createTableAlias(User.class);
        User user = userAlias.pxy();
          
        SQLBuilder sql = new SQLBuilder(1024, userAlias);
        sql.append("select ");
        sql.append(userAlias.columns());
        sql.append(" from ").append(userAlias.tableName());
        sql.append(" where ").column(user.getStatus()).append(" = ? and ").column(user.isDeleted()).append(" = ?"); // the interesting part...
          
        stmt = SQLUtils.prepare(conn, sql.toString(), Status.GOLD.toString(), 1); // varargs for params
      
        rset = stmt.executeQuery();
          
        List users = new LinkedList();
          
        while(rset.next()) {
            User u = new User();
            session.populateBean(rset, u);
            users.add(u);
        }
      
        System.out.println("Number of users loaded: " + users.size());
          
    } finally {
        SQLUtils.close(rset, stmt, conn); // you can also statically import SQLUtils.*
    }

5 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes