Skip to content

Multi Database

Brian Zou edited this page Jun 26, 2018 · 1 revision
@Tables("users")
class User
{
    int id;
    string name;
}

@Factory("article")
@Tables("articles")
class Article
{
    int id;
    string title;
    string content;
}

EntityOption option = new EntityOption();
option.database.driver = "mysql";
option.database.host = "localhost";
option.database.database = "account";
option.database.username = "root";
    
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default", option);

EntityOption option1 = new EntityOption();
option1.database.driver = "mysql";
option1.database.host = "localhost";
option1.database.database = "article";
option1.database.username = "root";
    
EntityManagerFactory articleEntityManagerFactory = Persistence.createEntityManagerFactory("article", option1);

int id = 1;
EntityManager em = entityManagerFactory.createEntityManager();
auto user = em.find!User(id);

int article_id = 9;
EntityManager article_em = articleEntityManagerFactory .createEntityManager();
auto article= article_em .find!Article(article_id);
Clone this wiki locally