2017-11-19 24 views
0

私はスプリングブートアプリケーションを開発していますが、ここで問題に取り掛かります。私は私のRepositoryImpl、RepositoryCustomとリポジトリクラスのために別々のパッケージを持っていると思いますが、私はパッケージを分離する場合、このエラーを取得イム:Spring JPAカスタムリポジトリ別パッケージ

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property customMethod found for type Demo!

それが唯一の私は、同じパッケージに私のRepositoryImpl、RepositoryCustomとリポジトリのクラスを入れたときに動作します。私は@EnableJpaRepositories("com.example.demo.persist")を試しましたが、まだ動作していません。

これを達成する方法はありますか?

ここに私のコードです:

Project packages

DemoApplication.java

@SpringBootApplication 
@EnableJpaRepositories("com.example.demo.persist") 
public class DemoApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(DemoApplication.class, args); 
    } 
} 

DemoController.java

@RestController 
public class DemoController { 

    @Autowired 
    DemoService demoService; 

    @RequestMapping("/test") 
    public String getUnidades() { 
     demoService.customMethod(); 
     return "test"; 
    } 

} 

DemoRepositoryCustom.java

public interface DemoRepositoryCustom { 

    void customMethod(); 

} 

DemoRepositoryImpl.java

public class DemoRepositoryImpl implements DemoRepositoryCustom { 

    @Override 
    public void customMethod() { 
     // do something 
    } 

} 

DemoRepositoryCustom.java

public interface DemoRepository extends JpaRepository<Demo, Long>, DemoRepositoryCustom { 

} 

Demo.java

@Entity 
public class Demo { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id", columnDefinition = "bigint") 
    private int id; 

    @NotEmpty 
    @Column(name = "name", columnDefinition = "VARCHAR(60)", length = 60, unique = false, nullable = false) 
    private String name; 

    // ... 

DemoService.java

application.properties

spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect 

spring.datasource.url=jdbc:mysql://localhost:3306/demo?createDatabaseIfNotExist=true 
spring.datasource.username=root 
spring.datasource.password=root 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

答えて

1

カスタム実装の自動検出は、リポジトリを宣言する1以下のパッケージでのみ動作します。

しかし、必要なクラス名と一致する名前のBeanをBeanにすることができます。 あなたの場合はdemoRepositoryImplになります。

See the documentation for details

1

実際には、パッケージ階層の順序が正しくありません。

はこのようにそれを作る:

com.example.demo.repository 
com.example.demo.repository.custom 
com.example.demo.repository.custom.impl 

そして、それは動作します。