an implementation of the JPA specification with Dlang

JPA规范主要包括三部分:

  • ORM元数据,元数据描述对象与表之间的映射关系.(JPA是通过annotion或XML两种形式描述对象-关系映射,我们可以通过模板,把Obejct注册进去)
  • Entity操作API
  • 查询语言 (JPA是根据JPQL实现的,暂时可以用SQLBuilder实现,后期可以根据SQL语句自己写parser生成AST来处理)

实现类说明

an implementation of the JPA specification with Dlang_第1张图片
jpa.png
  • Persistence

读取配置信息

  • EntityManagerFactory

用于创建会话/实体管理器的工厂类

  • EntityManager

提供Entity操作API,管理事务,创建查询等
1.persist ,持久化entity
2.remove,删除entity
3.merge,持久化entity(updateIfExist)

  • EntityTransaction

管理事务

  • Query

执行查询

Entity生命周期

an implementation of the JPA specification with Dlang_第2张图片
entity.png
  • New 新建Entity对象
  • Managed 对象处于Persistence Context,由EntityManager管理
  • Detached 对象进入Application Domain
  • Removed Entity被删除

Entity关联关系

  • one-to-one
  • one-to-many
  • many-to-one
  • many-to-many

Entity字段属性说明

  • Auto
  • PrimaryKey
  • Table
  • ...

异常说明

  • DatabaseException

Dialect说明

主要功能包括将对象操作转换成SQL语句.和从sql数据转换为对象

D的使用示例

class User : Model
{
    @AUTO
    int id;
    string name;
}

DatabaseOption option = new DatabaseOption();
option.addDatabaseSource("mysql://root:123456@localhost:3306/blog?charset=utf-8");
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("mysql",option);
EntityManager entitymanager = entityManagerFactory.createEntityManager!(User);
User user = new User();
user.setName = "viile";
entitymanager.persist(user);

你可能感兴趣的:(an implementation of the JPA specification with Dlang)