Table with non incremental integer key

Hi everybody,

in my Oracle database, I have many tables with a simple non-incremental integer key (no UUID).

However, while there is BaseStringIdEntity there is no corresponding entities with integer key.

If I try to use BaseIntegerIdEntity or BaseLongIdEntity, the key of the table is always called id and seems to be impossible to change this setting.

As a workaround I have implemented, for each table, a key embedded with a single integer field.

First question: Whereas I can not change the database schema, I’m doing something that makes sense?

Second question: when the key of this table is related in many-to-one inside another table … how should I define the attribute? Embedded? If I do so I get something like:

‘’’
@Embedded
@AttributeOverrides ({
@AttributeOverride (Name = “tgn0701” column = @Column (name = “ANA1021”))
})
protected Tgn07PK ana1021;
‘’’

But I have no relationship in DDL preview, between ana10 table (customers) and tgn07 table (type of customer)

This causes me a lot of confusion, because it is impossible to manage a integer key exactly as a string key, with many-to-one relationship into another table?

I enclose entire project. An example of a table with integer key is tgn07 with the relative EmbeddableEntity tgn07PK. The table ana10 customer has the ana1021 field that should have many-to-one relationship with tgn07 (or tgn07pk?).

Any help is appreciated.

Thank you.

Maurizio.

erp.zip (20.4M)

Hi Maurizio,

If you have a PK field different from the default ID, you can simply override the default name in your entity class, for example:


@Table(name = "FOO")
@Entity(name = "intkey$Foo")
@AttributeOverrides({
        @AttributeOverride(name = "id", column = @Column(name = "FOO_ID"))
})
public class Foo extends BaseIntegerIdEntity {
    private static final long serialVersionUID = -3306363763647353095L;

    @Column(name = "NAME")
    protected String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Here the Foo entity is mapped to the FOO table which has FOO_ID primary key.

With this modification you can use links between entities as usual.

See the sample project attached.

int-key.zip (30.5K)

Simple solution.

Everything works.

Thank you Konstantin.

Maurizio.