1

I have two Entity classes, each for my Table. They both are OneToOne-Mapped. When I read the Data, am always getting the other table's value as null.

These are my SQLs

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)
)

These are my Entity classes

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;

}

This is my DriverMasterRepository

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

This is my UserMasterRepository

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

I am creating DriverMaster and UserMaster at the same time. Code Snippet below

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);
    }

When i access each repository and get the data, the data for that particular table is getting populated while the referenced Object is always coming as Null.

After creation of DriverMaster, I couldn't get UserMaster within DriverMaster, it is always coming as null .

The REST Response below shows that UserMaster within DriverMaster is coming as 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
}
Arun
  • 3,440
  • 11
  • 60
  • 108

1 Answers1

0

it's not a 100% problem's reason, but I believe you have to add an implementation of Serializableinterface into your entity classes.

Yuriy Tsarkov
  • 2,461
  • 2
  • 14
  • 28