I am in need of assistance with incorporating a particular hibernate Inheritance mapping into my project

I am dealing with a situation where I have two classes, parent and child, with a self-referential relationship on the child side. The database is set up with separate tables for both parent and child, sharing the same "id", and using the column "holder" as a foreign key to itself. Below is the code that I have written. As I navigate this complex example of inheritance and interrelation, I am seeking guidance on whether my notations are correct.

Person.class

@Entity
@Getter @Setter @NoArgsConstructor
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "person")
public class Person {

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

  @NotNull
  @Column(name = "name")
  private String name;
}

Member.class

@Entity
@Getter @Setter @NoArgsConstructor
@Table(name = "member")
public class Member extends Person {

    @NotNull
    @Column(name = "member_type")
    private Integer memberType;

    @Column(name = "id_holder")
    private Integer idHolder;

    @ManyToOne(targetEntity = Member.class)
    @JoinColumn(name = "id_holder", referencedColumnName = "id", insertable = false, updatable = false)
    private Member holder;

    @OneToMany(mappedBy = "holder", cascade = CascadeType.ALL)
    private List<Member> dependents;
}

The object structure that will populate these entities on the frontend:

Member.model

export class Member {
    constructor(
        public id?: number,
        public name?: string,
        public memberType?: number,
        public idHolder?: any
    ) {}
}

Answer №1

Solving the puzzle myself:

After delving into Spring Boot and Hibernate, I stumbled upon a common issue with cyclic references. To address this, I modified my list of dependents to be write-only since it wasn't necessary for every read operation.

I also made the decision to eliminate the redundant idHolder property from the codebase.

Member.class

@Entity
@Getter @Setter @NoArgsConstructor
@Table(name = "member")
public class Member extends Person {

    @NotNull
    @Column(name = "member_type")
    private Integer memberType;

    @ManyToOne
    @JoinColumn(name = "id_holder", referencedColumnName = "id")
    private Member holder;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @OneToMany(mappedBy = "holder")
    private List<Member> dependents;
}

Furthermore, I have learned that managing such cyclic references can also be achieved through JsonManagedReference/JsonBackReference based on which entity controls the relationship. There are still more techniques that I am eager to explore in this domain.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to activate an input field in react-select?

Currently, I am working on a dropdown feature using react-select and have encountered some issues that need to be addressed: 1) The input field should be focused in just one click (currently it requires 2 clicks). 2) When the dropdown is opened and a cha ...

Utilizing separately generated elements from ngFor

I am currently working with an angular2 component that generates a list of chapters using an *ngFor= tag. However, I am facing an issue where I am unable to individually target these chapters in my ng2 component to highlight the selected chapter. I expecte ...

Can React Hooks API be used in TypeScript without using JSX?

After attempting to convert the JSX version of the React Hooks API demo into one without JSX, following the guidelines provided in react-without-jsx documentation, I ended up with the code below: import React, { useState } from 'react'; import R ...

Differences between Typescript static methods and functions defined outside of classesWhen comparing Types

What distinguishes a static class method from a function defined outside a class in TypeScript, especially when visibility is not a concern? While there are differences in visibility from other classes and files, what factors should be considered when dec ...

Locate the element for a button or an image

Having trouble recognizing the object in the image. Looking for guidance on how to identify the Button using ClassName or customized XPATH. <div class="schedule"> <a href="https://link" target="_blank"> <img border="0" alt="Sc ...

The Puzzling Case of Struts2 JSON Parameters

Experiencing difficulties with the deployment of a tomcat project. Trying to deploy a complex web application that was not created by me and have managed to resolve most errors, except for one particular issue. It appears to be causing the servlet to shu ...

Get the latest bitcoin price using JSON in JAVA

Looking to retrieve the current or historical bitcoin price using JSON... Encountering an error in the code: Exception in thread "main" java.lang.NullPointerException at RwithJlab.Basic.main(Basic.java:19) ---------------------------------code------ ...

HTML not updating after a change in properties

My template is structured as a table where I update a column based on a button click that changes the props. Even though the props are updated, I do not see the template re-rendered. However, since I am also caching values for other rows in translatedMessa ...

Can TestCafe be used to simulate pressing the key combination (Ctrl + +)?

I've been having a tough time trying to use the key combination specified in the title (Ctrl + +). So far, this is what I've attempted: 'ctrl+\+' 'ctrl+\\+' Does TestCafe even support this key combination? T ...

Experiencing difficulties with a click event function for displaying or hiding content

Struggling with implementing an onClick function for my two dynamically created components. Currently, when I click on any index in the first component, all content is displayed. What I want is to show only the corresponding index in the second component ...

What is the best way to pass only the second parameter to a function in TypeScript?

Let's consider a TypeScript function as shown below: openMultipleAddFormModal(commission?: Commission, people?: People): void { // some data } To make a parameter optional, I have added the Optional Chaining operator. Now, how can I modify the code ...

The operation to assign a value to property 'two' cannot be completed as it is currently undefined

I'm facing an issue with the code below and cannot figure out why I am encountering the error message. I have ensured that each object contains a value, so why is there a reference to 'undefined'? Cannot set property 'two' of unde ...

Angular - creating a specialized input field for a unique MatDialogConfig configuration file

I have a unique setup with a custom MaterialDialogConfig file dedicated to handling all of my material dialog components. Here's what the configuration file looks like: import { MatDialogConfig } from "@angular/material"; export class MaterialDialog ...

Why is it that the resource fails to load in an Angular localhost environment when making an http request?

In the realm of ASP.NET MVC projects lies my creation, a masterpiece utilizing Angular UI components powered by ASP.NET WebAPI. As an explorer navigating the unknown territory of Angular, I present to you the sacred texts residing within my project. Behol ...

A guide on determining the return type of an overloaded function in TypeScript

Scenario Here is a ts file where I am attempting to include the type annotation GetTokenResponse to the function getToken. import { ConfigService } from '@nestjs/config'; import { google, GoogleApis } from 'googleapis'; import { AppCon ...

Typescript's Accessor decorator ensures that the decorated code is executed only once, fetching the previously calculated value for any subsequent calls

The topic discussed here originates from a previous discussion on a method decorator in Typescript. In some scenarios, there are `get` methods in a Typescript class that involve intensive computations. Some of these methods always return the same result an ...

Customizing default attribute prop types of HTML input element in Typescript React

I am currently working on creating a customized "Input" component where I want to extend the default HTML input attributes (props). My goal is to override the default 'size' attribute by utilizing Typescript's Omit within my own interface d ...

Can you identify the reason for the hydration issue in my next.js project?

My ThreadCard.tsx component contains a LikeButton.tsx component, and the liked state of LikeButton.tsx should be unique for each logged-in user. I have successfully implemented the thread liking functionality in my app, but I encountered a hydration error, ...

Incorporate one JSON object as a nested child within another using Java programming

I'm currently tackling a complex issue and need some guidance specifically with the following JSON array: parentArray: [{"name":"folder1","children":[],"parent":"root","type":"folder"}, {"name":"folder2","children":[],"parent":"folder1","type" ...

What is the best way to display suggested words from a given list of options?

Looking for a way to provide suggestions to users based on a list of words they enter in TypeScript while maintaining performance. How can this be achieved efficiently? ...