I have three tables and three entity classes.
CREATE TABLE public.users (
id bigserial NOT NULL,
email_address varchar(255) NOT NULL,
password varchar(100) NOT NULL,
"name" varchar(50) NOT NULL,
phone_number varchar(15) NOT NULL,
status int2 NULL DEFAULT 1,
created_time timestamp(0) NULL DEFAULT now(),
updated_time timestamp(0) NULL,
image bytea NULL,
user_type int2 NULL DEFAULT 0,
CONSTRAINT users_pk PRIMARY KEY (id),
CONSTRAINT users_un UNIQUE (email_address)
);
CREATE TABLE public.modules (
id bigserial NOT NULL,
"name" varchar(100) NOT NULL,
description varchar(2000) NULL,
status int2 NULL,
CONSTRAINT modules_pk PRIMARY KEY (id),
CONSTRAINT modules_un UNIQUE (name)
);
CREATE TABLE public.module_user_mapper (
id bigserial NOT NULL,
user_id int8 NOT NULL,
module_id int8 NOT NULL,
CONSTRAINT module_user_mapper_pk PRIMARY KEY (id)
);
ALTER TABLE public.module_user_mapper ADD CONSTRAINT modules_module_id_fkey FOREIGN KEY (module_id) REFERENCES modules(id);
ALTER TABLE public.module_user_mapper ADD CONSTRAINT modules_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id);
Entity Class
@Entity
@Table(name = "users")
@Data
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Users implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "email_address")
private String emailAddress;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "password")
private String password;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "name")
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 15)
@Column(name = "phone_number")
private String phoneNumber;
@Column(name = "status")
private Short status;
@Column(name = "created_time")
@Temporal(TemporalType.TIMESTAMP)
private Date createdTime;
@Column(name = "updated_time")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedTime;
@Column(name = "image")
private byte[] image;
@Column(name = "user_type")
private Short userType;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
private Collection<ModuleUserMapper> moduleUserMapperCollection;
}
@Entity
@Table(name = "modules")
@Data
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Modules implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "name")
private String name;
@Size(max = 2000)
@Column(name = "description")
private String description;
@Column(name = "status")
private Short status;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "moduleId")
private Collection<ModuleUserMapper> moduleUserMapperCollection;
}
@Entity
@Table(name = "module_user_mapper")
@Data
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ModuleUserMapper implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@JoinColumn(name = "module_id", referencedColumnName = "id")
@ManyToOne(optional = false)
private Modules moduleId;
@JoinColumn(name = "user_id", referencedColumnName = "id")
@ManyToOne(optional = false)
private Users userId;
}
When i access get all users - i need all users and their modules without infinite recursion. when i access get all modules - i need all modules & its users without infinite recursion.
currently i am trying to access get all users - getting all users and their modules with infinite recursion. I tried with @JsonIgnore, but i remove the child entity.
I am using SpringBoot version 2.1.6.RELEASE & spring boot JPA
infinite recursion means - get all users = User -> Module -> User -> Modules etc.
If any wrong in my entity mapping or table creation, please help me?