简体   繁体   English

Hibernate不保存到联接表中-ManyToOne和JoinTable关系

[英]Hibernate not saving into join table - ManyToOne and JoinTable relation

I'm using SpringBoot 2.0.2 with hibernate 5.2.17 and MariaDB 10.1 我正在将SpringBoot 2.0.2与hibernate 5.2.17和MariaDB 10.1一起使用

Customer: 顾客:

@Entity
public class Customer extends Company {

    @ManyToOne(fetch = FetchType.LAZY, cascade = ALL)
    @JoinTable(name = "company_services",
            joinColumns = @JoinColumn(name = "companyId", insertable = true, updatable = true),
            inverseJoinColumns = @JoinColumn(name = "serviceId", insertable = true, updatable = true))
    private Service service;

Service: 服务:

@Entity
public class Service {

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

With Hibernate log enabled I can see this: 启用休眠日志后,我可以看到以下内容:

Hibernate: 
    /* insert com.example.company.Customer
        */ insert 
        into
            `
            companies` (
                ...
            ) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    select
        last_insert_id()
Hibernate: 
    /* insert com.example.company.Customer
        */ insert 
        into
            `
            company_service` (
                `serviceId`, `companyId`
            ) 
        values
            (?, ?)
20:43:53.524 TRACE [cid: none] [session: none] org.hibernate.type.descriptor.sql.BasicBinder -- binding parameter [1] as [BIGINT] - [3]
20:43:53.524 TRACE [cid: none] [session: none] org.hibernate.type.descriptor.sql.BasicBinder -- binding parameter [2] as [BIGINT] - [3]

But when I confirm the result in the db the companies table is OK but the company_services record does not get saved. 但是,当我在db中确认结果时,companies表可以,但是company_services记录未保存。 Is that a known bug? 那是一个已知的错误吗? I'm missing something? 我想念什么吗?

Hibernate does not use join table for many to one relationship. Hibernate不将联接表用于多对一关系。 It uses a join column for many to one relationship. 它对多对一关系使用联接列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM