Many-to-Many relationship
There are ways to generate data for many-to-many relationships by Foxglove.
dataGenerator() - A method gives a DataGenerator instance.
Confined domain of ids
Confine the domain for both sides
Use Cartesian product to generate data for the junction table
Fixed domain
// The domain of ids of cars
var idsOfCars = LongStream.range(11, 21).boxed()
.toList();
// The domain of ids of members
var idsOfMembers = LongStream.range(101, 111).boxed()
.toList();
/*
* Sets up the facets for car and member tables by their domain of ids.
*/
var carFacet = JdbcTableFacet.builder(TABLE_CAR)
.keyOfInt("cr_id").domain(idsOfCars)
.build();
var memberFacet = JdbcTableFacet.builder(TABLE_MEMBER)
.keyOfInt("mb_id").domain(idsOfMembers)
.build();
// :~)
/*
* Uses Cartesian product to set up the many-to-many relationship
*/
var rentFacet = JdbcTableFacet.builder(TABLE_RENT)
.cartesianProduct("rt_cr_id").domain(idsOfCars)
.cartesianProduct("rt_mb_id").domain(idsOfMembers)
.build();
// :~)
dataGenerator()
.generate(carFacet, memberFacet, rentFacet);One side referencing
Any way to decide the number of rows to referenced side(table)
Use
.referencing(String)for this sideUse the number of values on referencing side to
.cardinality(int)
Fix the domain for another side(table)
Use
.column(String).roundRobin(…)for this side
One-side referencing
final int numberOfCars = 10;
// The domain of ids of members
var idsOfMembers = LongStream.range(101, 111).boxed()
.toList();
/*
* Can be any way to generate the ids of cars
*/
var carFacet = JdbcTableFacet.builder(TABLE_CAR)
.keyOfInt("cr_id").limit(1020, numberOfCars)
.build();
// :~)
/*
* Fixed domain of ids of members
*/
var memberFacet = JdbcTableFacet.builder(TABLE_MEMBER)
.keyOfInt("mb_id").domain(idsOfMembers)
.build();
// :~)
/*
* Uses Cartesian product to set up the many-to-many relationship
*/
var rentFacet = JdbcTableFacet.builder(TABLE_RENT)
// References to the ids of cars
.referencing("rt_cr_id").parent(carFacet, "cr_id")
// The cardinality is the number of members
.cardinality(idsOfMembers.size())
// The domain comes from the ids of members
.column("rt_mb_id").from(memberFacet, "mb_id")
// Uses round-robin to cover all members
.roundRobin()
.build();
// :~)
dataGenerator()
.generate(carFacet, memberFacet, rentFacet);Of course, you can use .column("rt_mb_id").roundRobin(idsOfMembers) directly.
Two side referencing
Both of the sides are referencing by
cartesianProduct(String)
One-side referencing
// Any way to generate data for these two tables
var carFacet = JdbcTableFacet.builder(TABLE_CAR)
.keyOfInt("cr_id").limit(1000, 3)
.build();
var memberFacet = JdbcTableFacet.builder(TABLE_MEMBER)
.keyOfInt("mb_id").limit(2000, 5)
.build();
// :~)
var rentFacet = JdbcTableFacet.builder(TABLE_RENT)
// References to ids of cars
.cartesianProduct("rt_cr_id")
.referencing(carFacet, "cr_id")
// References to ids of members
.cartesianProduct("rt_mb_id")
.referencing(memberFacet, "mb_id")
.build();