ORM for D programming language, fork from Hibernated.

Package Information

Version0.1.0 (2017-Mar-20)
LicenseBoost Software License (BSL 1.0)
Registered byBrian Zou



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




Entity is ORM for D language (similar to JPA, fork from hibernated)

Uses DDBC as DB abstraction layer:

Available as DUB package

Use SQLite 3.7.11 or later. In older versions syntax INSERT INTO (col1, col2) VALUES (1, 2), (3, 4) is not supported.

Database config url:


Sample code:

import entity;
import std.algorithm;
import std.stdio;

// Annotations of entity classes

class User {
    long id;
    string name;
    Customer customer;
    @ManyToMany // cannot be inferred, requires annotation
    LazyCollection!Role roles;

class Customer {
    int id;
    string name;
    // Embedded is inferred from type of Address
    Address address;
    Lazy!AccountType accountType; // ManyToOne inferred
    User[] users; // OneToMany inferred
    this() {
        address = new Address();

class Address {
    string zip;
    string city;
    string streetAddress;

class AccountType {
    int id;
    string name;

class Role {
    int id;
    string name;
    @ManyToMany // w/o this annotation will be OneToMany by convention
    LazyCollection!User users;

int main()
    // create metadata from annotations
    EntityMetaData schema = new SchemaInfoImpl!(User, Customer, AccountType, 
        Address, Role);
    // setup DB connection factory
    version (USE_MYSQL) {
            MySQLDriver driver = new MySQLDriver();
            string url = MySQLDriver.generateUrl("localhost", 3306, "test_db");
            string[string] params = MySQLDriver.setUserAndPassword("testuser", "testpasswd");
            Dialect dialect = new MySQLDialect();
        } else {
        import ddbc.all;
            SQLITEDriver driver = new SQLITEDriver();
            string url = "zzz.db"; // file with DB
            static import std.file;
            string[string] params;
            Dialect dialect = new SQLiteDialect();
        DataSource ds = new ConnectionPoolDataSourceImpl(driver, url, params);

        // create session factory
        EntityManagerFactory factory = new EntityManagerFactoryImpl(schema, dialect, ds);
        scope(exit) factory.close();

        // Create schema if necessary
        // get connection
        Connection conn = ds.getConnection();
        scope(exit) conn.close();
        // create tables if not exist
        factory.getDBMetaData().updateDBSchema(conn, false, true);

        // Now you can use HibernateD

        // create session
        EntityManager sess = factory.createEntityManager();
        scope(exit) sess.close();

        // use session to access DB

        // read all users using query
        Query q = sess.createQuery("FROM User ORDER BY name");
        User[] list = q.list!User();

        // create sample data
        Role r10 = new Role(); = "role10";
        Role r11 = new Role(); = "role11";
        Customer c10 = new Customer(); = "Customer 10";
        c10.address = new Address(); = "12345"; = "New York";
        c10.address.streetAddress = "Baker st., 12";
        User u10 = new User(); = "Alex";
        u10.customer = c10;
        u10.roles = [r10, r11];;;;;
        sess = factory.createEntityManager();

        // load and check data
        User u11 = sess.createQuery("FROM User WHERE name=:Name").
                                   setParameter("Name", "Alex").uniqueResult!User();

        writeln("u11.customer.users.length=", u11.customer.users.length);


        // remove entity
       // sess.remove(u11);

        User u112 = sess.createQuery("FROM User WHERE name=:Name").
        setParameter("Name", "Alex").uniqueResult!User();
        writeln("u11.customer.users.length=", u112.customer.users.length);

        return 0;

Available versions

0.1.0 0.0.8 0.0.7 0.0.6 0.0.5 0.0.4 0.0.3 0.0.2 ~master