D 语言的 ORM 框架 HibernateD

码农软件 · 软件分类 · ORM/持久层框架 · 2019-09-23 18:57:54

软件介绍

HibernateD 是 D 语言的 ORM 框架,类似 Java 的 Hibernate,示例代码:

import hibernated.core;


// 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();
    }
}

@Embeddable
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;
}

// create metadata from annotations
EntityMetaData schema = new SchemaInfoImpl!(User, Customer, AccountType, 
                                 T1, TypeTest, Address, Role, GeneratorTest);




// setup DB connection factory
MySQLDriver driver = new MySQLDriver();
string url = MySQLDriver.generateUrl("localhost", 3306, "test_db");
string[string] params = MySQLDriver.setUserAndPassword("testuser", "testpasswd");
DataSource ds = ConnectionPoolDataSourceImpl(driver, url, params);

// create session factory
Dialect dialect = new MySQLDialect();
SessionFactory factory = new SessionFactoryImpl(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
Session sess = factory.openSession();
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();
r10.name = "role10";
Role r11 = new Role();
r11.name = "role11";
Customer c10 = new Customer();
c10.name = "Customer 10";
User u10 = new User();
u10.name = "Alex";
u10.customer = c10;
u10.roles = [r10, r11];
sess.save(r10);
sess.save(r11);
sess.save(c10);
sess.save(u10);

// load and check data
User u11 = sess.createQuery("FROM User WHERE name=:Name").
                           setParameter("Name", "Alex").uniqueResult!User();
assert(u11.roles.length == 2);
assert(u11.roles[0].name == "role10" || u11.roles.get()[0].name == "role11");
assert(u11.roles[1].name == "role10" || u11.roles.get()[1].name == "role11");
assert(u11.customer.name == "Customer 10");
assert(u11.customer.users.length == 1);
assert(u11.customer.users[0] == u10);
assert(u11.roles[0].users.length == 1);
assert(u11.roles[0].users[0] == u10);

// remove reference
u11.roles.get().remove(0);
sess.update(u11);

// remove entity
sess.remove(u11);

本文地址:https://codercto.com/soft/d/15248.html

创客

创客

克里斯·安德森 / 萧潇 / 中信出版社 / 2012-12 / 45.00元

★《长尾理论》、《免费》作者克里斯•安德森最新作品 ★ 创客运动,一场即将到来的革命 ★ 编辑推荐: 这是一场即将到来的革命。 这是一个创客的时代,他们引领科技行业走进了一个新的方向,即个体制造时代的到来。运用互联网和最新的工业技术进行创造,创客运动发出了最强音。 如果说《第三次工业革命》一书的核心是互联网与新能源融合在一起所引发的工业变革。那么《创客》一书的核心则是......一起来看看 《创客》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具