简体   繁体   English

Hibernate本机查询方法

[英]Hibernate native query method

So, I am doing an assignment where I have a 2 Entities - User and Car with a OneToMany relationship respectively. 因此,我正在做一个分配,其中有2个实体- User and Car分别具有OneToMany关系。

Therefore User will posses a List of Cars . 因此, User将拥有Cars列表。

I need to implement a controller method, which will be displaying all the cars that user posses by UserId(which is a foreign key for Car entity) 我需要实现一个控制器方法,该方法将显示用户通过UserId(这是Car实体的外键)拥有的所有汽车

Method is: [GET] - /users/{id}/cars 方法是: [GET] - /users/{id}/cars

Right now If I am simply seleting all users I am getting raw JSON smth like that: 现在,如果我只是删除所有用户,那么我将得到原始的JSON smth:

{
  "id":"1",
  "name":"Taavet Prulskih",
  "cars":[{
    "Id":"1",
    "make":"BMW",
    "model":"760",
    "numberplate":"123FFF"
  },
  {
    "Id":"2",
    "make":"Opel",
    "model":"Astra",
    "numberplate":"789BFX"
  }]
}

But I don't want to have a data about User, because it's not necessary as I am already specifing in get request that I am selecting a certain user. 但是我不想拥有有关User的数据,因为没有必要,因为我已经在get请求中指定了要选择某个用户的信息。 So I wan't to have only the list of cars that certain user posses. 因此,我不想只拥有某些用户拥有的汽车列表。

The JSON output that I am looking for is: 我正在寻找的JSON输出是:

{  
   "Id":"1",
   "make":"BMW",
   "model":"760",
   "numberplate":"123FFF"
},
{  
   "Id":"2",
   "make":"Opel",
   "model":"Astra",
   "numberplate":"789BFX"
}

ATM I have a CarRepository which extends JpaRepository with a method: ATM上有一个CarRepository,它通过以下方法扩展了JpaRepository:

@Query(value = "SELECT * FROM Cars c where c.id = :id", nativeQuery = true)
    Optional<Car> findByUserId(@Param("id") Long id);

But for some reason I am getting a "java.util.NoSuchElementException: No value present" exception. 但是由于某种原因,我收到了“ java.util.NoSuchElementException:无值”异常。 When trying to use findByUserId Query method. 尝试使用findByUserId查询方法时。

Controller part that doesnt work: 控制器部分不起作用:

@GetMapping("/users/{id}/cars")
    public ResponseEntity<Car> getAllCarsByUserId(@PathVariable("id") Long id)
    {
        return ResponseEntity.ok(carRepository.findByUserId(id).get());
    }

Repository: 仓库:

public interface CarRepository extends JpaRepository<Car, Long> {

    @Query(value = "SELECT * FROM Cars c where c.user_id = :id", nativeQuery = true)
    List<Car> findByUserId(@Param("id") Long id);

}

Could you be so cool to help me to geet done with that issue? 您能帮助我解决这个问题吗?

Thank You in advance! 先感谢您! :) :)

Here is my understanding of what you are looking for: 这是我对您要寻找的内容的理解:

Repositoy 知识库

@Repository
public interface CarRepository extends JpaRepository<Car, Long> {

  @Query(value = "SELECT * FROM T_CARS c where c.USER_ID = :id", nativeQuery = true)
  Collection<Car> findByUserId(@Param("id") Long id);

}

Controller 控制者

class UserController {

  @Autowired
  private CarRepository carRepository;

  @GetMapping("/users/{id}/cars")
  public ResponseEntity<Collection<Car>> getAllCarsByUserId(@PathVariable("id") Long id) {
      return ResponseEntity.ok(carRepository.findByUserId(id));
  }

}

I hope that it works for you :) 我希望它对您有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM