首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >此位置不允许使用查询注解

此位置不允许使用查询注解
EN

Stack Overflow用户
提问于 2018-05-28 19:34:56
回答 2查看 5.7K关注 0票数 1

Spring新手入门。

尝试使用@Query注解,但收到消息:此位置不允许注解@Query

一定是一些设置或配置,这里缺少的是我所拥有的:

pom.xml

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.2.RELEASE</version>
 <relativePath/>
</parent>
… to dependency:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>      
</dependency>

我不得不使用1.5.2.RELEASE,因为它是雇主查看Maven依赖项的当前标准: hibernate-core-5.0.12.Final hibernate-jpa-2.1-api-1.0.0.Final.jar spring-data-jpa-1.11.1.RELEASE.jar +许多其他

在我看到的一些示例中,添加@Query注释似乎是一件很容易的事情,但是它似乎不起作用。目前,我只有一个实体、存储库、控制器和一个main。

我尝试过@Repository,但似乎没有什么不同

这是回购

public interface DeptRepo extends JpaRepository<Dept, Long> {

@Query(value = "select d from dept d where name = 'ACCOUNTING'")
List<Dept> findByAccounting;
}

应用程序属性

spring.jpa.show_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/to2b
spring.datasource.username=scott
spring.datasource.password=tiger

我的问题是,我缺少的设置信息是什么?

EN

回答 2

Stack Overflow用户

发布于 2019-11-22 16:54:36

而不是

public interface DeptRepo extends JpaRepository<Dept, Long> {
   @Query(value = "select d from dept d where name = 'ACCOUNTING'")
   List<Dept> findByAccounting;
}

使用

public interface DeptRepo extends JpaRepository<Dept, Long> {
  @Query(value = "select d from dept d where name = 'ACCOUNTING'")
  List<Dept> findByAccounting(); 
}
票数 4
EN

Stack Overflow用户

发布于 2018-05-28 19:55:38

您的存储库包是否映射到configuration类?

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {
        "br.com.example.repository"}, entityManagerFactoryRef = "exampleEntityManager", transactionManagerRef = "exampleTransactionManager")
public class ExampleDataSourceConfig {

    @Primary
    @Bean(name = "exampleEntityManager")
    public LocalContainerEntityManagerFactoryBean exampleEntityManager() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(exampleDataSource());
        em.setPackagesToScan(new String[] { 
                "br.com.example.domain"});

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        final HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", "validate");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");      
        em.setPersistenceUnitName("examplePU");
        em.setJpaPropertyMap(properties);
        return em;
    }

    public DataSource exampleDataSource() {
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource("jdbc/examplejndi");
        return dataSource;

    }

    @Primary
    @Bean(name = "exampleTransactionManager")
    public PlatformTransactionManager exampleTransactionManager() {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(exampleEntityManager().getObject());
        return transactionManager;
    }   
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50565490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档