telosystools

Open full view…

ManyToMany relationship doen't work

ing_magnus
Wed, 17 Aug 2022 14:15:25 GMT

Hi, I'm trying to generate a many to many relationship, but it seems that doesn't work. Automatic new model creation generate the below code (with all one to many relationships - and the join table is recognized as a normal table): @DbTable(roles) @DbCatalog(lazycat) Roles { // attributes id : int { @Id @AutoIncremented @DbName(id) @DbType(INT) @NotNull @NotEmpty }; name : string { @DbName(name) @DbType("VARCHAR(50)") @Size(50) @MaxLen(50) }; // links usersrolesList : UsersRoles[]; } @DbTable(users) @DbCatalog(lazycat) Users { // attributes id : int { @Id @AutoIncremented @DbName(id) @DbType(INT) @NotNull @NotEmpty }; user : string { @DbName(user) @DbType("VARCHAR(50)") @Size(50) @MaxLen(50) }; password : string { @DbName(password) @DbType("VARCHAR(50)") @Size(50) @MaxLen(50) }; // links usersrolesList : UsersRoles[]; } @DbTable(users_roles) @DbCatalog(lazycat) UsersRoles { // attributes idUser : int { @DbName(id_user) @DbType(INT) @FK(FK__users, Users.id) }; idRole : int { @DbName(id_role) @DbType(INT) @FK(FK__roles, Roles.id) }; // links roles : Roles { @LinkByFK(FK__roles) }; users : Users { @LinkByFK(FK__users) }; } Below I report the SQL code to create the tables: CREATE TABLE `roles` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci', PRIMARY KEY (`id`) USING BTREE ) COLLATE='utf8mb4_general_ci' ENGINE=InnoDB AUTO_INCREMENT=2 ; CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `user` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci', `password` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci', PRIMARY KEY (`id`) USING BTREE ) COLLATE='utf8mb4_general_ci' ENGINE=InnoDB AUTO_INCREMENT=2 ; CREATE TABLE `users_roles` ( `id_user` INT(11) NULL DEFAULT NULL, `id_role` INT(11) NULL DEFAULT NULL, INDEX `FK__users` (`id_user`) USING BTREE, INDEX `FK__roles` (`id_role`) USING BTREE, CONSTRAINT `FK__roles` FOREIGN KEY (`id_role`) REFERENCES `lazycat`.`roles` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT `FK__users` FOREIGN KEY (`id_user`) REFERENCES `lazycat`.`users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION ) COLLATE='utf8mb4_general_ci' ENGINE=InnoDB ; Please could I know where I'm wrong?

Telosys Team
Wed, 17 Aug 2022 17:27:08 GMT

You're not wrong, it's the behavior of the tool. The links follow all the FK-PK relationships found in the database. A model created from a database is only a "raw model", so you will need to refine it to align with your own expectations. The tool cannot presume whether a table should be considered as a "join table", even if it only has 2 FK. Perhaps it would be interesting to add an option to consider all these types of tables as de facto "join table" ? Anyway, the join table (or "join entity") must exist in the model to be referenced with the @LinkByJoinEntity(xx) annotation in a @ManyToMany link.

ing_magnus
Wed, 17 Aug 2022 17:37:58 GMT

Yes, it would be interesting to create an annotation to consider this kind of tables as "join table".

ing_magnus
Wed, 17 Aug 2022 17:40:59 GMT

Is there a way to exclude this kind of tables in the $entity object in the velocity template? Because for these tables it should not to be created any entity class.