【问题标题】:Trouble getting started with gradle springboot and postgresql难以开始使用 gradle springboot 和 postgresql
【发布时间】:2016-12-09 11:37:51
【问题描述】:

我在 Eclipse 中有一个 Java 项目,我之前使用过 mysql 我实现了 spring,现在我将数据库切换到 postgresql,我还想用 gradle 实现 springboot 并将我的基于 xml 的配置切换到基于 java 的配置。在 xml 中,我有以下 dataconfig.xml,当我将早期的 jdbc 换成 mysql 到 jdbc 换成 postgresql 时,我得到了在服务器上运行的应用程序。

<!-- Simple implementation of the standard JDBC DataSource interface, configuring 
        the plain old JDBC DriverManager via bean properties -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url"
            value="jdbc:postgresql://localhost:5432/up2u_user" />
        <property name="username" value="up2u_user2" />
        <property name="password" value="e2f2c2ac87" />
    </bean>

    <!-- This produces a container-managed EntityManagerFactory; rather than 
        application-managed EntityManagerFactory as in case of LocalEntityManagerFactoryBean -->
    <bean id="entityManagerFactoryBean"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />

        <!-- This makes /META-INF/persistence.xml is no longer necessary -->
        <property name="packagesToScan" value="se.up2u.flowkeeper.*" />

        <!-- JpaVendorAdapter implementation for Hibernate EntityManager. Exposes 
            Hibernate's persistence provider and EntityManager extension interface -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.connection.pool_size">0</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.event.merge.entity_copy_observer">allow</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">true</prop>
            </props>
        </property>
    </bean>
</beans>

现在我的问题是我的新 springboot gradle 项目中有以下文件...

这是我的 application.properties 文件。

# General JPA properties
spring.jpa.database=POSTGRESQL
spring.jpa.database-platform=postgres
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

# DataSource configuration
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/up2u_user
spring.datasource.username=up2u_user2
spring.datasource.password=password

# Hibernate Specific properties
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.hibernate.ddl-auto=create-drop

这是我的依赖项。

dependencies {
    compile 'com.graphql-java:graphql-java:2.0.0'
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.security.oauth:spring-security-oauth2')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-jersey')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile group: 'postgresql', name: 'postgresql', version: '9.1-901-1.jdbc4'
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

这是我的申请文件。

@SpringBootApplication
@EnableAutoConfiguration
@EnableWebMvc
@EntityScan("se.up2u.flowkeeper.model.entity")
public class FlowKeeperCoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(FlowKeeperCoreApplication.class, args);
    }
}

这是我的配置文件,atm 里面很空……

@Configuration
@EnableWebSecurity
public class FlowKeeperConfig {
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

当我尝试运行“gradle bootRun”时,我得到以下堆栈跟踪。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: class demo.fabric.Employee not found while looking for property: id
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) ~[spring-context-4.3.2.RELEASE.jar:4.3.2                                                        .RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at se.up2u.flowkeeper.FlowKeeperCoreApplication.main(FlowKeeperCoreApplication.java:16) [main/:na]
Caused by: org.hibernate.MappingException: class demo.fabric.Employee not found while looking for property: id
    at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:212) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:422) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindSimpleEntityIdentifier(ModelBinder.java:712) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindEntityIdentifier(ModelBinder.java:342) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindRootEntity(ModelBinder.java:237) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindEntityHierarchy(ModelBinder.java:184) ~[hibernate-core-5.0.9.Final.ja                  r:5.0.9.Final]
    at org.hibernate.boot.model.source.internal.hbm.HbmMetadataSourceProcessorImpl.processEntityHierarchies(HbmMetadataSourceProcessorImpl.java:144) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:218) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    ... 16 common fmittedo
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [demo.fabric.Employee]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:229) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:208) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    ... 32 common frames omitted
Caused by: java.lang.ClassNotFoundException: Could not load requested class : demo.fabric.Employee
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_92]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_92]
    at java.lang.Class.forName0(Native Method) ~[na:1.8.0_92]
    at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_92]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:226) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    ... 33 common frames omitted

我已经明白我应该定义 bean 来替换默认值一次,但我真的不知道该怎么做?我已经搜索过,但还没有真正理解要创建哪些 bean、要更改哪些属性或缺少或不必要的依赖项。

更新: 堆栈跟踪中提到的类和包不在我的项目中。我使用包结构 se.up2u.flowkeeper 我刚刚意识到我使用的 jdbc 是 postgresql 所指的 jdbc41,它适用于 java 1.7,没有适用于 1.8 的 jdbc42。如果我删除 postgresql 依赖项,我还会收到另一个关于不满足依赖项的错误。

【问题讨论】:

  • 错误表示您的实体类 demo.fabric.Employee 没有注释为 @Id 的字段。 Caused by: org.hibernate.MappingException: class demo.fabric.Employee not found while looking for property: id
  • 是的,但这不是我的项目中的课程。我使用名为 se.up2u.flowkeeper 的包。等等。当我用 entetymanagerFactory 添加@Bean 时,我得到另一个关于 TransactionManager 的错误,但我明白了这一切,感觉就像我错过了一些明显的东西。
  • 正在从Eclipse 运行gradle?检查您的类路径,看起来您添加了另一个项目作为依赖项。顺便说一句:github.com/asanciangco/CS143/blob/master/HW3/…docs.oracle.com/cd/E17952_01/mysql-utilities-1.4-en/….
  • 不,我在我的 macbook air 上从终端运行它。嗯,有趣的是,这是我的所有依赖项,你可以在这里看到。那是一个有趣的链接,但那是一个 mysql 项目。我会尝试添加该类和包,看看会发生什么。
  • 你有没有试过查看你的依赖:gradle app:dependencies

标签: java postgresql hibernate jpa spring-boot


【解决方案1】:

对我来说,问题已经通过从 java 类路径中删除多余的 mysql-connector-java.jar 得到解决(对我来说是 /lib/ext)。

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多