我正在尝试使用Spring Data JPA - 没有任何实现的CrudRepository(所有默认选项)。
使用我的代码我有这个例外:
线程中的异常" main" org.springframework.beans.factory.BeanCreationException:使用名称' springJpaContactService'创建bean时出错:注册自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.sample.hibernate.ContactRepository com.sample.hibernate.RepositoryContactServiceImpl.contactRepository;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为' contactRepository'的init时出错:init方法的调用失败;嵌套异常是java.lang.AbstractMethodError:org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg / springframework / data / repository / core / RepositoryInformation;)Ljava / lang / Object; at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834) 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) 在com.sample.hibernate.HibernateJpaApplication.main(HibernateJpaApplication.java:15)
我的课程负责CrudRepository:
ContactRepository :
package com.sample.hibernate;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface ContactRepository extends CrudRepository<Contact, Long> {
List<Contact> findByFirstName(String firstName);
List<Contact> findByFirstNameAndLastName(String firstName, String lastName);
}
RepositoryContactService :
package com.sample.hibernate;
import java.util.List;
public interface RepositoryContactService {
List<Contact> findAll();
List<Contact> findByFirstName(String firstName);
List<Contact> findByFirstNameAndLastName(String firstName, String lastName);
}
RepositoryContactServiceImpl :
package com.sample.hibernate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
@Service("springJpaContactService")
@Repository
@Transactional
public class RepositoryContactServiceImpl implements RepositoryContactService {
@Autowired
private ContactRepository contactRepository;
@Transactional(readOnly = true)
public List<Contact> findAll() {
return Lists.newArrayList(contactRepository.findAll());
}
@Transactional(readOnly = true)
public List<Contact> findByFirstName(String firstName) {
return contactRepository.findByFirstName(firstName);
}
@Transactional(readOnly = true)
public List<Contact> findByFirstNameAndLastName(String firstName,
String lastName) {
return contactRepository
.findByFirstNameAndLastName(firstName, lastName);
}
}
与:
package com.sample.hibernate;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@Entity
@Table(name = "contact")
@NamedQueries({
@NamedQuery(name="Contact.findAll", query="select c from Contact c"),
@NamedQuery(name = "Contact.findAllWithDetail", query = "select distinct c from Contact c left join fetch c.contactTelDetails t left join fetch c.hobbies h"),
@NamedQuery(name = "Contact.findById", query = "select distinct c from Contact c left join fetch c.contactTelDetails t left join fetch c.hobbies h where c.id = :id") })
@SqlResultSetMapping(name="contactResult", entities=@EntityResult(entityClass=Contact.class)
)
public class Contact implements Serializable {
private static final long serialVersionUID = -8008307767408320097L;
private Long id;
private int version;
private String firstName;
private String lastName;
private Date birthDate;
private Set<ContactTelDetail> contactTelDetails = new HashSet<ContactTelDetail>();
private Set<Hobby> hobbies = new HashSet<Hobby>();
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
@Column(name = "VERSION")
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Temporal(TemporalType.DATE)
@Column(name = "BIRTH_DATE")
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@OneToMany(mappedBy = "contact", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<ContactTelDetail> getContactTelDetails() {
return contactTelDetails;
}
public void setContactTelDetails(Set<ContactTelDetail> contactTelDetails) {
this.contactTelDetails = contactTelDetails;
}
public void addContactTelDetail(ContactTelDetail contactTelDetail) {
contactTelDetail.setContact(this);
getContactTelDetails().add(contactTelDetail);
}
public void removeContactTelDetail(ContactTelDetail contactTelDetail) {
getContactTelDetails().remove(contactTelDetail);
}
@ManyToMany
@JoinTable(name = "contact_hobby_detail", joinColumns = @JoinColumn(name = "CONTACT_ID"), inverseJoinColumns = @JoinColumn(name = "HOBBY_ID"))
public Set<Hobby> getHobbies() {
return hobbies;
}
public void setHobbies(Set<Hobby> hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "Contact [id=" + id + ", version=" + version + ", firstName="
+ firstName + ", lastName=" + lastName + ", birthDate="
+ birthDate + "]";
}
}
应用-config.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:sql/schema.sql" />
<jdbc:script location="classpath:sql/test-data.sql" />
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.sample.hibernate" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<context:component-scan base-package="com.sample.hibernate" />
<jpa:repositories base-package="com.sample.hibernate"
entity-manager-factory-ref="emf" transaction-manager-ref="transactionManager" />
</beans>
我会很高兴有任何线索..这个例子来自一本书,我已经用它检查了3次+在线教程,我不知道为什么它没有按预期工作......
答案 0 :(得分:1)
带注释的所有内容都很好 - 无需将@Repository移动到ContactRepository。这是因为ContactRepository扩展了CrudRepository,它告诉Spring他可以在RepositoryContactServiceImpl中通过引用自动装配。问题出在maven配置中(正如你正确思考并感谢)。我改变了:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.M2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
到
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
现在存储库按预期工作。
答案 1 :(得分:0)
您的@Repository
注释位置错误。您的ContactRepository
课程应该是使用@Repository
注释的课程。
Spring无法自动装配您的ContactRepository
,因为它不是由Spring管理的,因为您没有使用@Repository
对其进行注释。
此外,您@Transactional
上有RepositoryContactServiceImpl
,这会使该类中的所有公共方法都是事务性的,因此将@Transactional
放在该类中的每个方法上都是多余的。如果您需要每种方法具有不同的事务行为,则可以在每个方法上保留@Transactional
注释,并针对每种方法进行修改以满足您的需要。然后,您可以从班级中删除@Transactional
。
但是,如果每个方法都需要相同类型的事务(如提供的示例所示),则可以从每个方法中删除@Transactional
注释,并将@Transactional
保留在类级别,并指定你那里的readOnly
财产。