jpa funciton findfieldbyid()扩展了jparepository不正常工作吗?

发布于 2025-01-23 03:51:53 字数 714 浏览 0 评论 0原文

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
    String findNameById(Integer id);

    int getClassidById(Integer id);
}

我使用了一段时间,它们以前运行良好,但是今天它们都返回实体对象而不是单个属性
例如FindNameById(ID)返回导致

classcastException的学生对象:db.entity.student不能将其施放到java.lang.string
即使它也无法返回地图:

Map<String, Object> findOneById(Integer id);

我的想法会注意到'student'''''''''''''''''''''''''''''

@Query("select s.name from Student s where s.id = ?1")
String findNameById(Integer id);

只有我使用@query它们正常工作。有人有这个问题吗?
(弹簧data-jpa 2.2.5.Release)

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
    String findNameById(Integer id);

    int getClassidById(Integer id);
}

I used those functions for a time and they worked well before, but today all of them return an entity object instead of single property
Such as findNameById(id) returns a Student object which cause
ClassCastException: db.entity.Student cannot be cast to java.lang.String
Even it can't return Map like :

Map<String, Object> findOneById(Integer id);

And My IDEA will notice 'Student' domain type or valid projection interface expected here

@Query("select s.name from Student s where s.id = ?1")
String findNameById(Integer id);

Only do I use @Query that they work properly. Does anyone has this problem?
(spring-data-jpa 2.2.5.RELEASE)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半葬歌 2025-01-30 03:51:53

您的jParepository引用了学生对象。这就是为什么它在该方法参数中返回带有特定ID的学生的原因。

但是,在本机查询中,您明确要求s.name,因此,您要回到该名称的字符串值,

您可以获取对象并从此对象中提取名称,如果您想保留您的JParepository&Lt; Integer&gt;扩展到StudentRepository。从您的JPA中返回可选。然后在您的服务层中提取名称

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
    Optional<Student> findById(Integer id);
}

和服务类,

public class StudentService {
    public void processStudent(){
          Optional<Student> student = studentRepository.findById(1);
          if(student.isPresent()){
            Student studentEntity = student.get();
            String name = studentEntity.getName();
          }
    }
}

Your JPARepository references a Student object. Which is why it returns a Student with a specific Id that you request in that method argument.

However In your Native query, you are explicitly asking for s.name and for that reason you are getting back a String value of that name

You can get the object and extract the name from this object, if you would like to retain your JpaRepository<Student, Integer> extension to the StudentRepository. Get an Optional returned from your JPA. Then in your service layer extract the name

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
    Optional<Student> findById(Integer id);
}

And in your service class,

public class StudentService {
    public void processStudent(){
          Optional<Student> student = studentRepository.findById(1);
          if(student.isPresent()){
            Student studentEntity = student.get();
            String name = studentEntity.getName();
          }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文