簡體   English   中英

是否可以指定使用 Hibernate 將 id 自動增量非空主鍵設置為 @ManyToMany 關系映射?

[英]Is it possible specify to set an id auto increment not null primary key into a @ManyToMany relationship mapping using Hibernate?

我正在開發一個使用 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 字段不是 NULL。

我知道可能的解決方案是刪除此 ID 主鍵字段並創建一個復合 PK(由我的兩個字段組成: portal_user_id_fkuser_type_id_fk )。 它可以工作,但我不想嘗試保留這個單獨的id PK 字段。

是否可以指定我的@ManyToMany注釋來生成自動增量 id 字段? 或者可能的解決方案是什么? (我知道我也可以嘗試使用兩個@OneToMany和關聯表的另一個實體來實現多對多關系,但我不希望使用這種方法以避免過於復雜)

只需在portal_user_user_type表中自動增加您的idid bigint NOT NULL GENERATED ALWAYS AS IDENTITY... 然后 Hibernate 將生成一個有效的插入語句,而不提及 id。

您不能直接向@ManyToMany注釋添加自動增量屬性,而是應該向連接的數據庫表添加自動增量 ID,並添加具有Identity生成類型的@GeneratedValue注釋,如下所示:

@Entity
@Table(name = "portal_user_user_type")
public class PortalUserUserType{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM