I try to build a bidirectional relationship. I am using Spring Boot 1.5.4.RELEASE with Spring Boot JPA to generate my repositories. I try to save two entities which are associated to each other, but it isnt working. I commented the test-statements which fails.
My Entites:
Driver:
@Entity
@ToString
@EqualsAndHashCode
public class Driver {
public static final String COLUMN_CAR = "car";
@Id
@GeneratedValue
private long id;
@Getter
@Setter
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = COLUMN_CAR)
private Car car;
}
Car:
@Entity
@ToString
@EqualsAndHashCode
public class Car {
@Id
@GeneratedValue
private long id;
@Getter
@Setter
@OneToOne(mappedBy = Driver.COLUMN_CAR)
private Driver driver;
}
I used Spring JPA to generate repositories.
DriverRepository:
@Repository
public interface DriverRepository extends CrudRepository<Driver, Long> { }
CarRepository:
@Repository
public interface CarRepository extends CrudRepository<Car, Long> { }
Test:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class StackoverflowTest {
@Autowired
private DriverRepository driverRepository;
@Autowired
private CarRepository carRepository;
@Test
public void test1() {
Driver driver = driverRepository.save(new Driver());
Car car = carRepository.save(new Car());
driver.setCar(car);
driverRepository.save(driver);
/* Success, so the driver got the car */
driverRepository.findAll().forEach(eachDriver -> Assert.assertNotNull(eachDriver.getCar()));
/* Fails, so the car doesnt got the driver */
carRepository.findAll().forEach(eachCar -> Assert.assertNotNull(eachCar.getDriver()));
}
@Test
public void test2() {
Driver driver = driverRepository.save(new Driver());
Car car = carRepository.save(new Car());
car.setDriver(driver);
carRepository.save(car);
/* Success, so the car got the driver */
carRepository.findAll().forEach(eachCar -> Assert.assertNotNull(eachCar.getDriver()));
/* Fails, so the driver doesnt got the car */
driverRepository.findAll().forEach(eachDriver -> Assert.assertNotNull(eachDriver.getCar()));
}
}
In both tests the last statement fails. Any ideas? Thanks in Advice.