Open DevShivmohan opened 2 years ago
User Entity
package com.woro.assignment.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@AllArgsConstructor
@NoArgsConstructor
@DaTa
public class User {
@id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String username;
private String password;
private String email;
}
ToDoList Entity
package com.woro.assignment.entity;
import javax.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@AllArgsConstructor
@NoArgsConstructor
@DaTa
public class ToDoList {
@id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private User user;
}
ToDoItem Entity
package com.woro.assignment.entity;
import javax.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Entity
@AllArgsConstructor
@NoArgsConstructor
@DaTa
public class ToDoItem {
@id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String title;
private String description;
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private ToDoList toDoList;
private boolean completed;
private Date dueDate;
}
Mapping functionality -
Issue
When it's throw
hibernateLazyInitializer
error to load mapped object whenfetchType=FetchType.LAZY
then handle it via annotate@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
with it's getter.Example
Inside customer entity declared PrimaryContact
andSecondaryContact
.