我无法在0.7.1中创建hibernate会话。我知道我没有正确创建hibernate会话。但是无法弄清楚我哪里出错了。任何帮助将不胜感激。
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。
答案 0 :(得分:6)
在资源方法上添加@UnitOfWork
注释后,它工作正常。