简体   繁体   中英

Spring & Hibernate without jpa

For my new project I plan to use Hibernate 5 and Spring 4 and as always like to separate into different layers / projects.

Gradle dependencies:

"org.springframework:spring-webmvc:4.2.1.RELEASE",
"org.springframework:spring-orm:4.2.1.RELEASE",
'org.hibernate:hibernate-core:5.0.2.Final',
'mysql:mysql-connector-java:5.1.36'

There is an API project, that contains a User.class. From my opinion this user class must not use any annotations for a database layer. It must not specify @Table(name = "users") or other things. It should be a simple Objects with getters and setters.

The database layer should decide how to store the data and this depends strongly on the database (eg MongoDB or MySQL).

I followed some tutorials for Hibernate and ended up with the following @Configuration class

@Configuration
@ComponentScan("de.pentos.proto")
@EnableWebMvc
@EnableTransactionManagement
public class AppConfig {
    private static final Logger log = LoggerFactory.getLogger(AppConfig.class);

    private static Properties getHibernateProperties() {
        final Properties properties = new Properties();
        properties.put("hibernate.show_sql", "true");
        // properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.put("hbm2ddl.auto", "create");
        return properties;
    }

    {
        log.debug("Here am I: {}");
    }

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/myschema");
        dataSource.setUsername("user");
        dataSource.setPassword("password");
        return dataSource;
    }

    @Inject
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(final DataSource dataSource) {
        final LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        sessionBuilder.addAnnotatedClasses(User.class);
        sessionBuilder.addProperties(getHibernateProperties());
        return sessionBuilder.buildSessionFactory();
    }

    @Inject
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager(final SessionFactory sessionFactory) {
        final HibernateTransactionManager transactionManager = new HibernateTransactionManager(
                sessionFactory);
        return transactionManager;
    }
}

It works very well, except it uses an annotated class.

How can I add my hbm/user.xml to the sessionBuilder?

I tried with Configuration class, that I found in some examples, but the Method buildSessionFactory() is deprecated.

I also tried the ServiceRegistry described here but then lost my datasource approach and without the datasource the system was not able to setup the HibernateTransactionManager .
Without HibernateTransactionManager I was not able to use @Transactional and I don't like to open and close me transactions manually.

Currently I'm spinning in circles and really need help to get it to work. I already thought about throwing Hibernate away and use my good old MyBatis approach, but you know, I like to learn something new...

Add xml files as a resource to SessionFactory , as follows:

@Inject
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(final DataSource dataSource) {
        final LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        sessionBuilder.addResource("/path-to-/hbm/user.xml");
        sessionBuilder.addAnnotatedClasses(User.class);
        sessionBuilder.addProperties(getHibernateProperties());
        return sessionBuilder.buildSessionFactory();
    }

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