【问题标题】:Issue with setting up hibernate with Dropwizard 0.7.1. "Unable to create a hibernate session on 0.7.1"使用 Dropwizard 0.7.1 设置休眠时出现问题。 “无法在 0.7.1 上创建休眠会话”
【发布时间】:2014-10-06 17:34:07
【问题描述】:

我无法在 0.7.1 中创建休眠会话。我知道我没有正确创建休眠会话。但无法弄清楚我哪里出错了。任何帮助将不胜感激。

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]

应用程序文件:

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

配置文件:

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 文件:

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

我尝试过的一些链接:

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

how to auto-wire HibernateBundle with guice on dropwizard?

【问题讨论】:

    标签: java hibernate dropwizard


    【解决方案1】:

    在资源方法上添加@UnitOfWork注解后就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 2011-01-24
      相关资源
      最近更新 更多