简体   繁体   中英

How can i establish a one to one relationship between this two classes using spring boot jpa?, also, how do i save the entities?

This is my Alias record class, it has a one to one relationship to the personal details class:

@Entity
public class AliasRecord
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    private String policeId;
    private String enrollmentId;
    @NotNull
    private boolean isVerified;

    @NotEmpty(message = "status not set")
    private String status;

   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name = "personalDetails", referencedColumnName = "id")
    private PersonalDetails personalDetails;


    public AliasRecord(String policeId, String enrollmentId, @NotNull boolean isVerified, @NotEmpty(message = "status not set") String status) {
        this.policeId = policeId;
        this.enrollmentId = enrollmentId;
        this.isVerified = isVerified;
        this.status = status;
    }

    public AliasRecord(String policeId, String enrollmentId, @NotNull boolean isVerified,
                       @NotEmpty(message = "status not set") String status, PersonalDetails personalDetails) {
        this.policeId = policeId;
        this.enrollmentId = enrollmentId;
        this.isVerified = isVerified;
        this.status = status;
        this.personalDetails = personalDetails;
    }

    public AliasRecord() {
   }

}

Here is my personal details class:

@Entity
public class PersonalDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    private String title;
    private String firstName;
    private String middleName;
    private String previousMiddleName;
    private String otherNames;
    private String surName;
    private String previousSurName;
    private String maidenName;
    private String email;
    private String phone;
    private String gender;
    private String dob;
    private String stateOfBirth;
    private String countryOfBirth;
    private String dobVerification;
    private String enrolmentType;
    private String photo;

    @OneToOne(mappedBy = "personalDetails")
    private AliasRecord record;


    public PersonalDetails() {
    }

    public PersonalDetails(String title, String firstName, String middleName, String previousMiddleName, String otherNames, String surName, String previousSurName, String maidenName, String email, String phone, String gender, String dob, String stateOfBirth, String countryOfBirth, String dobVerification, String enrolmentType, String nin, String photo, AliasRecord record) {
        this.title = title;
        this.firstName = firstName;
        this.middleName = middleName;
        this.previousMiddleName = previousMiddleName;
        this.otherNames = otherNames;
        this.surName = surName;
        this.previousSurName = previousSurName;
        this.maidenName = maidenName;
        this.email = email;
        this.phone = phone;
        this.gender = gender;
        this.dob = dob;
        this.stateOfBirth = stateOfBirth;
        this.countryOfBirth = countryOfBirth;
        this.dobVerification = dobVerification;
        this.enrolmentType = enrolmentType;
        this.nin = nin;
        this.photo = photo;
        this.record = record;
    }
}

This is how I save the entities using Crud Repository:

AliasRecord record = new AliasRecord("12","1222",true,"status");
       AliasRecord aliasRecord = aliasRepository.save(record);
       PersonalDetails details = new PersonalDetails("d","","","","","",
                       "","","","","","","",
                       "","34","4444","","cccc",
                       aliasRecord);

       personalDetailsRepository.save(details);

I just put random values in the constructor just to see that the code works, anyway, when I try to save the entities, the column "personalDetails" appears in the AliasRecord table as a column for the foreign key, but its always null.How do I go about creating a one to one relationship between this two entities and how do I go about saving the entities to a MySQL db?.

Dont use bidirectional mapping in OneToOne relationship. If "PersonalDetails" depends on "AliasRecord" add OneToOne mapping in to "PersonalDetails" entity. Identify the most significant entity and add OneToOne mapping into that entity. If you add OneToOne mapping in both sides then you cant save because both expect FK values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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