Jpa mapped superclass without id

I am interested in the following problem. I have two sides, A

and B

. It stores almost the same information (for example, the name is more difficult in real life), but joins and foreign keys are different.

Can I make a mapped superclass without Id. And class A

and class B

by extending the mapped superclass containing only the Id attribute?

For example:

import lombok.Getter;
import lombok.Setter;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;

@MappedSuperclass
@Getter
@Setter
@Data
class superClass {

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

}

@Entity
@Table(name = "A")
@Data
class A extends superClass {

    @Id
    @OneToMany
    @JoinColumn(name = "id", referencedColumnName = "referencedName")
    private SomeClass id;

}

@Entity
@Table(name = "B")
@Data
class B extends superClass {

    @Id
    @OneToOne
    @JoinColumn(name = "id", referencedColumnName = "referencedName")
    private SomeOtherClass id;

}

      

Will this be valid JPA? I read mappedSuperClass

JavaDocs and didn't say anything about it. I would say this is indeed the case, but IntelliJ Idea says the superclass should have an Id attribute. I haven't found anything online about this.

Edit: Sorry I missed this. I left the annotation Entity

on the superclass and so the idea signed an error. I removed this and the error went away. But I'm not sure if this is really the case.

+3


source to share


2 answers


Yes this is true. In any case, your superclass will not appear in the table in the table.



+1


source


yes, there is no requirement that MappedSuperclass has something in it. It just provides additional annotations for subclasses.



+2


source







All Articles