Search code examples
hibernatejpahibernate-annotations

What do these JPA annotations mean?


Could you please explain what these annotaions mean in plain "human" language. I don't want documentation, I just want to hear something like tAttr table is joined with tInstit table by primary key InstitID and constraint is as follows and so on. SQL language would be ok :) I'm a bit confused. Thanks

I've this mapping class

    @Entity
    @DiscriminatorValue("Individual")
    @SecondaryTable(schema = "dbo", name="tAttr", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "InstitID")})
    public class FaIndividual extends FaClient 

and it extends this:

@Entity
public abstract class FaClient extends FaInstit

and above extends

@Entity
@Immutable
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(
    "CASE "
        + "WHEN ((InstType = 0) AND (PropDeal = 0)) THEN 'Individual' "
        + "WHEN ((InstType = 0) AND (PropDeal = 1)) THEN 'Legal' "
        + "WHEN (InstType = 1) THEN 'Bank' "
        + "WHEN (InstType = 2) THEN 'Subdivision' "
    + "END"
)
@Table(schema = "dbo", name = "tInstit")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public abstract class FaInstit

Solution

  • Your classes FaInstit (<--Extends) FaClient (<-- Extends) FaIndividual are all annotated @Entity which means that these are persistent classes with their fields mapping to DB tables/columns.

    In you super class FaInstit you have nominated that all that the entities in the inheritance hierarchy will all share the same primary table - dbo.tInstit – with the annotation @Inheritance(strategy = InheritanceType.SINGLE_TABLE)

    When using a single table for all entities, you need to identify each record in the table as belonging to one or other of the classes. This is done with a @DiscriminatorColumn (a default column name will be chosen if you skip this annotation). The value placed in the DiscriminatorColumn can be specified with the @DiscriminatorValue annotation. So whenever you persist the FaIndividual, the value in the DiscriminatorColumn will be Individual.

    It is the case sometimes we require an entity to map to more than one table. The primary table in your example is dbo.tInstit but we may define any number of secondary tables with the @SecondaryTable annotation. You have done this in FaIndividual and you should see in the definition of this class (not shown) that the fields of the class map to either of the tables. The row of the secondary table used is defined by the join information in the @SecondaryTable attribute;

    pkJoinColumns = {@PrimaryKeyJoinColumn(name = "InstitID")}
    

    So we have a join where dbo.tInstit.ID = dbo.tAttr.InstitID, and JPA will update/read etc the entity fields across the two tables according to this join information.

    You have stated

    I don't want documentation

    but I’m afraid that is what you do need as there is a lot to learn here. I suggest digging out a few tutorials/examples covering this sort of ground.

    I hope this gets you going.