Hibernate MappingException - несоответствие столбцов иностранных ключей с указанным первичным ключомJAVA

Программисты JAVA общаются здесь
Anonymous
Hibernate MappingException - несоответствие столбцов иностранных ключей с указанным первичным ключом

Сообщение Anonymous »

Я сталкиваюсь с ошибкой при попытке сохранить данные с помощью JPA и Hibernate. Сообщение об ошибке: < /p>
Caused by: org.hibernate.MappingException: Foreign key (FKi3uxs48nod3arim7hhpwx1anb:role_group [app_group_id])) must have same number of columns as the referenced primary key (app_group [id, app_id, group_id])
< /code>
У меня есть следующие определения таблицы и сопоставления объектов: < /p>
app_group Создание таблицы: < /p>
create table app_group
(
id varchar(36) not null,
group_id varchar(36) not null,
app_id varchar(36) not null,
primary key (id),
unique (group_id, app_id),
constraint app_group_group_fk foreign key (group_id) references `group` (id),
constraint app_group_app_fk foreign key (app_id) references `app` (id)
);
< /code>
AppGroup JPA Entity: < /p>
@Entity
@Table(name = "app_group")
public class AppGroup implements Serializable {
@Id
@Size(min = 36, max = 36)
protected String id;

@ManyToOne
@JoinColumn(name = "app_id")
private App app;

@ManyToOne
@JoinColumn(name = "group_id")
private Group group;

@OneToMany(mappedBy = "appGroup", cascade = CascadeType.ALL, orphanRemoval = true)
private Set groupRoles = new HashSet();
}
< /code>
role_group Table Creation: < /p>
create table role_group
(
id varchar(36) not null,
group_id varchar(36) not null,
role_id varchar(36) not null,
app_group_id varchar(36) not null,
primary key (id),
unique (group_id, role_id),
constraint role_group_group_fk foreign key (group_id) references `group` (id),
constraint role_group_role_fk foreign key (role_id) references `role` (id),
constraint role_group_app_group_fk foreign key (app_group_id) references `app_group` (id)
);
< /code>
rolegroup jpa Entity: < /p>
@Entity
@Table(name = "role_group")
@Accessors(fluent = true)
public class RoleGroup {
@Id
@Size(min = 36, max = 36)
protected String id;

@ManyToOne
@JoinColumn(name = "role_id")
private Role role;

@ManyToOne
@JoinColumn(name = "group_id")
private Group group;

@ManyToOne
@JoinColumn(name = "app_group_id")
private AppGroup appGroup;
}

Проблема:
Таблица app_group имеет идентификатор первичного ключа и уникальное ограничение на (group_id, app_id) , который должен работать хорошо. Это не так в схеме. И как я могу решить эту проблему?@Entity
@Table(name = "`group`")
public class Group {
@Id
@Size(min = 36, max = 36)
protected String id;

@ManyToMany(mappedBy = "groups")
private final Set apps;
}
< /code>
App JPA Entity: < /p>
@Entity
@Table(name = "app")
public class App {
@Id
@Size(min = 36, max = 36)
protected String id;

@ManyToMany
@JoinTable(
name = "app_group",
joinColumns = {@JoinColumn(name = "app_id")}, inverseJoinColumns = {@JoinColumn(name = "group_id")}
)
private Set groups;
}


Подробнее здесь: https://stackoverflow.com/questions/795 ... ferenced-p

Вернуться в «JAVA»