Spring & Hibernate without jpa

痴心易碎 提交于 2019-12-12 14:24:51

问题


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 (e.g. 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...


回答1:


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();
    }


来源:https://stackoverflow.com/questions/32904513/spring-hibernate-without-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!