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

Prisma Hack: excluding properties in type generation

EDIT hiding fields in the TypeScript definitions may pose a hidden danger: inaccessible fields during development with intellisense, but accidentally sending the full object with "hidden" fields in a response could potentially expose sensitive data. While ...

Generic Abstract Classes in TypeScript

In my TypeScript code, I have an abstract generic class with a method that takes a parameter of a class type variable. When I tried to implement the abstract method in a derived class, I noticed that the TypeScript compiler doesn't check the type of t ...

Is the naming convention for parameterized types (T, U, V, W) in Generics adhered to by Typescript?

Is TypeScript following the same naming convention for parameterized types as other languages like C++ and Java, using T,U,V,W, or is there a mixed usage of conventions? In the TS 2.8 release notes, we see examples like: type ReturnType<T> = T exten ...

Unable to interpret the DirectionsRoute object from JSON in MapBox

Utilizing the MapBox REST API on the backend, I have written a simplified code snippet to create a route: public class MapBoxRequest { private static final String PATTERN = "https://api.mapbox.com/directions/v5/mapbox/walking/%s,%s;%s,%s?alte ...

Navigating Through Angular Components

I have a layout with 3 components arranged like this: <app-header></app-header> <app-body></app-body> <app-footer></app-footer> However, I want to change the positioning of the footer and body as shown below: <app-he ...

Employing monaco-editor alongside typescript without webpack within an electron endeavor

I need help incorporating the monaco-editor into my electron project built with TypeScript. Using npm install -D monaco-editor, I installed it successfully and imported it with import { editor } from "monaco-editor";. Despite not receiving any mo ...

This error message in AngularJS indicates that the argument 'fn' is not being recognized as a function

I am currently working with angularjs and typescript and I am attempting to create a directive in the following manner: Below is my controller : export const test1 = { template: require('./app.html'), controller($scope, $http) { ...

What exactly does the $any() type cast in Angular do and how is it used?

While browsing the Angular.io site, I came across this title: The $any() type cast function There are instances where a binding expression may trigger a type error during AOT compilation and it is not possible or difficult to fully specify the type. To re ...

What is the solution for resolving an Element that implicitly has said it has an 'any' type as the expression of type 'string' cannot be used to index the type?

Having some trouble with TypeScript in my React project and encountering this error message. Error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ paymentMethod ...

Having trouble building the React Native app after adding react-native-vector icons?

A recent project I've been working on involved adding react-native-vector-icons using react-native 0.63.4. However, during the build process, I encountered an error when running the command npx react-native run-ios in the terminal. The warnings/errors ...

Using `await` inside an if block does not change the type of this expression

Within my code, I have an array containing different user names. My goal is to loop through each name, verify if the user exists in the database, and then create the user if necessary. However, my linter keeps flagging a message stating 'await' h ...

Dealing with showModalDialog window poses a challenge in Selenium Webdriver using Java

I am currently in the process of automating a web application using Selenium webdriver (java). I have successfully handled popup windows in other parts of the application, but I am encountering difficulty with the showModalDialog window. In this particular ...

Analyze the information presented in an HTML table and determine the correct response in a Q&A quiz application

I need to compare each row with a specific row and highlight the border accordingly: <table *ngFor="let Question from Questions| paginate: { itemsPerPage: 1, currentPage: p }"> <tr><td>emp.question</td></tr> <tr> ...

Showing an error message upon submission in Angular 4 according to the server's response

Struggling for hours to display an error message when a form submits and returns an error status code. The solution seems elusive... In the login form component below, I've indicated where I would like to indicate whether the form is valid or invalid ...

Generate a list of items in typescript, and then import them into a react component dynamically

I have a variable that stores key/value pairs of names and components in a TypeScript file. // icons.tsx import { BirdIcon, CatIcon } from 'components/Icons'; interface IconMap { [key: string]: string | undefined; } export const Icons: IconM ...

Using Selenium and Java to manipulate dynamic web elements

Looking to use Selenium for interacting with an element on a website that dynamically changes content based on user behavior. The element in question always contains exactly one HTML element. In its default state, the element appears as follows: https:// ...

TypeScript is throwing an error because a value has been declared but never actually used in the

private tree_widget: ITreeWidget; private $ghost: JQuery | null; private drag_element: DragElement | null; private previous_ghost: IDropHint | null; private open_folder_timer: number | null; constructor(tree_widget: ITreeWidget) { this.tree_widget = t ...

Typescript validation of tokens using Azure functions

Currently working on a website utilizing Azure Static Web App, where the login/registration is managed by Azure B2C. The backend API consists of typescript Azure functions integrated with Azure Static web app. Certain API calls can only be accessed when th ...

Accessing different pages in Angular 2 based on user type

If I have four pages and two user types, how can we implement access control in Angular 2 so that one user can access all four pages while the other is restricted to only two pages? ...

Error: zsh is unable to locate the command, even after defining it in package.json bin and installing it globally

I attempted to create a command-line application using TypeScript. Below is the code I have written: //package.json { "name": "jihea-cli", "version": "1.0.0", "description": "", "main": "index.ts", "bin": { "cli": "./bin/index.ts" }, // ...