I tried to test my repository from JpaRepository in my Spring Boot Application but i get this error:
java.lang.IllegalStateException: Failed to load ApplicationContext
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carDealerRepository' defined in schule.privas.microservice.repository.CarDealerRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: schule.privas.microservice.model.Car.cardealer in schule.privas.microservice.model.CarDealer.cars
(The Exception list is a lot longer but i think that is the only interesting part)
Here is the code of my Repository Test
@Transactional
@SpringBootTest
public class CarDealerRepositoryTest {
@Autowired
private CarDealerRepository carDealerRepository;
private CarDealer carDealer;
private Car car;
@BeforeEach
void initialize(){
carDealer = CarDealer.builder()
.cars(new ArrayList<Car>())
.address("schibinabi")
.sales(10000f)
.build();
car = Car.builder()
.brand("McLaren")
.maxSpeed(300)
.price(10500f)
.build();
carDealer.addCar(car);
}
@Test
void verifyFindAll(){
Iterable<CarDealer> carDealerList = carDealerRepository.findAll();
assertThat(carDealerList).isNotNull();
assertThat(carDealerList).isEmpty();
}
}
I tried to use a break point but it doesnt even comes to the code i wrote.
Here are the classes of Car
and CarDealer
CarDealer:
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
@Setter
public class CarDealer extends AbstractPersistable<Long> {
private Float sales;
private String address;
@OneToMany(mappedBy = "cardealer", cascade = CascadeType.PERSIST)
private List<Car> cars = new ArrayList<Car>();
public void addCar(Car car) {
cars.add(car);
if(!this.equals(car.getCarDealer())){
car.setCarDealer(this);
}
}
public boolean containsCar(Car car) {
return cars.contains(car);
}
}
Car:
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
@Setter
public class Car extends AbstractPersistable<Long> {
private String brand;
private Float price;
private Integer maxSpeed;
@ElementCollection
private List<String> versions;
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JsonIgnore
private CarDealer carDealer;
public void setCarDealer(CarDealer dealer) {
this.carDealer = dealer;
if (!dealer.containsCar(this)) {
dealer.addCar(this);
}
}
}
And here is also the repository interface:
@Repository
public interface CarDealerRepository extends JpaRepository<CarDealer, Long> {
}
You should try to use the same casing as the property in Car.
So it should be:
@OneToMany(mappedBy = "carDealer", cascade = CascadeType.PERSIST)
instead of:
@OneToMany(mappedBy = "cardealer", cascade = CascadeType.PERSIST)