gpt4 book ai didi

java - Spring JPA :PropertyAccessException 1:. ..MethodInvocationException : .'driverClassName' . .. org/postgresql/Driver : Unsupported major. 次要版本 52.0

转载 作者:行者123 更新时间:2023-11-29 13:21:00 27 4
gpt4 key购买 nike

我是 Spring Data JPA 的新手。我能够实现在 PostgreSQL 数据库表中正常存储、删除和查找对象/行的简单解决方案。

我尝试了一个内存数据库,即 HSQLDB,它对我来说工作得很好。但是,问题是当我尝试连接到 PostgreSQL 数据库架构时。请指导我解决这个问题,看看是什么导致了这个问题。

下面是相同的堆栈跟踪:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 52.0
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.spring.btl.pl.controller.UserController.main(UserController.java:17)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 52.0
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1493)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:320)
... 15 more
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 52.0
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:108)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489)
... 23 more

我的 spring.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">

<!-- Scans for components that will be auto-registered as Spring beans -->
<context:component-scan base-package="com.spring.btl.pl" />
<!-- Scans for repositories that will be auto-registered -->
<jpa:repositories base-package="com.spring.btl.pl.repository" />
<!-- Activates various annotations to be detected in bean classes e.g: @Autowired -->
<context:annotation-config />

<!-- JPA -->

<!-- Datasource, that is currently hsqldb (in-memory database). -->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:datasource" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean> -->



<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/parulTestDB" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
<property name="connectionProperties">
<props>
<prop key="socketTimeout">10</prop>
</props>
</property>
</bean>

<!-- EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="com.spring.btl.pl.entity"
p:dataSource-ref="dataSource"
>
<!-- <property name="jpaProperty">
<props>
<entry key="hibernate.cache.use_second_level_cache" value="true"/>
<entry key="hibernate.cache.use_query_cache" value="true"/>
<entry key="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<entry key="hibernate.show_sql" value="true" />
<entry key="hibernate.use_sql_comments" value="false" />
<entry key="hibernate.format_sql" value="true" />
<entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<entry key="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</props>
</property> -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- <property name="database" value="${jpa.database}" />
<property name="showSql" value="${jpa.showSql}" />
<property name="databasePlatform" value="${jpa.dialect}" />
<property name="generateDdl" value="${jpa.generateDdl}" /> -->
<property name="generateDdl" value="true" />
<property name="showSql" value="false" />
</bean>
</property>

</bean>

<!-- Transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

</beans>

我的 UserController.java 如下:

package com.spring.btl.pl.controller;

import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.btl.pl.dao.UserDao;
import com.spring.btl.pl.entity.Usert;


public class UserController {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/spring.xml");
UserDao userDao=ctx.getBean(UserDao.class);
userDao.saveObject(new Usert(3,"sanjay", "xyz"));
List<Usert> list= (List<Usert>) userDao.findObject();
System.out.println(list);
System.out.println("user object saved");
}
}

Usert.java 即实体如下:

package com.spring.btl.pl.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name="User")
public class Usert {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public Integer id;
public String user_name;
public String description;

/**
*
*/
public Usert() {

}


public Usert(Integer id) {
super();
this.id = id;
}


/**
*
* @param id
* @param user_name
* @param description
*/
public Usert(Integer id, String user_name, String description) {
this.id = id;
this.user_name = user_name;
this.description = description;
}
/**
*
*/
@Override
public String toString() {
return "User [id=" + id + ", user_name=" + user_name + ", description="
+ description + "]";
}

}

UserDao.java 如下:

package com.spring.btl.pl.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.spring.btl.pl.entity.Usert;
import com.spring.btl.pl.repository.UserRepository;

@Component
public class UserDao implements BaseDao {

@Autowired
private UserRepository userrepo;
private Usert user;


public void saveObject(Object obj) {
//Usert user=new Usert();
user=(Usert) obj;
userrepo.save(user);

}


public void deleteObject(Object obj) {
user=(Usert) obj;
userrepo.delete(user);
}


public void updateObject(Object obj) {
// TODO Auto-generated method stub

}


public Object findObject() {
List<Usert> userList=userrepo.findAll();
return userList;
}

/*public void deleteObject(Object obj) {
// TODO Auto-generated method stub
userrepo.delete(user);
}

public void updateObject(Object obj) {
// TODO Auto-generated method stub

}*/
}

UserRepository 如下:

package com.spring.btl.pl.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.spring.btl.pl.entity.Usert;

@Transactional
@Repository
public interface UserRepository extends JpaRepository<Usert, Long> {


}

pom.xml 如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BTL_PL</groupId>
<artifactId>BTL_PL</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<!-- Generic properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>

<!-- SPRING & HIBERNATE / JPA -->
<spring.version>4.0.0.RELEASE</spring.version>
<spring.data.jpa.version>1.7.1.RELEASE</spring.data.jpa.version>
<hibernate.version>4.1.9.Final</hibernate.version>
<hsqldb.version>1.8.0.7</hsqldb.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring-Data-JPA -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.jpa.version}</version>
</dependency>

<!-- JPA Implementation (Hibernate)-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- IN MEMORY Database and JDBC Driver -->
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
<!-- LOG -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>

</dependencies>

<build>
<plugins>
<!-- This plugin is needed to define the java version in maven project. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

最佳答案

你的数据源应该这样配置:

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/parulTestDB" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
<property name="connectionProperties">
<props>
<prop key="socketTimeout">10</prop>
</props>
</property>
</bean>

关于java - Spring JPA :PropertyAccessException 1:. ..MethodInvocationException : .'driverClassName' . .. org/postgresql/Driver : Unsupported major. 次要版本 52.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41791218/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com