mssql 的 UUID 疯狂 [英] UUID madness with mssql
问题描述
我的数据库条目有一个带有值的 UUID(使用 Microsoft SQL Server Management Studio 提取)
My database entry has a UUID with the value (extracted using the Microsoft SQL Server Management Studio)
CDF86F27-AFF4-2E47-BABB-2F46B079E98B
CDF86F27-AFF4-2E47-BABB-2F46B079E98B
将其加载到我的 Scala 应用程序中后,toString 方法会生成此值
After this is loaded into my Scala application, the toString method yields this value
276ff8cd-f4af-472e-babb-2f46b079e98b
276ff8cd-f4af-472e-babb-2f46b079e98b
这是怎么发生的?当我手头只有裸字符串 CDF86F27-AFF4-2E47-BABB-2F46B079E98B 时,如何以编程方式创建 UUID 实例?
How does this happen? And how can I programmatically create a UUID instance, when I have only the bare string CDF86F27-AFF4-2E47-BABB-2F46B079E98B at hand?
相关的Slick代码(前者:表定义,后者:数据库访问对象)
Relevant Slick code (former: table definition, latter: database access object)
class ChannelTable(tag: Tag) extends Table[ChannelTuple](tag, "Channel") {
def id = column[UUID]("Id", O.PrimaryKey)
def channelId = column[Int]("Channel_Id", O.NotNull)
def timer = column[UUID]("Timer_Id", O.NotNull)
def from = column[Timestamp]("FromTime", O.NotNull)
def to = column[Timestamp]("ToTime", O.NotNull)
def mon = column[Boolean]("Mon", O.NotNull)
def tues = column[Boolean]("Tues", O.NotNull)
def wed = column[Boolean]("Wed", O.NotNull)
def thu = column[Boolean]("Thu", O.NotNull)
def fri = column[Boolean]("Fri", O.NotNull)
def sat = column[Boolean]("Sat", O.NotNull)
def sun = column[Boolean]("Sun", O.NotNull)
def * = (id, channelId, timer, from, to, mon, tues, wed, thu, fri, sat, sun)
}
object ChannelDAO extends EntityDAO[Channel, ChannelTuple] {
private val entities = TableQuery[ChannelTable]
[...]
override def get(id: UUID)(implicit session: Session): Option[Channel] = {
val y = for {
a <- entities if a.id === id
} yield (a)
if (y.list.length > 1) throw new NonUniqueResultException
y.firstOption
}
[...]
}
推荐答案
Slick 将 UUID 转换为唯一标识符的方式与 SQL Server 不同.
在 .NET/SQL Server 中未指定字节序.
endianness is unspecified in .NET / SQL Server.
如果您从 MSSQL 获取 UUID,请确保对 UUID 使用大端编码以与 JVM 保持一致.
Make sure to use big endian encoding for UUIDs to be consistent with the JVM if you are getting the UUID from MSSQL.
查看此 SO 帖子.看起来您需要创建一个方法来翻译 UUID.
Check out this SO post. Looks like you will need to create a method to translate the UUID.
这篇关于mssql 的 UUID 疯狂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!