Hibernate没有保存到连接表 - ManyToOne和JoinTable关系

时间:2018-05-24 19:29:03

标签: java spring hibernate jpa

我正在使用SpringBoot 2.0.2和hibernate 5.2.17以及MariaDB 10.1

客户:

@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;

服务:

@Entity
public class Service {

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

启用Hibernate日志后,我可以看到:

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]

但是当我在db中确认结果时,公司表是可以的,但是company_services记录没有得到保存。 这是一个已知的错误吗?我错过了什么?

1 个答案:

答案 0 :(得分:0)

Hibernate不会将连接表用于多对一关系。它使用连接列来实现多对一关系。