Create EntityManagerFactory programatically (without persistence.xml file) with annotated classes

Viewed 3126

Now I am creating EntityManagerFactory like this:

    Map<String, String> properties = ImmutableMap.<String, String>builder()
        .put(DRIVER, "com.mysql.jdbc.Driver")
        .put(DIALECT, "org.hibernate.dialect.MySQL5Dialect");
        .put(USER, dbUsername)
        .put(PASS, dbPassword)
        .put(URL, dbConnectionUrl)
        //Some more properties
        .build();

    Ejb3Configuration cfg = new Ejb3Configuration();

    cfg.configure(properties);

    cfg.addAnnotatedClass(AuditEntry.class);
    cfg.addAnnotatedClass(LastWrittenEventId.class);
    //Some more annotated classes

    return cfg.createEntityManagerFactory();

However as I can see in javadocs, Ejb3Configuration is deprecated and I shouldn't use it. I should use Persistence.createEntityManagerFactory() according to JPA spec section 7.3. But then I can pass only some properties, but can I add annotated classes somehow?

1 Answers
Related