Skip to content Skip to sidebar Skip to footer

How To Use Multiple Primary Keys

I created database, for my android app, witch has 16 tables. I want to use ORMlite mapping. The problem is that I didn't find examples where you have composite id(Multiple primary

Solution 1:

You have to use the following annotation above each unique field:

@DatabaseField (uniqueCombo = true)

Here are the docs on uniqueCombo.

Solution 2:

It is possible to generate an artificial ID field which consists of a composition of other fields. This can be done by settings the useSetGet Property in the @DatabaseField Annotation to true. This makes ORMLite call the getter and setter methods, instead of using reflection.

In your getter you could then return a composition of your fields.

This would look something like this in your example:

@DatabaseField(id=true, useGetSet=true)
privateString id;

...

publicStringgetId(){

    return culturalAcitivityId +"-" +cityId +"-" +activityId;

}
publicvoidsetId(String id){

    this.id = id;

}

http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/field/DatabaseField.html#useGetSet()

Solution 3:

Limitations

For simplicity, and to be able to have the same POCO class persisted in db4o, memcached, redis or on the filesystem (i.e. providers included in ServiceStack), each model must have a single primary key, by convention OrmLite expects it to be Id although you use [Alias("DbFieldName")] attribute it map it to a column with a different name or use the [PrimaryKey] attribute to tell OrmLite to use a different property for the primary key.

You can still SELECT from these tables, you will just be unable to make use of APIs that rely on it, e.g. Update or Delete where the filter is implied (i.e. not specified), all the APIs that end with ById, etc.

Workaround single Primary Key limitation

A potential workaround to support tables with multiple primary keys is to create an auto generated Id property that returns a unique value based on all the primary key fields, e.g:

publicclassOrderDetail
{
    publicstring Id { get { returnthis.OrderId + "/" + this.ProductId; } }

    

publicint OrderId { get; set; }
    publicint ProductId { get; set; }
    publicdecimal UnitPrice { get; set; }
    publicshort Quantity { get; set; }
    publicdouble Discount { get; set; }
}

https://github.com/ServiceStack/ServiceStack.OrmLite/#limitations

Solution 4:

ServiceStack's OrmLite by design doesn't support multiple composite primary keys. In order for your same POCOs to be useful outside of a db (e.g. NoSQL datastores, cache providers, etc) OrmLite expects a single primary key on each type, which my default is the Id property (overridable with ModelConfig class or [PrimaryKey], [Alias] attributes).

Work arounds to overcome this limitation include creating a single unique Id but include a unique constraint on the composite primary keys, e.g:

[CompositeIndex("CompositePkId1","CompositePkId2", Unique = true)] 
publicclassPoco 
{
    [AutoIncrement]
    publicint Id { get; set; }
    publicint CompositePkId1 { get; set; }
    publicint CompositePkId2 { get; set; }
}

Or creating a pseudo column that contains combination of all primary keys, e.g:

publicclassPoco 
{
    publicstring Id { get { return CompositePkId1 + ":" + CompositePkId2; } }
    publicint CompositePkId1 { get; set; }
    publicint CompositePkId2 { get; set; }
}

Post a Comment for "How To Use Multiple Primary Keys"