简体   繁体   English

使用Dropwizard 0.7.1设置休眠模式的问题。 “无法在0.7.1上创建休眠会话”

[英]Issue with setting up hibernate with Dropwizard 0.7.1. “Unable to create a hibernate session on 0.7.1”

I am unable to create hibernate session in 0.7.1. 我无法在0.7.1中创建休眠会话。 I know I am not creating hibernate session right. 我知道我不是在创建休眠会话。 But cant figure out where am I going wrong. 但是无法弄清楚我要去哪里错了。 Any help will be appreciated. 任何帮助将不胜感激。

ERROR:
! org.hibernate.HibernateException: No session currently bound to execution context
! at org.hibernate.context.internal.ManagedSessionContext.currentSession(ManagedSessionContext.java:75) ~[userengine-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) ~[userengine-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]

The Application file: 申请文件:

public class UserEngineApplication extends Application<UserEngineConfiguration>{

public static void main(String[] args) throws Exception {
    new UserEngineApplication().run(args);
}

private final HibernateBundle<UserEngineConfiguration> hibernateBundle =
        new HibernateBundle<UserEngineConfiguration>(Persons.class) {
            @Override
            public DataSourceFactory getDataSourceFactory (UserEngineConfiguration configuration) 
            {
                return configuration.getDataSourceFactory();
            }
};

@Override
public void initialize(Bootstrap<UserEngineConfiguration> bootstrap) {
    bootstrap.addBundle(hibernateBundle);
}

@Override
public void run(UserEngineConfiguration configuration, Environment environment) throws Exception {

    final PersonDAO personsdao = new PersonDAO(hibernateBundle.getSessionFactory());
        environment.jersey().register(new PersonsResource(personsdao));
}
}

The Configuration file: 配置文件:

public class UserEngineConfiguration extends Configuration {
@Valid
@NotNull
@JsonProperty
private DataSourceFactory database = new DataSourceFactory();

@JsonProperty("database")
public DataSourceFactory getDataSourceFactory() {
    return database;
}

@JsonProperty("database")
public void setDataSourceFactory(DataSourceFactory dataSourceFactory)
{
    this.database = dataSourceFactory;
}
}

YAML file: YAML文件:

database:
  driverClass: org.postgresql.Driver
  user: user
  password: pass
  url: jdbc:postgresql://localhost:5432/Tryout

Some links that I tried: 我尝试过的一些链接:

https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example . https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example

how to auto-wire HibernateBundle with guice on dropwizard? 如何使用dropwizard上的guice自动连接HibernateBundle?

在资源方法上添加@UnitOfWork批注后,它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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