微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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)
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。