0

User.java

package com.spring.demo.model;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="user")
public class User {



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

    private String fName;

    private String lName;

    @Column(unique=true,nullable=true)
    private String email;

    @Column(unique=true,nullable=true)
    private long mobile;

    private Date dob;

    @Lob
    private byte[] image;   

    @Transient
    private String base64Image;

    @OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
    @JoinColumn(name="userCredential_id")
    private UserCredential userCredential;

    @OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
    @JoinColumn(name="add_id")
    private Address address;



    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public long getMobile() {
        return mobile;
    }

    public void setMobile(long mobile) {
        this.mobile = mobile;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public UserCredential getUserCredential() {
        return userCredential;
    }

    public void setUserCredential(UserCredential userCredential) {
        this.userCredential = userCredential;
    }



}

UserCredential.java

package com.spring.demo.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name="usercredential")
public class UserCredential {

    @Id
    @Column(name="credential_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(unique=true,nullable=true)
    private String username;

    private String password;
    private String cnfrmpassword;



    @JsonIgnore
    @OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
    @JoinColumn(name="user_id",nullable=true)
    private User user;



    public UserCredential() {
        super();
        // TODO Auto-generated constructor stub
    }


    public UserCredential(int id, String username, String password, String cnfrmpassword, User user) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.cnfrmpassword = cnfrmpassword;
        this.user = user;
    }


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getCnfrmpassword() {
        return cnfrmpassword;
    }
    public void setCnfrmpassword(String cnfrmpassword) {
        this.cnfrmpassword = cnfrmpassword;
    }

    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

}

Address.java

package com.spring.demo.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name="address")
public class Address {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="add_id")
    private int id;

    @Column(name="city")
    private String city;

    @Column(name="state")
    private String state;

    @Column(name="house_no")
    private String h_no;

    @JsonIgnore
    @OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
    @JoinColumn(name="user_id", nullable=true)
    private User user;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getH_no() {
        return h_no;
    }

    public void setH_no(String h_no) {
        this.h_no = h_no;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

Here we have user as a parent table and (usercredential and address) are child classes in a relationship. When I insert data into tables then every primary key automatically incremented and get the appropriate value while the foreign key (user_id) always remains zero. https://i.stack.imgur.com/Pivlm.jpg

https://i.stack.imgur.com/fAPth.jpg

https://i.stack.imgur.com/l37mr.jpg

My concern is user_id(foreign key) in child tables should not be null and equals to primary key(user_id) in parent table. Please look for every cascading(delete, update) operation should be implemented well on table.

Further information I am using Json for inserting data into tables.

{
"fName":"sur kumst",
"lName":"adfdf",
"mobile":45106,
"email":"ksusjasd1sd@gmail.com",
"dob":"2012-04-23T18:25:43.511Z",
"address":{
    "city":"noida",
    "state":"up",
    "h_no":"1243"
},
"userCredential":{

    "username":"kr0302",
    "password":"12345",
    "cnfrmpassword":"12345"
}
}
  • enter image description here <----- – mavriksc Sep 11 '18 at 19:59
  • so that is a url not a description of the image. – mavriksc Sep 11 '18 at 20:28
  • 1) How do you add the user to the Address and UserCredential entities? - add the code, please. 2) Try to annotate only OneToOne (cascade = CascadeType.ALL). @JsonIgnore, JoinColumn must be predominantly in ManyToOne / annotations. 3) Also you may no annotate classes Address and UserCredential in class User if you use OneToAone logic in table. (only User necessary in classes) – Valentyn Hruzytskyi Sep 11 '18 at 21:37

2 Answers2

0

The issue is with the back reference. Hibernate cannot maintain this for you. Say you save your user object. it creates a credential row and generates id. it creates address and id. it updates the cred_id and add_id on the user object and then creates a row for it and generates id and returns that value. at this point you need to add your user object to credential and address and save those again.

mavriksc
  • 1,130
  • 1
  • 7
  • 10
  • I have already add the user object in both the tables as you have mentioned. please look thoroughly and help me out of this. – Suraj Kumar Sep 12 '18 at 06:58
  • see @jens-schauder note... debug the appllication and see what the values are for the user property of the creds and address are. they will be null... set them and save again..Add the code where you create and save the user. – mavriksc Sep 12 '18 at 14:35
0

It seems you are trying to model two bidirectional relationships:

User <-> UserCredentials and:

User <-> UserAddress.

But what you are really creating the following four relationships:

User -> UserCredentials

User <- UserCredentials

User -> UserAddress

User <- UserAddress

In order to fix this you need to use mappedBy. See this question for reference.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348