Java 使用JOOQ/PostgreSQL中的自定义类型更新字段
PostgreSQL中有一个带有ltree字段的表。如何使用字符串值更新此字段Java 使用JOOQ/PostgreSQL中的自定义类型更新字段,java,postgresql,jooq,ltree,Java,Postgresql,Jooq,Ltree,PostgreSQL中有一个带有ltree字段的表。如何使用字符串值更新此字段 DSL.using(configuration) .update(AREAS) .set(AREAS.TREE, area.getTree());//getTree() return String 我尝试了几种变体: val(area.getTree()).cast(field(“ltree”)) A org.jooq.exception.DataAccessException has been caught,
DSL.using(configuration)
.update(AREAS)
.set(AREAS.TREE, area.getTree());//getTree() return String
我尝试了几种变体:
val(area.getTree()).cast(field(“ltree”))
A org.jooq.exception.DataAccessException has been caught,
SQL [update "public"."areas" set "tree" = cast(? as any) where "public"."areas"."id" = ?];
ERROR: syntax error at or near "any"*
A org.jooq.exception.SQLDialectNotSupportedException has been caught,
Type class org.jooq.impl.SQLField is not supported in dialect DEFAULT
inline(String.format(“转换(\'%s\'asltree)”,area.getTree())
使用多余的引号
A org.jooq.exception.DataAccessException has been caught,
SQL [update "public"."areas" set "tree" = 'CAST(''1.35.1284739'' AS ltree)' where "public"."areas"."id" = ?];
ERROR: syntax error at position 4*
val(字段(“将({0}转换为ltree)”,area.getTree())
A org.jooq.exception.DataAccessException has been caught,
SQL [update "public"."areas" set "tree" = cast(? as any) where "public"."areas"."id" = ?];
ERROR: syntax error at or near "any"*
A org.jooq.exception.SQLDialectNotSupportedException has been caught,
Type class org.jooq.impl.SQLField is not supported in dialect DEFAULT
field("CAST(? as ltree)", area.getTree());
field("CAST({0} as ltree)", val(area.getTree()));
这就是您的各种尝试失败的原因:
这种方法一开始似乎很有意义,因为您正在使用jOOQ的内置cast()
支持。但是,您的字段(“ltree”)
表达式建模了一个名为ltree
的未知类型字段,因此当您将该字段传递给cast()
时,jOOQ不知道将val
转换为什么,因此:any
inline(String.format("CAST(\'%s\' AS ltree)", area.getTree()))
这在您的上下文中没有真正意义,因为它创建了一个内联绑定变量、常量或字符串文字(同一概念的所有不同名称)。您不需要字符串文字'CAST(…AS ltree)
,您需要表达式CAST(…AS ltree)
这与上面的情况类似,尽管你离这里很近。内部字段(…)
表达式创建一个SQL模板,这正是您想要的。但是,您使用将其包装在绑定变量中,这实际上没有意义,原因有二:
- 您不希望强制转换表达式是绑定变量
- 强制转换表达式(类型为
)不是用于创建绑定变量的有效类型。jOOQ不知道如何绑定类型为字段
Field
(对象)字段(“强制转换(?AS ltree)”,树代码>很抱歉给您添麻烦