Spring Data JPA OneToOne Mapping返回Null

时间:2018-02-16 15:46:13

标签: spring hibernate spring-boot spring-data-jpa

我有两个Entity类,每个类都用于我的Table。它们都是OneToOne-Mapped。当我读取数据时,总是将另一个表的值设为null。

这些是我的SQL

CREATE TABLE driver_master (
  unique_driver_id VARCHAR(60) PRIMARY KEY,
  driver_name TEXT,
  mobile_number VARCHAR (20),
  vehicle_number TEXT,
  vehicle_make TEXT,
  seating_capacity INTEGER (10),
  creation_time DATETIME
)

CREATE TABLE user_master (
  user_id MEDIUMINT AUTO_INCREMENT PRIMARY KEY,
  user_name TEXT,
  password VARCHAR (20),
  unique_driver_id VARCHAR(60),
  FOREIGN KEY (unique_driver_id) REFERENCES driver_master(unique_driver_id)
)

这些是我的实体类

DriverMaster.java

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "driver_master")
public class DriverMaster {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "unique_driver_id")
    UUID id;

    @Column(name = "driver_name")
    String driverName;


    @Column(name = "mobile_number")
    String mobileNumber;

    @Column(name = "vehicle_number")
    String vehicleNumber;

    @Column(name = "vehicle_make")
    String vehicleMake;

    @Column(name = "seating_capacity")
    Integer seatingCapacity;

    @Column(name = "creation_time")
    OffsetDateTime creationTime;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "driverMaster")
    UserMaster userMaster;

}

UserMaster.java

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "user_master")
public class UserMaster {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    Long id;

    @Column(name = "user_name")
    String userName;

    @Column(name = "password")
    String password;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "unique_driver_id", nullable = false)
    DriverMaster driverMaster;

}

这是我的DriverMasterRepository

public interface DriverMasterRepository extends CrudRepository<DriverMaster, Long> {
    DriverMaster findById(UUID id);
}

这是我的UserMasterRepository

public interface UserMasterRepository extends CrudRepository<UserMaster, Long> {
    UserMaster findById(Long id);
}

我正在同时创建DriverMaster和UserMaster。下面的代码片段

public DriverMaster create() {
        UserMaster userMaster = UserMaster.builder()
                .userName("xxxxx")
                .password("xxxx").build();
        DriverMaster driverMaster = DriverMaster.builder().driverName("TestDriver")
                .creationTime(ZonedDateTime.now().toOffsetDateTime())
                .seatingCapacity(8)
                .mobileNumber("xxxxxxx")
                .vehicleNumber("xxxx")
                .vehicleMake("xxxx")
                 .userMaster(userMaster)
                .build();


        return driverUserService.create(driverMaster);
    }

当我访问每个存储库并获取数据时,该特定表的数据将被填充,而引用的Object始终为空。

在创建DriverMaster之后,我无法在DriverMaster中获得UserMaster,它始终为null。

下面的REST响应显示DriverMaster中的UserMaster将显示为Null

   {
    "id": "0d97073b-6ae2-47a9-b751-0313fd9e8ba2",
    "driverName": "TestDriver",
    "mobileNumber": "11111",
    "vehicleNumber": "111",
    "vehicleMake": "figo",
    "seatingCapacity": 8,
    "creationTime": "2018-02-16T15:56:50.331Z",
    "userMaster": null
}

1 个答案:

答案 0 :(得分:0)

这不是100%问题的原因,但我相信您必须在实体类中添加Serializable接口的实现。