Reputation: 81
I have an issue with the JPA relationship within a MVC SpringBoot application.
I'm trying to create an application where I have a number of users and every user can have a number of cars. A user can have multiple cars. I've done a @OneToOne relationship between the user and the car object, here's what I've done:
here is the User class:
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", length = 500, nullable = false)
private String password;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Car> cars;
}
then here is the Car class:
@Entity
@Table(name = "car")
public class Car implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(length = 11)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "make", nullable = false)
private String make;
@Column(name = "model", nullable = false)
private String model;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "id")
private User user;
}
Then, here is the actual service implementation
@Component
@Transactional(readOnly = true)
public class CarServiceImpl implements CarService {
@Inject
private CarRepository carRepository;
@Inject
private UserRepository userRepository;
@Override
@Transactional(readOnly = false)
public Car addCar(Long userId, Car car) {
User user = userRepository.findOne(userId);
user.getGpsLocationModels().add(car);
car.setUser(user);
carRepository.save(car);
return car;
}
then I have the endpoint but that works fully. The add method looks like does work as supposed to, at least I get the expected output, however the find method I have no idea how to write it, well can't figure it out how to retrieve cars based on user, I know how to get them by their ID but not for every user separately.
Here is my try:
@Override
public Car findCar(Long userId, Long carId) {
//get the current user (that comes as JSON Request Param)
User user = userRepository.findOne(userId);
//get the car based on its ID, here's the problem, I want the car based on its ID AND its user, I can't display cars which aren't for that particular user
Car car = carRepository.findOne(carId);
return car;
}
Here is the method for get all cars for a particular user:
@Override
public List<Car> displayAllCars(Long userId) {
return userRepository.findOne(userId).getCars();
}
I'd really appreciate any help that you could advise.
Thank you so much
Upvotes: 2
Views: 125
Reputation: 11
You could have a method that accepts the user ID and returns the list in the car repository like:
List<Car> findCarByUser(Long userID);
And then you will have
@Override
public List<Car> displayAllCars(Long userId) {
return carRepository.findCarByUser(userId);
}
Upvotes: 0
Reputation: 23226
Your mappings are incorrect. Car > User is @ManyToOne. If you also make this bi-directional you can also then retrieve the cars via the user:
@Entity
@Table(name = "user")
public class User implements Serializable {
@OneToMany(mappedBy ="user",cascade = CascadeType.ALL)
private Set<Car> cars;
public Set<Car> getCars(){
return cars;
}
public void addCar(Car car){
cars.add(car);
car.setUser(this);
}
}
@Entity
@Table(name = "car")
public class Car implements Serializable {
@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name="user_id")
private User user;
}
@Override
public Set<Car> findCars(Long userId) {
return userRepository.findOne(userId).getCars();
}
Upvotes: 2