Я пытаюсь получить всю информацию от User
сущности, которая включает туфли, которые есть у пользователя от Shoes
сущности. Когда я пытаюсь вызвать findAll()
, он получает только информацию из одной таблицы, но игнорирует объединенную таблицу. Пожалуйста, скажите мне, что я делаю не так.
//Shoes
@Entity
public class Shoes implements Serializable {
private Integer shoesId;
private Integer shoesCode;
private String shoesColor;
private String shoesSeason;
private String shoesType;
private String shoesPicture;
private String shoesShortDescription;
private String shoesFullDescription;
private String shoesPrice;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "userShoes")
private Set<User> userSet;
//getters and setters
//User
@Entity
public class User implements Serializable {
private Integer userId;
private String userLogin;
private String userPassword;
private String userFirstName;
private String userLastName;
private String userEmail;
private String userBirthDay;
private String userFacebookId;
private String userGoogleId;
private String userAvatar;
@ManyToMany(targetEntity = Shoes.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "user_shoes",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "shoes_id")})
private Set<Shoes> userShoes;
//getters and setters
Я использую JpaRepository:
@Repository
@Transactional
public interface ShoesRepository extends JpaRepository<Shoes, Serializable> {
List<Shoes> findShoesByShoesColorAndShoesSeason(String shoesColor, String shoesSeason);
}
@Repository
@Transactional
public interface UserRepository extends JpaRepository<User, Serializable> {
}
Контроллеры:
@RestController
@RequestMapping("/v1/items")
public class ShoesController {
private ShoesService shoesService;
@Autowired
public ShoesController(ShoesService shoesService) {
this.shoesService = shoesService;
}
@GetMapping("/shoes/test")
public String hello(){
return "HelloWorld";
}
@GetMapping(path = "shoes")
public ResponseEntity<Iterable<Shoes>> getAll(){
List<Shoes> allshoes = new ArrayList<>();
shoesService.findAll().iterator().forEachRemaining(allshoes::add);
if (allshoes.isEmpty()){
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(allshoes, HttpStatus.OK);
} }
@RestController
@RequestMapping("v1/users")
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping(path = "user")
public ResponseEntity<List<User>> getAll(){
List<User> allUsers = new ArrayList<>();
userService.findAll().iterator().forEachRemaining(allUsers::add);
if (allUsers.isEmpty()){
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(allUsers, HttpStatus.OK);
} }
Я использую Почтальон для проверки:
http://localhost:8080/v1/users/user
получите JSON:
[
{
"userId": 1,
"userLogin": "fsdfsdf",
"userPassword": "sdfsdfsdf",
"userFirstName": "fsdfds",
"userLastName": "fsdfsd",
"userEmail": "dfsf",
"userBirthDay": "sdfsdfs",
"userFacebookId": "sdfdsfsd",
"userGoogleId": "fsdfsd",
"userAvatar": "sdsdf"
}
]
Но без свойства обуви.
http://localhost:8080/v1/items/shoes
[
{
"shoesId": 1,
"shoesCode": 23432234,
"shoesColor": "dsfsdf",
"shoesSeason": "fsdfs",
"shoesType": "fsdsdf",
"shoesPicture": "sdfsdf",
"shoesShortDescription": "dfsdfsd",
"shoesFullDescription": "sdfsdf",
"shoesPrice": "sdfsd"
}
]
Но без свойства пользователя.