使用Hibernate在Java实体类上映射MySql无符号BigInt的正确数据类型是什么?

问题描述 投票:1回答:1

我正在使用Hibernate将MySql表映射到实体类。启动应用程序,我遇到此表\实体映射错误:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [id] in table [accomodation]; found [bigint (Types#BIGINT)], but expecting [decimal(19,2) (Types#NUMERIC)]  

所以我有下表名为accommodation:

Field                                                            Type       Null Key Default    Extra                      
---------------------------------------------------------------------------------------------------------------------------
id                                                               bigint(20) unsigned NO   PRI            auto_increment             
user_id                                                          bigint(20) unsigned NO   MUL                                       
accomodation_name                                                varchar(255) NO                                             
description                                                      text       NO                                             
nation                                                           varchar(255) NO                                             
region                                                           varchar(255) NO                                             
province                                                         varchar(255) NO                                             
city                                                             varchar(255) NO                                             
geographical_position                                            point      NO                                             
stars                                                            int(10)    NO                                             
accomodation_typological_id                                      bigint(20) unsigned YES                                            
accomodation_service_id                                          bigint(20) unsigned YES                                            
tell                                                             varchar(255) YES                                            
mobile                                                           varchar(255) YES                                            
fax                                                              varchar(255) YES                                            
email                                                            varchar(255) YES                                            
time_stamp                                                       datetime   YES                                            

上一个表由此Accomodation类映射:

@Entity
@Table(name = "accomodation")
public class Accomodation implements Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private BigInteger id;
    //private Integer id;

    /*@ManyToOne
    private Users users;
    */

    @OneToMany(mappedBy = "accomodation")
    private List<Room> rooms;

    private String name;
    private String description;
    private String phone;
    private String mobile;
    private String fax;
    private String email;
    private Integer stars;
    private Double longitude;
    private Double latitude;

    // CONSTRUCTOR AND GETTER AND SETTER METHODS
    ...........................................................
    ...........................................................
    ...........................................................

}

因此,读取错误似乎在我的住宿表的id字段上存在映射错误,映射在Accomodation类的相关id字段上。

问题似乎是使用BigInteger作为映射类的id字段的类型我必须在表的id字段上使用decimal(19,2)。但我认为它不好,因为表的主键应该是DB上的无符号BigInt。

那么我可以在我的类中使用什么Java数据类型来正确映射这个未签名的BigInt?

正如您在前面的2个代码段中看到的那样

hibernate jpa orm hibernate-mapping
1个回答
0
投票

您应该能够将Mysql类型bigint(20)映射到Java原始long,例如:

@Entity
@Table(name = "accomodation")
public class Accomodation implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

但是根据this documentation,您还应该能够使用java.math.BigInteger。

我的建议是检查驱动程序的版本。

© www.soinside.com 2019 - 2024. All rights reserved.