无法在DAO

时间:2016-06-01 14:39:59

标签: java hibernate spring-mvc spring-data-jpa

我目前正在将JPA集成到我之前使用HibernateTemplate的项目中。

我做了以下配置:

@Configuration
@EnableJpaRepositories("package1, package2")
@EnableTransactionManagement
public class JpaConfiguration {


    @Bean
    public EntityManagerFactory entityManagerFactory(){

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        vendorAdapter.getJpaDialect();

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("package1, package2");
        factory.afterPropertiesSet();

        return factory.getObject();
    }

下面是我试图调用存储库的类,但当我将鼠标悬停在调用上时,我收到错误:no-static field applicantEntryRepository cannot be referenced from a static context

public static PsHeldSkillEntry retrievePsHeldSkillEntry(SkillEntry primaryKey) throws DataAccessException {
    //HibernateTemplate ht = new HibernateTemplate(ComponentBuilder.getSessionFactory());

    //SkillEntry skill = ht.get(SkillEntry.class, primaryKey);

    SkillEntry skill = applicantEntryRepository.getOne(primaryKey);

    return skill;
}


}

applicantEntryRepository在类的顶部自动装配,如下所示:

@Controller
public class ApplicantEntryDAO {

    @Autowired
    ApplicantEntryRepository applicantEntryRepository;

存储库

public interface ApplicantEntryRepository extends JpaRepository<SkillEntry,Long> {


}

Enitiy:

@Entity
@Table(name = "HELD_SKILL")
@IdClass(SkillEntryPK.class)
public class SkillEntry
{

1 个答案:

答案 0 :(得分:0)

你的问题是自动装配还是spring只是java问题,你的调用applicantEntryRepository在静态方法中不是静态的,你必须首先使这个变量静态

@Controller
public class ApplicantEntryDAO {

@Autowired
static ApplicantEntryRepository applicantEntryRepository;