2014-10-06 70 views
1

我無法在0.7.1中創建休眠會話。我知道我沒有正確創建休眠會話。但不知道我哪裏錯了。任何幫助將不勝感激。使用Dropwizard 0.7.1設置休眠的問題。 「無法在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?

回答

6

它在資源的方法添加註釋@UnitOfWork工作後。