Reputation: 43
I am trying to get all info from User
entity which include shoes that user have from Shoes
entity. When I try to call findAll()
, it gets only the information from one table but ignores the joined table. Please tell me what I am doing wrong.
//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
I'm using 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> {
}
The Controllers:
@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);
} }
I'm using Postman to test: http://localhost:8080/v1/users/user get a JSON:
[
{
"userId": 1,
"userLogin": "fsdfsdf",
"userPassword": "sdfsdfsdf",
"userFirstName": "fsdfds",
"userLastName": "fsdfsd",
"userEmail": "dfsf",
"userBirthDay": "sdfsdfs",
"userFacebookId": "sdfdsfsd",
"userGoogleId": "fsdfsd",
"userAvatar": "sdsdf"
}
]
But with no shoes property.
http://localhost:8080/v1/items/shoes
[
{
"shoesId": 1,
"shoesCode": 23432234,
"shoesColor": "dsfsdf",
"shoesSeason": "fsdfs",
"shoesType": "fsdsdf",
"shoesPicture": "sdfsdf",
"shoesShortDescription": "dfsdfsd",
"shoesFullDescription": "sdfsdf",
"shoesPrice": "sdfsd"
}
]
But with no user property.
Upvotes: 0
Views: 1334
Reputation: 43
Found resolusion in main class. I really don't know why someone add annotations:
@Import(RepositoryRestMvcConfiguration.class)
@ComponentScan({"com.ua.demoClothes"})
@EntityScan("com.ua.demoClothes.entity")
@JsonAutoDetect
but when it was removed it started workind propertly. Just with
@SpringBootApplication
in main class
and
@JsonBackReference
@JsonManagedReference
in entities class
Upvotes: 1