简体   繁体   English

JOOQ插入类型

[英]JOOQ insert with type

I'm using jOOQ 3.8.4 and PostgreSQL 9.5 in a Spring 4 application. 我在Spring 4应用程序中使用jOOQ 3.8.4和PostgreSQL 9.5。 I have the following table and type definition 我有以下表格和类型定义

 CREATE DOMAIN shop.money_amount AS numeric(6,2) DEFAULT 0 NOT NULL CHECK (value > 0::numeric);

 CREATE TYPE shop.money AS (
   m_amount shop.money_amount,
   m_currency shop.currency,
   m_country shop.site_country
 );

 CREATE TYPE shop.money_mapping AS (
   mm_moneys shop.money []
 );

 CREATE TABLE shop.article
 (
   a_id bigserial NOT NULL,
   a_price shop.money_mapping NOT NULL,
   CONSTRAINT a_pk_id PRIMARY KEY (a_id)
 );

Then I tried an insert using jOOQ, ie: 然后我尝试使用jOOQ插入,即:

MoneyMappingRecord priceMoneyMapping = new MoneyMappingRecord();
priceMoneyMapping.setMoneys(new MoneyRecord[]{
        new MoneyRecord().setAmount(new BigDecimal("11")).setCountry(SiteCountry.US).setCurrency(Currency.USD),
        new MoneyRecord().setAmount(new BigDecimal("14")).setCountry(SiteCountry.DE).setCurrency(Currency.EUR)
});

dsl.insertInto(ARTICLE)
        .set(ARTICLE.A_PRICE, priceMoneyMapping)
        .returning(ARTICLE.A_ID).fetchOne().getId();

Then I get: 然后我得到:

Caused by: org.springframework.jdbc.BadSqlGrammarException: jOOQ; 
  bad SQL grammar [insert into "shop"."article" ("a_price") values
  (row(?::money[])) returning "shop"."article"."a_id"]; nested 
  exception is org.postgresql.util.PSQLException: 
  ERROR: cannot cast type record to shop.money_mapping
  Detail: Cannot cast type money[] to shop.money[] in column 1.

What am I doing wrong? 我究竟做错了什么?


UPDATE UPDATE

I tried to rename the shop.money type as suggested by Lukas (from shop.money to shop.localized_money ), but I believe that the problem is related to the schema. 我试图按照Lukas的建议重命名shop.money类型(从shop.moneyshop.localized_money ),但我相信问题与架构有关。 See the updated error. 查看更新的错误。

Caused by: org.springframework.jdbc.BadSqlGrammarException: 
   jOOQ; bad SQL grammar [insert into "shop"."article" ("a_price") 
   values (row(?::localized_money[])) returning "shop"."article"."a_id"]; nested exception is    
   org.postgresql.util.PSQLException: ERROR: type "localized_money[]" does not exist

Maybe the type in type is an issue! 也许类型的类型是一个问题!

There seems to be a problem related to casting of nested array of types in jOOQ 3.8. 在jOOQ 3.8中,似乎存在与嵌套数组类型相关的问题。 I've created an issue for this: https://github.com/jOOQ/jOOQ/issues/5571 我为此创建了一个问题: https//github.com/jOOQ/jOOQ/issues/5571

The problem is that your custom type array type needs to be fully qualified when bound as a bind variable. 问题是当绑定为绑定变量时,您的自定义类型数组类型需要完全限定。 If it isn't fully qualified, then the type isn't found by PostgreSQL. 如果它不是完全限定的,那么PostgreSQL找不到该类型。 One possible workaround is described in this question: Permanently Set Postgresql Schema Path 此问题中描述了一种可能的解决方法: 永久设置Postgresql架构路径

You can add the shop schema on your user's search path: 您可以在用户的​​搜索路径中添加shop架构:

ALTER ROLE <your_login_role> SET search_path TO shop;

... which means that elements inside of the shop schema no longer need to be fully qualified. ...这意味着shop架构内的元素不再需要完全限定。 Which can also be a bad thing, so be careful! 这也可能是件坏事,所以要小心! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM