I am trying to successfully map two classes in an OneToOne
relationship; mainly because my User class needs two Ids that are GeneratedValue
. I’ve used Lambook for getter setters and constructors ; I’m using postgres as database.
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
@OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "digital_code", referencedColumnName = "digital_code")
private DigitalCode digitalCode;
@Column (name = "name")
private String name;
@Column (name = "email")
private String email;
}
Second Class
@Entity public class DigitalCode {
@Id
@Column(name = "digital_code", unique = true, nullable = false)
@Size(min = 100000, max = 999999)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long digitalCodeId;
}
Both tables user
and digital_code
do get created in the database. However,
When I post data into my database with body
{
"name": "test name",
"email": "[email protected]",
}
I get 200 back with
{
"digital_code": null,
"name": "test name",
"email": "[email protected]",
"id": 1
}
While I was expecting
{
"digital_code": 100001, // due to @size annotation
"name": "test name",
"email": "[email protected]",
"id": 1
}
New contributor
Ribash Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.