10

My database entry has a UUID with the value (extracted using the Microsoft SQL Server Management Studio)

CDF86F27-AFF4-2E47-BABB-2F46B079E98B

After this is loaded into my Scala application, the toString method yields this value

276ff8cd-f4af-472e-babb-2f46b079e98b

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?

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
  }
  [...]
}
Coxer
  • 1,694
  • 2
  • 26
  • 44
  • How does your code look like? Which type is used for the column in database and in Slick? – Alexey Romanov Mar 14 '14 at 11:20
  • Column type is java.util.UUID – Coxer Mar 14 '14 at 11:35
  • 1
    I know that .Net's binary encoding of Guids is different from the one UUID uses, so if Slick is unaware of that fact, it may explain your problem. Check the doc for Guid.ToByteArray on MSDN – Thomas Dufour Mar 14 '14 at 11:56
  • I think you are right, but how can I make Slick aware of that? Do you think I have to write my own UUID class for that reason? – Coxer Mar 15 '14 at 10:59

2 Answers2

2

Slick converts a UUID to a uniqueidentifier differently then SQL Server.

endianness is unspecified in .NET / SQL Server.

Make sure to use big endian encoding for UUIDs to be consistent with the JVM if you are getting the UUID from MSSQL.

Check out this SO post. Looks like you will need to create a method to translate the UUID.

How to read a .NET Guid into a Java UUID

Community
  • 1
  • 1
SoftwareCarpenter
  • 3,835
  • 3
  • 25
  • 37
0

Works fine for me:

> val uuidString = "CDF86F27-AFF4-2E47-BABB-2F46B079E98B"
uuidString: String = CDF86F27-AFF4-2E47-BABB-2F46B079E98B

> java.util.UUID.fromString(uuidString)
res2: java.util.UUID = cdf86f27-aff4-2e47-babb-2f46b079e98b
serejja
  • 22,901
  • 6
  • 64
  • 72
  • This is true. But when I load the entity from the database via slick, then the UUID prints the value "276ff8cd-f4af-472e-babb-2f46b079e98b" o.O - for the exactly same entity – Coxer Mar 14 '14 at 11:03
  • Code added for a better illustration of the problem – Coxer Mar 14 '14 at 12:56