【发布时间】:2021-12-21 07:11:01
【问题描述】:
我正在开发一个使用 Spring Data JPA 和 Hibernate 来映射预先存在的数据库表的 Spring Boot 应用程序。我发现 @ManyToMany 关系存在一些困难,因为相关关系表具有 bigint 自动增量 NOT NULL PK。
基本上这是我数据库上的关系表:
CREATE TABLE IF NOT EXISTS public.portal_user_user_type
(
id bigint NOT NULL,
portal_user_id_fk bigint NOT NULL,
user_type_id_fk bigint NOT NULL,
CONSTRAINT portal_user_user_type_pkey PRIMARY KEY (id),
CONSTRAINT portal_user_user_type_to_portal_user FOREIGN KEY (portal_user_id_fk)
REFERENCES public.portal_user (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT portal_user_user_type_to_user_type FOREIGN KEY (user_type_id_fk)
REFERENCES public.user_type (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
上一张表是我的portal_user表和** user_type**表之间的多对多关联表。这是因为一个用户可以有多个用户类型,并且一个用户类型可以与不同的用户相关。
所以我已将它映射到我的 User 类(映射 portal_user 表),以这种方式:
@SpringBootTest()
@ContextConfiguration(classes = GetUserWsApplication.class)
@TestMethodOrder(OrderAnnotation.class)
public class UserRepositoryTest {
@Autowired
private UsersRepository userRepository;
@Test
@Order(1)
public void testInsertUser() {
User user = new User("Mario", null, "Rossi", 'M', new Date(), "XXX", "xxx@gmail.com", "329123456", new Date());
Set<Address> addressesList = new HashSet<>();
addressesList.add(new Address("Italy", "RM", "00100", "Via XXX 123", "near YYY", user));
user.setAddressesList(addressesList);
Set<UserType> userTypesList = new HashSet<>();
UserType userType1 = new UserType("ADMIN", "Admin user type !!!");
UserType userType2 = new UserType("USER", "Just a simple user...");
userTypesList.add(userType1);
userTypesList.add(userType2);
user.setUserTypes(userTypesList);
userRepository.save(user);
assertTrue(true);
}
}
问题是这个测试方法,当执行 save() 方法时,我得到这个输出,但有以下异常:
Hibernate:
insert
into
portal_user
(birthdate, contact_number, created_at, e_mail, first_name, middle_name, sex, surname, tex_code)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
address
(country, notes, province, street, fk_user_id, zip_code)
values
(?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
user_type
(description, type_name)
values
(?, ?)
Hibernate:
insert
into
user_type
(description, type_name)
values
(?, ?)
Hibernate:
insert
into
portal_user_user_type
(portal_user_id_fk, user_type_id_fk)
values
(?, ?)
2021-11-08 12:58:50.859 WARN 10724 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23502
2021-11-08 12:58:50.863 ERROR 10724 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: null value in column "id" of relation "portal_user_user_type" violates not-null constraint
Detail: Failing row contains (null, 13, 1).
2021-11-08 12:58:50.958 INFO 10724 --- [ main] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
这是因为它似乎无法将记录插入到我的 portal_user_user_type 多对多关系表中,因为该表的 ID 字段不为空。
我知道一个可能的解决方案是删除此 ID 主键字段并创建一个复合 PK(由我的两个字段组成:portal_user_id_fk 和 user_type_id_fk)。 它可以工作,但我不希望尝试使用这个单独的 id PK 字段。
是否可以指定我的 @ManyToMany 注释以生成自动增量 id 字段?或者有什么可能的解决方案? (我知道我也可以尝试使用两个 @OneToMany 和关联表的另一个实体来实现 Many To Many 关系,但我不希望使用这种方法避免过于复杂)
【问题讨论】:
-
public.portal_user_user_type表缺少自增主键。 -
能否请您发布
@Entity课程User和UserType?这可能会提供一些线索。
标签: java spring-boot hibernate spring-data-jpa hibernate-mapping