简体   繁体   中英

How can I translate JPA xml configuration into a java class?

I want to a configure JPA in a java class (not using the usual persistence.xml approach)

Here is an example of what I'm doing:

public SessionFactory getSessionFactory(){
    Configuration configuration=new Configuration();

    Properties settings= new Properties();

    settings.put(Environment.DRIVER, "");
    settings.put(Environment.URL, "");
    settings.put(Environment.USER, "");
    settings.put(Environment.PASS, "");
    settings.put(Environment.DIALECT, "");
    settings.put(Environment.SHOW_SQL, "true");
    settings.put(Environment.DEFAULT_SCHEMA, "");
    configuration.setProperties(settings);
     configuration.addAnnotatedClass(Passenger.class);  
     ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
             .applySettings(configuration.getProperties()).build();
     sessionFactory=configuration.buildSessionFactory(serviceRegistry);

}

If you cannot (or don't want) use the standard XML configuration, you can bootstrap JPA via a custom implementation. Here is an example. If you provide more info about your desired result (dialect, persistence unit name etc.) I can give you a more detailed example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM