I have a MySQL DB with the following structure (excerpt):
CREATE TABLE MENU(
id_menu TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (id_menu)
);
CREATE TABLE OPERATION(
id_operation SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
id_menu TINYINT UNSIGNED NOT NULL,
operation VARCHAR(50) NOT NULL,
url VARCHAR(100) NOT NULL,
PRIMARY KEY (id_operation, id_menu)
);
CREATE TABLE operation_role(
id_operation SMALLINT UNSIGNED NOT NULL,
id_menu TINYINT UNSIGNED NOT NULL,
role CHAR(15) NOT NULL,
id_user BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (id_operation, id_menu, role, id_user)
);
CREATE TABLE role_user(
role CHAR(15) NOT NULL
id_user BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (role, id_user)
);
-- RELATIONSHIPS
--
-- TABLE: OPERATION
-- Meaning: a MENU has several OPERATION (One to Many relationship)
ALTER TABLE OPERACION ADD CONSTRAINT fk_menu_operacion
FOREIGN KEY (id_menu)
REFERENCES MENU(id_menu);
--
-- TABLE: operation_rol
-- This is the join table for the Many to Many relatioship OPERATION-role_user
ALTER TABLE operation_role ADD CONSTRAINT fk_operation_oprole
FOREIGN KEY (id_operation, id_menu)
REFERENCES OPERATION(id_operation, id_menu);
ALTER TABLE operaciones_rol ADD CONSTRAINT fk_roles_operation
FOREIGN KEY (role, id_user)
REFERENCES role_user(role, id_user);
--
-- TABLE: roles_usuario
-- Meaning: a user can have several roles (One to Many)
ALTER TABLE roles_usuario ADD CONSTRAINT fk_usuario_roles
FOREIGN KEY (id_usuario)
REFERENCES USUARIO(id_usuario);
Also, there's a USER table but it's not important, with these you can have the full picture of the problem.
As you can see, some columns has AUTO_INCREMENT
property which will become @GeneratedValue(strategy = GenerationType.IDENTITY)
in the @Entity
classes.
OPERATION
and role_user
has composite primary keys because of the relation they have with the other tables, so I can't change that. Because of the composite PK
, the mapped classes must have an @EmbeddedId
with the corresponding @Embeddable
class.
The problem is that I need a @GeneratedValue
in the "native" part of the composite PK
, e.g.: OPERATION.id_operation
must have @GeneratedValue
and OPERATION.id_menu
is propagated from MENU.id_menu, but
JPAdoes not support
@GeneratedValuein
@EmbeddedId`. Am I explaining correctly the situation?
I tried with the NetBeans's "suggestion" of the Entity classes from DataBase option but generates the @GeneratedValue
annotation for the tables with simple column's identifier (like MENU
) but not for the composite's one (like OPERATION
).
So the question is how can I do the mapping?. Changing the structure of the DB is not an option, it's made that way because of the business requirements.
Any help or guide is appreciated. Thank you so much in advance.
See Question&Answers more detail:
os