Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

5.10 Composite primary key


May 14, 2021 JFinal manual



Starting with version 2.0, JFinal ActiveRecord supports composite primary keys with minimalist design, and for Model you need to specify the compound primary key name when mapping, here are some examples:

ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
The configuration of the multi-data source is simply to specify a compound primary key name once by the second parameter below
arp.addMapping("user_role", "userId, roleId", UserRole. class );

At the same time specify the compound primary key value to find the record UserRole. dao .findById(123, 456);

At the same time specify the compound primary key value to delete the record UserRole. dao .deleteById(123, 456);

As shown in the previous code, for Model, you only need to specify the compound primary key name when you add the Model map to start using the composite primary key, in subsequent operations JFinal will detect the number of compound primary key support, when the number of composite primary keys is incorrect will report exceptions, especially when the number of composite primary keys is not enough to ensure data security. The composite primary key is not qualified to have only two, which can be any number supported by the database.


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

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