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 keys). For example I have table:
CREATE TABLE IF NOT EXISTS `Tourist_Guide`.`Cultural_activity` (
`City_Id` INT NOT NULL ,
`activity_Id` INT NOT NULL ,
`Cultural_activity_Id` INT NOT NULL AUTO_INCREMENT ,
`Name_Of_Cultural_activity` VARCHAR(30) NOT NULL ,
PRIMARY KEY (`Cultural_activity_Id`, `City_Id`, `activity_Id`) ,
INDEX `fk_Cultural_activity_activity1` (`City_Id` ASC, `activity_Id` ASC) ,
CONSTRAINT `fk_Cultural_activity_activity1`
FOREIGN KEY (`City_Id` , `activity_Id` )
REFERENCES `Tourist_Guide`.`activity` (`City_Id` , `activity_Id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Can you, please, tell me how to map this table to class(how this class should look like), is that even possible?
You have to use the following annotation above each unique field:
Here are the docs on
uniqueCombo
.https://github.com/ServiceStack/ServiceStack.OrmLite/#limitations
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:
Or creating a pseudo column that contains combination of all primary keys, e.g:
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:
http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/field/DatabaseField.html#useGetSet()