JPA:如何映射SQL Server uniqueidentifier类型 [英] JPA: how to map SQL Server uniqueidentifier type

查看:320
本文介绍了JPA:如何映射SQL Server uniqueidentifier类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个我试图通过JPA映射的SQL Server数据库。许多表都有 uniqueidentifier 列。我试图像这样映射它们:

I've inherited a SQL Server database that I'm trying to map via JPA. Many of the tables have a uniqueidentifier column. I'm trying to map them like so:

@Id
@GenericGenerator(name = "generator", strategy = "guid", parameters = {})
@GeneratedValue(generator = "generator")
@Column(name = "APPLICATION_ID")
private String id;

Hibernate抱怨:

Hibernate complains with:

Found: uniqueidentifier, expected: varchar(255)


推荐答案

POJO中主键属性的数据类型确定其映射的DB列的数据类型,该列由 Dialect class。根据 SQLServerDialect 提供的hibernate,它没有任何映射到 uniqueidentifier 的数据类型,默认情况下 String 映射到 varchar(255)

The data type of the primary key property in the POJO determines the data type of its mapped DB column, which is specified by the Dialect class. According to the SQLServerDialect provided by hibernate, it does not have any data type that maps to uniqueidentifier, and String by default maps to varchar(255)

我认为 guid <$ c上的策略$ c> String 主键仅表示hibernate将为POJO的主键属性生成GUID值,并且此生成的GUID值将插入 varchar(255)列来模拟 uniqueidentifier的效果

I think guid strategy on a String primary key only means that hibernate will generate a GUID value for POJO's primary key property and this generated GUID value will be inserted to the varchar(255) column to simulate the effect of uniqueidentifier

您可以尝试覆盖<指定的映射a href =http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/dialect/package-summary.html\"rel =nofollow noreferrer> Dialect class by the use columnDefinition 属性 @Column

You can try to override the mapping specified by the Dialect class by using the columnDefinition attribute of @Column

 @Id
 @GenericGenerator(name = "generator", strategy = "guid", parameters = {})
 @GeneratedValue(generator = "generator")
 @Column(name = "APPLICATION_ID" , columnDefinition="uniqueidentifier")
 private String id;

这篇关于JPA:如何映射SQL Server uniqueidentifier类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆