Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何重用Spring数据JpaRepository接口_Java_Spring_Oop_Spring Data Jpa - Fatal编程技术网

Java 如何重用Spring数据JpaRepository接口

Java 如何重用Spring数据JpaRepository接口,java,spring,oop,spring-data-jpa,Java,Spring,Oop,Spring Data Jpa,(使用Java8) 我有一个SpringDataJPA存储库接口,我在持久层中使用它。它负责A桌 以下是接口的代码: package com.bla.bla.persistence; import com.bla.bla..EntityA; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.spring

(使用Java8)

我有一个SpringDataJPA存储库接口,我在持久层中使用它。它负责A桌

以下是接口的代码:

package com.bla.bla.persistence;

import com.bla.bla..EntityA;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
public interface TableAJPARepository extends JpaRepository<EntityA, Long> {

    @Query("select distinct i.username from EntityA i")
    List<Integer> findDistinctUsernames();

    @Modifying
    @Query("update EntityA i set i.firstName = ?3 where i.lastName = ?1 and i.age < ?2")
    int updateEntityA(Integer age, String firstName, String lastName);

    @Query("select i from EntityA i where i.firstName = ?1 and (i.age =?2 or i.lastName =?3)")
    List<EntityA> findByFirstNameAndAgeOrLastName(String firstName, Integer age, String lastName);

    @Query("select DISTINCT(i) from EntityA i, EntityA1 r  where r.bookId=i.id " +
            "and i.firstName=?1 and r.title=?2 and i.lastName=?3 and i.age!=2 and i.age<=?4 " +
            "order by i.firstName Desc, i.age DESC ")
    List<EntityA> findBySomeFields(String firstName, String title, String lastName, Integer age,  Pageable pageable);
    ...
}
package com.bla.bla.persistence;
导入com.bla.bla..EntityA;
导入org.springframework.data.domain.Pageable;
导入org.springframework.data.jpa.repository.JpaRepository;
导入org.springframework.data.jpa.repository.Modifying;
导入org.springframework.data.jpa.repository.Query;
导入org.springframework.transaction.annotation.Transactional;
导入java.util.List;
@交易的
公共接口表JpaRepository扩展了JpaRepository{
@查询(“从EntityA i中选择不同的i.username”)
列出findDistinctUsernames();
@修改
@查询(“更新EntityA i set i.firstName=?3,其中i.lastName=?1和i.age<?2”)
int updateEntityA(整数年龄、字符串名、字符串名);
@查询(“从实体i中选择i,其中i.firstName=?1和(i.age=?2或i.lastName=?3)”)
列出FindByFirstName和GeorgelastName(字符串firstName、整数年龄、字符串lastName);
@查询(“从EntityA i、EntityA1 r中选择不同的(i),其中r.bookId=i.id”+

“I.FieldNe==1和R.Tee==2,I.LaSTeNe==3,I.AGE=2,I.AGE

可以考虑制作自定义存储库-请注意@ NoePosiTyryBeabyPosi-

import org.springframework.data.repository.*;

@NoRepositoryBean
public interface YourRepository<T, ID extends Serializable> extends Repository<T, ID> {
}
import org.springframework.data.repository.*;
@菜豆
公共接口YourRepository扩展了存储库{
}
第一个方法不需要@Query,因为Distinct是spring jpa上的一个方法名选项-

List<T> findDistinctUsernames();
列出findDistinctUsernames();

ID需要对更新进行思考——但您可能会使用泛型来为

您可以考虑制作自定义存储库-请注意@ NoePosiTyryBead注释-< /P>
import org.springframework.data.repository.*;

@NoRepositoryBean
public interface YourRepository<T, ID extends Serializable> extends Repository<T, ID> {
}
import org.springframework.data.repository.*;
@菜豆
公共接口YourRepository扩展了存储库{
}
第一个方法不需要@Query,因为Distinct是spring jpa上的一个方法名选项-

List<T> findDistinctUsernames();
列出findDistinctUsernames();

我需要考虑一下更新-但是您可能会使用泛型来解决这个问题

这并不能解决主要问题-更改查询中的实体名称。其中有很多。您定义一个泛型NoRepositoryBean-然后有两个扩展表a和B的存储库,但表/实体名称在
@查询中是一个常量。这并没有解决主要问题-更改查询中的实体名称。其中有很多。您定义了一个通用的NoRepositoryBean-然后有两个扩展表a和B的存储库,但表/实体名称在
@query
中是一个常量。为什么您甚至有很多这样的方法?似乎您想绕过它JPA…(Spring将为您生成查询,而不是您需要指定它).Use.@M.Deinum,这些只是示例。其他查询具有联接和排序。另一个问题是将联接表
A1
更改为表
B1
。@manish,感谢该链接。我可能应该在问题中提到这一点,但一些查询具有到必须更改的辅助表
A1
的联接e到
B1
,使用表
B
。我将把它添加到问题中…您需要做一些类似于
的操作,从#{实体}e中选择不同的e连接e.children c,其中e.firstName=?1和c.title=?2…
而不是显式命名连接类。当然,任何具有公共(可重用)的策略只有当所有实体都是多态的(即,
EntityA
有一个
children
集合,
EntityB
等等)时,base才会起作用。多态对象的确切类型无关紧要。为什么你甚至有很多这样的方法?似乎你想绕过JPA。。。(Spring将为您生成查询,而不是您需要指定它).Use.@M.Deinum,这些只是示例。其他查询具有联接和排序。另一个问题是将联接表
A1
更改为表
B1
。@manish,感谢该链接。我可能应该在问题中提到这一点,但一些查询具有到必须更改的辅助表
A1
的联接e到
B1
,使用表
B
。我将把它添加到问题中…您需要做一些类似于
的操作,从#{实体}e中选择不同的e连接e.children c,其中e.firstName=?1和c.title=?2…
而不是显式命名连接类。当然,任何具有公共(可重用)的策略只有当所有实体都是多态的(即
EntityA
children
集合,以及
EntityB
等)时,base才会起作用。多态对象的确切类型无关紧要。