Composite primary key


JFinal ActiveRecord starts from version 2.0, using minimalist design to support composite primary keys. For Model, you need to specify the composite primary key name when mapping. The following is a specific example:

ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
// The configuration of multiple data sources is only to specify the composite primary key name once in the second parameter as follows
arp.addMapping("user_role", "userId, roleId", UserRole.class) ;

//Specify the composite primary key value at the same time to find the record UserRole.dao.findById(123, 456);

//Specify the composite primary key value at the same time to delete the record UserRole. dao.deleteById(123, 456);

As shown in the above code, for the Model, you only need to specify the composite primary key name when adding the Model mapping to start using the composite primary key. In subsequent operations JFinal will detect the number of composite primary keys supported. When the number of composite primary keys is incorrect, an exception will be reported. This can ensure data security, especially when the number of composite primary keys is insufficient. There is no limit to only two composite primary keys, it can be any number supported by the database.

For Db + Record mode, the use of composite primary keys does not require configuration, just use it directly:

Db.findById("user_role", "roleId, userId", 123, 456);
Db.deleteById("user_role", "roleId, userId", 123, 456);