UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]

前端 未结 1 663
逝去的感伤
逝去的感伤 2021-01-25 05:48

I know that this question has been answered before, but none of the responses seem to help me, since i am not using a datasource in my project.

I a have a Maven project,

相关标签:
1条回答
  • 2021-01-25 06:07

    Well, i have figured it out. Seems like, instead of my current sessionFactory creation, I should have had something like:

    StandardServiceRegistryBuilder  serviceRegistryBuilder = new StandardServiceRegistryBuilder();  
                    DataSource datasource = getDataSource();
                    serviceRegistryBuilder.applySetting(Environment.DATASOURCE, datasource);
                    serviceRegistryBuilder.applySettings(configuration.getProperties());
                    Properties hibernateProperties = setHibernateProperties();
                    serviceRegistryBuilder.applySettings(hibernateProperties);
                    StandardServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
    
                    sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
    

    Which means i externalized the properties from hibernate.cfg.xml like that:

    @Bean
    public static DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSource.setUrl("jdbc:sqlserver://localhost:1433;databaseName=Licenta");
        dataSource.setUsername("sa");
        dataSource.setPassword("admin9");
        return dataSource;
    }
    
     private static Properties setHibernateProperties() {
         Properties properties = new Properties();
         properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2008Dialect");
         properties.put("hibernate.hbm2ddl.auto", "create");
         properties.put("hibernate.show_sql", "true");
         return properties;        
    }
    
    0 讨论(0)
提交回复
热议问题