質問

I have two classes using annotations to define links between fields and database column names. These classes are very similar except for the column name they define:

class TableA {
    @ForeignName("IDTableA") // Column name in TableC referring to this TableA record
    List<TableC> elements;
    // Some other field definitions, common between TableA and TableB

    // Some other field definitions, specific to TableA
}

class TableB {
    @ForeignName("IDTableB") // Column name in TableC referring to this TableB record
    List<TableC> elements;
    // Some other field definitions, common between TableA and TableB

    // Some other field definitions, specific to TableB
}

class TableC {
    @LocalName("IDTableA")
    TableA refA;
    @LocalName("IDTableB")
    TableB refB;
}

I would like to have a super-class to which I could give the "IDTableA/B" constant. Ideally "something like" (I know you can't give anything else than types to Generics, but just to make my point):

abstract class TableAB<IDName> {
    @ForeignName(IDName)
    List<TableC> elements;
    // Field definitions common between TableA and TableB
}

class TableA extends TableAB<"IDTableA"> {
    // Field definitions, specific to TableA
}

class TableB extends TableAB<"IDTableB"> {
    // Field definitions, specific to TableB
}

Is that somehow possible?

役に立ちましたか?

解決

No, this isn't possible. The IDName part needs to be a constant, and you can't provide a String literal as a generics type in any case.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top