I have a class A which has a List of a class B. Since the attributes of a A is similar to what a B has, I thought about using inheritance to solve my problem. But I don't want B to have a list to B, and here is my problem. How can I prevent B from inheriting the List of B from the super-class?
The super-class:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@SuperBuilder
@Inheritance(strategy = InheritanceType.JOINED)
public class A
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
protected Integer id;
@OneToMany(mappedBy = "a")
private List<B> list = new ArrayList<>();
// other fields that I want to be inherited...
}
The subclass:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@SuperBuilder
public class B extends A
{
@JsonIgnore
@ManyToOne
@JoinColumn
private A a;
}
The JSON returned after creating the class A:
{
"id": 1,
// other fields
"list": [
{
"id": 2,
// inherited fields
"list": [], // I want to prevent this!
}
],
}
And one last thing, the reason why I want to use inheritance in this case instead of creating a B class with its own fields it's because I am using a composite key for solving another problem, and both classes share this composite key.