TypeMismatchException:为类提供了错误类型的ID

问题描述

我的以下JPA类存在问题。当我尝试查询Customer对象时,Hibernate给了我一个TypeMismatchException

@Entity
@Table(name = "customer",schema = "dbforq")
public class Customer implements Serializable {

    @EmbeddedId
    private CustomerId customerId;

    @OnetoOne(optional = true,cascade = { CascadeType.PERSIST,CascadeType.MERGE },orphanRemoval = true,fetch=FetchType.LAZY)
    @JoinColumns({
        @JoinColumn(name = "CUSTOMER_ID",referencedColumnName = "ADDRESS_ID",insertable = false,updatable = false),@JoinColumn(name = "CUSTOMER_KEY",referencedColumnName = "ADDRESS_KEY",updatable = false)
    })
    private Address address;
}


@Embeddable
public class CustomerId implements Serializable {

    @Column(name = "CUSTOMER_ID",nullable = false)
    private String id;

    @Column(name = "CUSTOMER_KEY")
    private String key;
}



@Entity
@Table(name = "address",schema = "dbforq")
public class Address implements Serializable {

    @EmbeddedId
    private AddressId addressId;

    @OnetoOne(mappedBy = "address")
    private Customer customer;
}


@Embeddable
public class AddressId implements Serializable {

    @Column(name = "ADDRESS_ID",nullable = false)
    private String id;

    @Column(name = "ADDRESS_KEY")
    private String key;
}

例外情况如下。

org.hibernate.TypeMismatchException: Provided id of the wrong type for class my.package.dataaccess.objects.Address. Expected: class my.package.dataaccess.objects.AddressId,got class my.package.dataaccess.objects.CustomerId

在这里想念什么?我正在使用2.3.1版的Spring Boot。

解决方法

我认为James Gawron的意思是,您尝试过吗?

@JoinColumns({
    @JoinColumn(name = "ADDRESS_ID",referencedColumnName = "CUSTOMER_ID",insertable = false,updatable = false),@JoinColumn(name = "ADDRESS_KEY",referencedColumnName = "CUSTOMER_KEY",updatable = false)
})