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.
@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:
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.