If you go through THIS ANSWER, you will find that we can't have auto increment property in Composite Primary keys. So, the workaround would be to use concept of indexing
and unique constraint
. Make the role
,deptt
,grade
columns as you indices
and set the unique
constraint as true
. This will ensure that the pair of these three values are always unique(like primaryKey). Then add the techerId
as Primarykey
(autoGenerate = true) in the entity. Your entity
class would look something like:
@Entity(tableName = "teacher",indices = arrayOf(Index(value = ["role","department","grade"],unique = true)))
public class Teacher{
@PrimaryKey(autoGenerate = true)
int teacher_id;
@ColumnInfo(name = "name")
String teacher_name;
//Rest of the fields
......
......
}