在 List<Object> SpringBoot JPA Repository 中找到 属性
findBy property in List<Object> SpringBoot JPA Repository
我的数据库 Story 和 Tag 中的两个对象之间存在一对多关系。
我希望能够获取所有具有字符串名称的 Tag 对象的 Story 对象。
Story.java
@Entity
@Table(name = "stories")
public class Story {
@Id
@GeneratedValue
private Long id;
@Column(name = "title")
private String title;
@JsonIgnoreProperties({"story"})
@OneToMany(mappedBy = "story", fetch = FetchType.LAZY)
private List<Tag> tags;
public Story(String title){
this.title = title;
}
public Story(){
}
// getters & setters
}
Tag.java
@Entity
@Table(name = "tags")
public class Tag {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String name;
@JsonIgnoreProperties({"tags"})
@ManyToOne
@JoinColumn(name = "story_id", nullable = false)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
private Story story;
public Tag(String name, Story story){
this.name = name;
this.story = story;
}
public Tag(){
}
/// getters & setters
}
StoryController.java
@RestController
public class StoryController {
@Autowired
StoryRepository storyRepository;
@CrossOrigin(origins = "http://localhost:8080/api")
@GetMapping(value = "/stories")
public ResponseEntity<List<Story>> getAllStories(){
return new ResponseEntity<>(storyRepository.findAll(), HttpStatus.OK);
}
@CrossOrigin(origins = "http://localhost:8080/api")
@GetMapping(value="/stories/tagSearch/{name}")
public ResponseEntity<Story> getStoryByTag(@PathVariable String name) {
return new ResponseEntity(storyRepository.findByTags_Name(name), HttpStatus.OK);
}
@CrossOrigin(origins = "http://localhost:8080/api")
@GetMapping(value="/stories/{id}")
public ResponseEntity<Story> getStory(@PathVariable Long id) {
return new ResponseEntity(storyRepository.findById(id), HttpStatus.OK);
}
}
StoryRepository.java
@Repository
public interface StoryRepository extends JpaRepository<Story, Long> {
public List<Story> findByTags_Name(String name);
}
当试图通过浏览器查询时,转到地址 localhost:8080/api/stories/tagSearch/?name="tag" 数据库 returns 数据库中的所有对象而不是我正在寻找的结果。
在您的存储库中执行此操作
@Query("SELECT s FROM Story s left join s.tags t WHERE t.name = ?1")
public List<Story> findByTags_Name(String name);
您需要在 repository
.
中使用以下方法
public List<Story> findByTagsName(String name);
您需要使用以下 URL 来获取详细信息,因为您已将 name
定义为 PathVariable
localhost:8080/api/stories/tagSearch/tag
tag - 是你的名字
我的数据库 Story 和 Tag 中的两个对象之间存在一对多关系。
我希望能够获取所有具有字符串名称的 Tag 对象的 Story 对象。
Story.java
@Entity
@Table(name = "stories")
public class Story {
@Id
@GeneratedValue
private Long id;
@Column(name = "title")
private String title;
@JsonIgnoreProperties({"story"})
@OneToMany(mappedBy = "story", fetch = FetchType.LAZY)
private List<Tag> tags;
public Story(String title){
this.title = title;
}
public Story(){
}
// getters & setters
}
Tag.java
@Entity
@Table(name = "tags")
public class Tag {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String name;
@JsonIgnoreProperties({"tags"})
@ManyToOne
@JoinColumn(name = "story_id", nullable = false)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
private Story story;
public Tag(String name, Story story){
this.name = name;
this.story = story;
}
public Tag(){
}
/// getters & setters
}
StoryController.java
@RestController
public class StoryController {
@Autowired
StoryRepository storyRepository;
@CrossOrigin(origins = "http://localhost:8080/api")
@GetMapping(value = "/stories")
public ResponseEntity<List<Story>> getAllStories(){
return new ResponseEntity<>(storyRepository.findAll(), HttpStatus.OK);
}
@CrossOrigin(origins = "http://localhost:8080/api")
@GetMapping(value="/stories/tagSearch/{name}")
public ResponseEntity<Story> getStoryByTag(@PathVariable String name) {
return new ResponseEntity(storyRepository.findByTags_Name(name), HttpStatus.OK);
}
@CrossOrigin(origins = "http://localhost:8080/api")
@GetMapping(value="/stories/{id}")
public ResponseEntity<Story> getStory(@PathVariable Long id) {
return new ResponseEntity(storyRepository.findById(id), HttpStatus.OK);
}
}
StoryRepository.java
@Repository
public interface StoryRepository extends JpaRepository<Story, Long> {
public List<Story> findByTags_Name(String name);
}
当试图通过浏览器查询时,转到地址 localhost:8080/api/stories/tagSearch/?name="tag" 数据库 returns 数据库中的所有对象而不是我正在寻找的结果。
在您的存储库中执行此操作
@Query("SELECT s FROM Story s left join s.tags t WHERE t.name = ?1")
public List<Story> findByTags_Name(String name);
您需要在 repository
.
public List<Story> findByTagsName(String name);
您需要使用以下 URL 来获取详细信息,因为您已将 name
定义为 PathVariable
localhost:8080/api/stories/tagSearch/tag
tag - 是你的名字