如何插入UUID的值? [英] How to insert value of UUID?

查看:330
本文介绍了如何插入UUID的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Play Framework 2.3支持的Postgresql 9.4中使用anorm 2.4

I'm using anorm 2.4 in play framework 2.3 backed postgresql 9.4

给出这样的模型:

case class EmailQueue(id:UUID,
                  send_from:String,
                  send_to:String,
                  subject:String,
                  body:String,
                  created_date:Date,
                  is_sent:Boolean,
                   email_template:String)

这是我的解析器:

val parser: RowParser[EmailQueue] = {
get[UUID]("id") ~
  get[String]("send_from") ~
  get[String]("send_to") ~
  get[String]("subject") ~
  get[String]("body") ~
  get[Date]("created_date") ~
  get[Boolean]("is_sent") ~
  get[String]("email_template") map {
  case id ~ send_from ~ send_to ~ subject ~ body ~
    created_date ~ is_sent ~ email_template=> EmailQueue(id,
    send_from,
    send_to,
    subject,
    body,
    created_date,
    is_sent,
    email_template)
}

}

这是我的插入语句:

def insert(email:EmailQueue): Unit ={
DB.withTransaction { implicit c =>
  SQL(s"""
        INSERT INTO "email_queue" ( "body", "created_date", "id", "is_sent", "send_from", "send_to", "subject", "email_template")
        VALUES ( {body}, {created_date}, {id}, {is_sent}, {send_from}, {send_to}, {subject}, {email_template} );
      """).on(
      "body" -> email.body,
      "created_date" -> email.created_date,
      "id" -> email.id,
      "is_sent" -> email.is_sent,
      "send_from" -> email.send_from,
      "send_to" -> email.send_to,
      "subject" -> email.subject,
      "email_template" -> email.email_template
    ).executeInsert()
}

}

我在插入时收到以下错误:

I receive following error when inserting:

[错误] PSQLException ::错误:列"id"的类型为uuid,但 表达式的字符类型不同[错误]提示:您将需要 重写或强制转换表达式. [错误]位置:153 (xxxxxxxxxx.java:2270)

[error] PSQLException: : ERROR: column "id" is of type uuid but expression is of type character varying [error] Hint: You will need to rewrite or cast the expression. [error] Position: 153 (xxxxxxxxxx.java:2270)

数据库表是通过以下查询创建的:

The database table is created by this query:

CREATE TABLE email_queue (
   id UUID PRIMARY KEY,
   send_from VARCHAR(255) NOT NULL,
   send_to VARCHAR(255) NOT NULL,
   subject VARCHAR(2000) NOT NULL,
   body text NOT NULL,
   created_date timestamp without time zone DEFAULT now(),
   is_sent BOOLEAN NOT NULL DEFAULT FALSE,
   email_template VARCHAR(2000) NOT NULL
);

推荐答案

与JDBC一样,规范与DB无关,因此默认情况下不支持特定于供应商的数据类型.

Anorm is DB agnostic, as JDBC, so vendor specific datatype are not supported by default.

您可以在语句中使用{id}::uuid,以便将在JDBC参数中作为字符串传递的java.util.UUID从传递的VARCHAR转换为特定的PostgreSQL uuid.

You can use {id}::uuid in the statement, so that the java.util.UUID passed as String in JDBC parameters is then converted from passed VARCHAR to a specific PostgreSQL uuid.

不建议在SQL(s"...")中使用字符串插值(SQL注入),但是可以使用Anorm插值.

Using string interpolation in SQL(s"...") is not recommanded (SQL injection), but Anorm interpolation can be used.

def insert(email:EmailQueue): Unit = DB.withTransaction { implicit c =>
  SQL"""
    INSERT INTO "email_queue" ( "body", "created_date", "id", "is_sent", "send_from", "send_to", "subject", "email_template")
    VALUES ( ${email.body}, ${email.created_date}, ${email.id}::uuid, ${email.is_sent}, ${email.send_from}, ${email.send_to}, ${email.subject}, ${email.email_template} )
  """).executeInsert()
}

不推荐使用,但是有时对于特定于供应商的类型可能有用,anorm.Object可用于传递不透明的值作为JDBC参数(::uuid对我来说更好).

Not recommended, but can be useful sometimes for vendor specific type, the anorm.Object can be used to pass an opaque value as JDBC parameter (there the ::uuid is nicer for me).

您还可以实现自定义ToStatement[java.util.UUID].

这篇关于如何插入UUID的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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