BaseLongIDEntity when primary key is not named "ID"

How do I use BaseLongIDEntity when my primary key is named “TRAN_ID” and not “ID”? I tried this:


public class GWTrans extends BaseLongIdEntity {
    private static final long serialVersionUID = -5808842913829717049L;

    @Column(name = "TRAN_ID", nullable = false, unique = true)
    @Id
    protected Long tranId;

but it doesn’t work. The system still looks for a field named “ID” when it goes to load the data.

What is the correct way to deal with a table without a field named “ID”?

In this case you should add the @AttributeOverrides annotation to your entity, for example:


@AttributeOverrides({
        @AttributeOverride(name = "id", column = @Column(name = "actor_id"))
})
@Table(name = "actor")
@Entity(name = "mdb$Actor")
public class Actor extends BaseIdentityIdEntity {
...

Studio does it automatically when generating model from an existing database.

Thank you. I will try it.

Just so you know, I first tried the model generator. It didn’t see TRAN_ID as a primary key, so it insisted on creating a new UUID field named “ID”. I don’t want my tables modified as other software is using them, so I created the entity manually. I have another question open on this.

Yes, we’ll check it.

I have just tried it. My code looks like this:


@AttributeOverrides({
        @AttributeOverride(name="id", column=@Column(name="tran_id"))
})
@NamePattern("%s %s %s %s %s|gatewayId,purchDate,gwprodsProdId,prmordnum,amount")
@Table(name = "gwtrans")
@Entity(name = "prm$GWTrans")
public class GWTrans extends BaseLongIdEntity {
    private static final long serialVersionUID = -5808842913829717049L;

    @Column(name = "TRAN_ID", nullable = false, unique = true)
    protected Long tranId;

(followed by the rest of the class, of course).

When I build and run, I get the following in app.log:

Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.2.cuba11): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.6.2.cuba11):   org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [gwtrans.tran_id].  Only one may be  defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[tranId-->gwtrans.tran_id]
Descriptor: RelationalDescriptor(com.paslists.prm.entity.GWTrans --> [DatabaseTable(gwtrans)])

After searching out the error, I found that I needed to change my Column definition to:


    @Column(name = "TRAN_ID", nullable = false, unique = true, insertable= false, updatable = false)
    protected Long tranId;

As far as I can tell, it seems that the “ID” definition that we are attaching to with the @AttributeOverride already is insertable and updatable, so we have to turn that off for our definition. Is this correct or have I just turned off the ability to insert a new row since we can’t insert into the primary key column TRAN_ID?

Do not define the tranId field in your entity at all. The @AttributeOverrides annotation will map your TRAN_ID column to the id attribute defined in the base entity. So when you call getId() or setId() you will actually work with data in your TRAN_ID column.

Thank you. I will make the change.