我目前正在将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
{
答案 0 :(得分:0)
你的问题是自动装配还是spring只是java问题,你的调用applicantEntryRepository
在静态方法中不是静态的,你必须首先使这个变量静态
@Controller
public class ApplicantEntryDAO {
@Autowired
static ApplicantEntryRepository applicantEntryRepository;