Own primary key instead of system id

Heya,

Is it possible to use one of our own (unique) attributes as primary key, or map it over over the systems ID attribute? (like invoicenumbers) so we can access our entities via those keys via the API ?

As a sidenote, I can’t search for forums for terms with 3 or less characters (api, key, id), so could be that I missed the topic which described that :slight_smile:

Thanks!

Hi,

Sure it is possible via @AttributeOverride annotation on your entity class. If you generate model from your database in Studio, it will be applied automatically. If you map your entity manually, write something like this:

@Table(name = "CUSTOMER")
@Entity(name = "sales$Customer")
@AttributeOverride(name = "id", column = @Column(name = "CUSTOMER_ID"))
public class Customer extends BaseLongIdEntity {
// ...
}
1 Like

Hey Konstantin,
Thanks for the reply. When I add the AttributeOverride to the entity, and I create the attribute, I can create the database and compile with no errors, but when I launch the webclient, I don’t get the login screen, but the error: "Unexpected error, Please contact system administrator"
When I dig trough the logfile, I come across this error:
Exception Description: Multiple writable mappings exist for the field [TESTPROJECTJE_ORDER.INVOICENUMBER]. Only one may be defined as writable, all others must be specified read-only.
Here is my entity:

@NamePattern("%s|invoicenumber")
@Table(name = "TESTPROJECTJE_ORDER")
@Entity(name = "testprojectje$Order")
@AttributeOverride(name = "id", column = @Column(name = "INVOICENUMBER"))

public class Order extends BaseLongIdEntity {
    private static final long serialVersionUID = -7740540954588581229L;

    @Column(name = "INVOICENUMBER", nullable = false, unique = true)
    protected Long invoicenumber;
//...

I already changed some of the attribute restrictions, but with no success. I’m new to java, so perhaps I’m missing something :slight_smile:

You cannot use both id and invoicenumber attributes mapped to a single column.
If your invoicenumber is an artificial key, you can just drop this attribute and use id instead - it will be generated automatically.
If invoicenumber is a natural key and you want your users to enter its value manually, you should inherit your entity from BaseGenericIdEntity like this:

@Entity(name = "sample$MyEntity")
@Table(name = "...")
public class MyEntity extends BaseGenericIdEntity<Long> {

    @Id
    @Column(name = "INVOICENUMBER")
    private Long id;
    
    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }
}
1 Like

Thanks, this seems to work, but it seems to bring up a little bug. As soon as I use your code (the option for a natural key), and use the BaseGenericIdEntity entity, then the MyEntity model is uneditable in the data model interface. I can only edit it trough the IDE.
Is this an expected behavior ?

[edit] The remove icon is also disabled. I’ve added a screenshot

cuba_interface

Yes, Studio cannot work with such entities yet.