hunt-entity 2.0.1

Entity is an object-relational mapping tool for the D programming language. Referring to the design idea of JPA. support PostgreSQL / MySQL / SQLite.


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Build Status

Entity

Entity is an object-relational mapping tool for the D programming language. Referring to the design idea of JPA.

Support databases

  • PostgreSQL 9.0+
  • MySQL 5.1+
  • SQLite 3.7.11+

Depends

Simple code

import hunt.entity;

@Table("user")
class User
{
    @PrimaryKey
    @AutoIncrement
    int id;

    string name;
    double money;
    string email;
    bool status;
}

void main()
{
    auto option = new EntityOption;

    option.database.driver = "mysql";
    option.database.host = "localhost";
    option.database.port = 3306;
    option.database.database = "test";
    option.database.username = "root";
    option.database.password = "123456";
    option.database.charset = "utf8mb4";
    option.database.prefix = "hunt_";

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default", option);
    EntityManager em = entityManagerFactory.createEntityManager();

    // begin transaction
    em.getTransaction().begin();

    // define your database existing row id in here
    int id = 10;

    auto user = em.find!User(id);
    log("User name is: ", user.name);

    // commit transaction
    em.getTransaction().commit();

    em.close();
    entityManagerFactory.close();
}

Insert row

    auto user = new User();
    user.name = "Brian";
    user.email = "brian@huntlabs.cn";
    user.money = 99.9;
    
    // insert user
    em.persist(user);
    log("User id is: ", user.id);

Delete row

    int n = em.remove!User(id);
    log("The number of users deleted is: ", n);

Update row

    auto user = em.find!User(id);
    log("User name is: ", user.name);
    user.name = "zoujiaqing";
    em.merge!User(user);
    log("The number of users updated is: ", n);

Use CriteriaQuery to find

    // create CriteriaBuilder object from em
    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery!User criteriaQuery = builder.createQuery!User;
    Root!User root = criteriaQuery.from();
    Predicate p1 = builder.equal(root.User.id, id);
    TypedQuery!User typedQuery = em.createQuery(criteriaQuery.select(root).where(p1));

    auto user = typedQuery.getSingleResult();

    log("User name is: ", user.name);

Use CriteriaQuery to Multi-condition find

    // create CriteriaBuilder object from em
    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery!User criteriaQuery = builder.createQuery!User;
    Root!User root = criteriaQuery.from();

    Predicate p1 = builder.lt(root.User.id, 1000);  // User id is less than 1000.
    Predicate p2 = builder.gt(root.User.money, 0);  // User money is greater than 0.
    Predicate p3 = builder.like(root.User.name, "z%");  // User name prefix is z.

    TypedQuery!User typedQuery = em.createQuery(criteriaQuery.select(root).where(builder.and(p1, p2), p3));
    User[] users = typedQuery.getResultList();

    log("The number of users found is: ", users.length);
Authors:
  • HuntLabs.cn
Dependencies:
hunt-database, hunt, hunt-sql
Versions:
2.0.1 2018-Nov-06
2.0.0 2018-Nov-02
1.6.2 2018-Sep-07
1.6.1 2018-Sep-05
1.6.0 2018-Aug-28
Show all 6 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 0 downloads total

Score:
2.3
Short URL:
hunt-entity.dub.pm