DevShivmohan / Learning-everything

Learning for developer only
0 stars 1 forks source link

Mapping related issues and solutions in Spring boot #14

Open DevShivmohan opened 2 years ago

DevShivmohan commented 2 years ago

Issue

When it's throw hibernateLazyInitializer error to load mapped object when fetchType=FetchType.LAZY then handle it via annotate @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) with it's getter.

Example

Inside customer entity declared PrimaryContact and SecondaryContact. image

DevShivmohan commented 1 year ago

User, ToDoList and ToDoItem entity mapping

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 -