Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 自动连线JpaRepository始终为空_Spring_Repository_Jpa 2.0_Eclipselink_Spring Data Jpa - Fatal编程技术网

Spring 自动连线JpaRepository始终为空

Spring 自动连线JpaRepository始终为空,spring,repository,jpa-2.0,eclipselink,spring-data-jpa,Spring,Repository,Jpa 2.0,Eclipselink,Spring Data Jpa,我正在配置我的JPA2+Spring项目,并尝试使用JPARepositories,但我似乎在autowired repository对象上遇到空指针异常。请查看代码并帮助我确定我缺少什么 Pom.xml <dependencies> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging<

我正在配置我的JPA2+Spring项目,并尝试使用JPARepositories,但我似乎在autowired repository对象上遇到空指针异常。请查看代码并帮助我确定我缺少什么

Pom.xml
    <dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons-core</artifactId>
        <version>1.4.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.java-persistence</groupId>
        <artifactId>jpa-api</artifactId>
        <version>2.0-cr-1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.2.0.RELEASE</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>r09</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
</dependencies>
Pom.xml
公用记录
公用记录
1.1.1
org.springframework.data
spring数据共享核心
1.4.1.1发布
org.hibernate.java-persistence
jpa api
2.0-cr-1
org.slf4j
slf4j api
1.7.5
log4j
log4j
1.2.14
假如
org.springframework.data
spring数据jpa
1.2.0.1发布
假如
番石榴
番石榴
r09
朱尼特
朱尼特
4.11
org.springframework
弹簧芯
3.2.4.1发布
org.springframework
spring上下文
3.2.4.1发布
org.springframework
弹簧试验
3.2.4.1发布
org.springframework
春季aop
3.2.4.1发布

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence    
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Testing124"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.myclassregister.model.base.misc.School</class>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
        <property name="eclipselink.jdbc.batch-writing" value="Buffered" />
        <property name="eclipselink.target-database" value="MySQL" />
        <property name="eclipselink.jdbc.native-sql" value="false" />
        <property name="eclipselink.logging.level" value="fine" />
        <property name="eclipselink.logging.file"
            value="C:\Users\Vineet\workspace1\MyClassRegister\persistance_logging.txt" />
        <property name="eclipselink.application-location" value="C:\Users\Vineet\workspace1\MyClassRegister" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testing124"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="test"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties>
</persistence-unit>

org.eclipse.persistence.jpa.PersistenceProvider
com.myclassregister.model.base.misc.School
启用\u选择性

applicationContext.xml

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

<context:component-scan base-package="com.myclassregister.model" />

<context:annotation-config/>

<bean id="datasource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/testing124" />
    <property name="username" value="root" />
    <property name="password" value="test" />
</bean>

<bean id="jpaDialect"
    class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="datasource" />
    <property name="persistenceUnitName" value="MyClassRegister" />
    <property name="jpaDialect" ref="jpaDialect" />
    <property name="jpaVendorAdapter">
        <bean
            class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" />
    </property>
    <property name="loadTimeWeaver">
        <bean
            class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />
    </property>
</bean>

<bean id="helloWorld"
    class="com.myclassregister.model.test.TestGetSchoolListActivity" />

<jpa:repositories base-package="com.myclassregister.model.repository"></jpa:repositories>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="loadTimeWeaver" class="com.myclassregister.utils.JpaAwareLoadTimeWeaver" />

<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans>

SchoolRepository.java

@Repository
public interface SchoolRepository 
extends JpaRepository <School,Long> {

public School findById(Long Id);

public Collection<School> findAllSchools();

}
@存储库
公共接口存储库
扩展JPA假设{
公立学校findById(长Id);
公共集合findAllSchools();
}
GetSchoolListActivity.java

 @Service
 public final class GetSchoolListActivity {

private static final String PERSISTENCE_UNIT_NAME = "MyClassRegister";
private static EntityManagerFactory factory;

@Autowired  
SchoolRepository schoolRepository;

@Transactional(readOnly=true)
public Collection<School> GetSchools () {

    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();

    JpaRepositoryFactory jpaRepositoryFactory = new JpaRepositoryFactory(em);

        schoolRepository = jpaRepositoryFactory.getRepository(SchoolRepository.class);

    Collection<School> schools = new ArrayList <School> ();
    try{
        schools = schoolRepository.findAllSchools();
    }
    catch(Exception e){
    }
    return schools;
}   
}
@服务
公开期末班GetSchoolListActivity{
私有静态最终字符串持久化\u单元\u NAME=“MyClassRegister”;
私营静态实体管理工厂;
@自动连线
学校知识库;
@事务(只读=真)
公立学校(){
factory=Persistence.createEntityManagerFactory(Persistence\u UNIT\u NAME);
EntityManager em=factory.createEntityManager();
JpaRepositoryFactory JpaRepositoryFactory=新的JpaRepositoryFactory(em);
schoolRepository=jpaRepositoryFactory.getRepository(schoolRepository.class);
集合学校=新ArrayList();
试一试{
schools=schoolRepository.findAllSchools();
}
捕获(例外e){
}
返回学校;
}   
}
堆栈跟踪

Exception in thread "main" org.springframework.data.mapping.PropertyReferenceException: No property find found for type com.myclassregister.model.base.misc.School
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:73)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:240)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:68)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:280)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:116)
at com.myclassregister.api.login.GetSchoolListActivity.GetSchools(GetSchoolListActivity.java:35)
at com.myclassregister.model.test.TestGetSchoolListActivity.main(TestGetSchoolListActivity.java:12)
线程“main”org.springframework.data.mapping.PropertyReferenceException中的异常:找不到com.myclassregister.model.base.misc.School类型的属性 位于org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75) 位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) 位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353) 位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353) 位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) 位于org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271) 位于org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245) 位于org.springframework.data.repository.query.parser.Part.(Part.java:73) 位于org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:180) 位于org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260) 位于org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:240) 位于org.springframework.data.repository.query.parser.PartTree(PartTree.java:68) 位于org.springframework.data.jpa.repository.query.PartTreeJpaQuery(PartTreeJpaQuery.java:57) 位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90) 位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162) 位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68) 位于org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor。(RepositoryFactorySupport.java:280) 位于org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148) 位于org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:116) 在com.myclassregister.api.login.GetSchoolListActivity.GetSchools(GetSchoolListActivity.java:35) 位于com.myclassregister.model.test.TestGetSchoolListActivity.main(TestGetSchoolListActivity.java:12)
创建entityManagerFactory时,需要指定域类。 在配置中添加以下属性:


在您粘贴到这里的stacktrace中,它表示无法映射/识别学校班级。一旦您包含了上述属性,它就应该可以工作了。

由于实体没有任何列“ID”(其SchoolID),并且存储库有一个findByID方法抛出错误,所以我遇到了问题。

您可以进行堆栈后跟踪吗?为什么需要从jpaRepoFactory获取repo实例。自动连线应该是enuf。如果我试图直接从自动连线中获取repo实例,我在堆栈跟踪中得到的唯一结果就是调用存储库函数时出现空指针异常。请尝试将方法名称findAllSchools更改为findAll。(我很感谢你的帮助,但是我尝试过在applicationContext.xml中将属性添加到我的entitymanagerfactory bean中,但是没有成功