我正在開發一個使用 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", "[email protected]", "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 欄位不是 NULL。
我知道可能的解決方案是洗掉此 ID 主鍵欄位并創建一個復合 PK(由我的兩個欄位組成:portal_user_id_fk和user_type_id_fk)。它可以作業,但我不想嘗試保留這個單獨的id PK 欄位。
是否可以指定我的@ManyToMany注釋來生成自動增量 id 欄位?或者可能的解決方案是什么?(我知道我也可以嘗試使用兩個@OneToMany和關聯表的另一個物體來實作多對多關系,但我不希望使用這種方法以避免過于復雜)
uj5u.com熱心網友回復:
您不能@ManyToMany直接向注釋添加自動增量屬性,而應向連接的資料庫表添加自動增量 ID,并添加@GeneratedValue具有Identity生成型別的注釋,如下所示:
@Entity
@Table(name = "portal_user_user_type")
public class PortalUserUserType{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
uj5u.com熱心網友回復:
只是讓你id在portal_user_user_type表自動遞增的:id bigint NOT NULL GENERATED ALWAYS AS IDENTITY...。然后 Hibernate 將生成一個有效的插入陳述句,而不提及 id。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/352784.html
上一篇:Java組態檔多維陣列
