春天与春天无需JPA即可休眠 [英] Spring & Hibernate without jpa

查看:85
本文介绍了春天与春天无需JPA即可休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的新项目,我计划使用Hibernate 5和Spring 4,并且一如既往地希望分成不同的层/项目.

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

等级依赖性:

"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'

有一个API项目,其中包含User.class.我认为该用户类不得对数据库层使用任何注释.它不能指定@Table(name = "users")或其他内容.它应该是带有getter和setter的简单对象.

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.

数据库层应决定如何存储数据,这在很大程度上取决于数据库(例如MongoDB或MySQL).

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

我遵循了一些有关Hibernate的教程,最后得到了以下@Configuration

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.

如何将hbm/user.xml添加到sessionBuilder?

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

我尝试了在某些示例中找到的Configuration类,但是不推荐使用方法buildSessionFactory().

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

我还尝试了此处中所述的ServiceRegistry,但后来我迷路了数据源方法,并且没有数据源,系统无法设置HibernateTransactionManager.
没有HibernateTransactionManager,我将无法使用@Transactional,并且我不喜欢手动打开和关闭我的交易.

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.

目前,我正在圈子中旋转,确实需要帮助才能使其正常工作.我已经考虑过放弃Hibernate并使用我以前的MyBatis良好的方法,但是您知道,我喜欢学习新的东西...

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...

推荐答案

xml文件作为资源添加到SessionFactory,如下所示:

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

这篇关于春天与春天无需JPA即可休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆