简体   繁体   English

JPA双向OneToOne无法正常工作

[英]JPA Bidirectional OneToOne Not working

I try to build a bidirectional relationship. 我尝试建立双向关系。 I am using Spring Boot 1.5.4.RELEASE with Spring Boot JPA to generate my repositories. 我正在将Spring Boot 1.5.4.RELEASE与Spring Boot JPA一起使用来生成我的存储库。 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. 我使用Spring JPA生成存储库。

DriverRepository: DriverRepository:

@Repository
public interface DriverRepository extends CrudRepository<Driver, Long> { }

CarRepository: 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. 感谢您的建议。

Several mistakes in what you posted. 您发布的内容有几个错误。

First: 第一:

@OneToOne(mappedBy = Driver.COLUMN_CAR)

mappedBy expects the name of the Java field/property on the other side of the association. mappedBy在关联的另一端需要Java字段/属性的名称。 Not the name of the database column. 不是数据库列的名称。 It works here because both happen to have the same name. 之所以在这里起作用,是因为它们碰巧具有相同的名称。

Second: 第二:

carRepository.findAll().forEach(eachCar -> Assert.assertNotNull(eachCar.getDriver()));

That fails simply because you're doing everything in a single transaction, and you failed to properly initialize the two sides of the association. 失败仅是因为您在单个事务中完成了所有操作,并且未能正确初始化关联的两侧。 So car.driver is just as you initialized it: null. 所以car.driver就像您初始化它一样:null。

Third: 第三:

driverRepository.findAll().forEach(eachDriver -> Assert.assertNotNull(eachDriver.getCar()));

You made the same mistake as before, but worse. 您犯了和以前一样的错误,但是更糟。 Here, you only initialized one side of the association, but you initialized the inverse side of the association (the one which has the mappedBy attribute). 在这里,您仅初始化了关联的一侧,但是初始化了关联的侧(具有mappedBy属性的那一侧)。 So the association won't even be saved in the database, as it would have been in your previous snippet. 因此,该关联甚至都不会保存在数据库中,就像以前的片段中那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM