简体   繁体   English

当我在 Jooq 中插入日期时,出现此错误:column creation_date is of type timestamp with time zone but expression is of type character varying

[英]When I insert a date in Jooq, I get this error: column creation_date is of type timestamp with time zone but expression is of type character varying

I'm using Spring boot and postgresql, this is my "pom.xml" file:我正在使用 Spring 引导和 postgresql,这是我的“pom.xml”文件:

<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jooq</artifactId>
   </dependency>
   <dependency>
    <groupId>org.postgresql</groupId>
       <artifactId>postgresql</artifactId>
       <scope>runtime</scope>
   </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
            
        <plugin>
           <groupId>org.jooq</groupId>
           <artifactId>jooq-codegen-maven</artifactId>
           <executions>
              <execution>
                 <id>generate-postgre</id>
                 <phase>generate-sources</phase>
                 <goals>
                    <goal>generate</goal>
                 </goals>
                 <configuration>
                     <jdbc>
                        <driver>org.postgresql.Driver</driver>
                        <url>jdbc:postgresql://localhost:5432/DEMO</url>
                        <user>postgres</user>
                        <password>password</password>
                        </jdbc>
                      <generator>
                         <database>                           
                            <name>org.jooq.meta.postgres.PostgresDatabase</name>
                            <includes>.*</includes>
                            <excludes></excludes>
                            <schemata>
                                <schema> <inputSchema>comun</inputSchema> </schema>
                            </schemata>
                        </database>
                        <generate>
                              <pojos>true</pojos>
                              <pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
                              <javaTimeTypes>true</javaTimeTypes>
                              <fluentSetters>true</fluentSetters>
                         </generate>
                         <target>
                              <packageName>com.firedragon.erp.comun.entities</packageName>
                              <directory>target/generated-sources/jooq</directory>
                         </target>
                      </generator>
                   </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I want to insert a User object like this:我想像这样插入一个用户 object:

User:
  userId --> Long
  name --> String
  password --> String
  creationDate --> ZonedDateTime
  modifitionDate --> ZonedDateTime

In database:在数据库中:

Users:
  user_id --> bigint
  name --> character varying
  password --> character varying
  creation_date --> timestamp with time zone
  modifition_date --> timestamp with time zone

but when I try to insert the user:但是当我尝试插入用户时:

@Autowired
DSLContext ctx;
...
Users users=Users.USERS;
ctx.insertInto(users, Arrays.asList(users.NAME, users.PASSWORD, users.CREATION_DATE, users.MODIFICATION_DATE))
   .values(Arrays.asList("user1","password",ZonedDateTime.now(),ZonedDateTime.now()))
   .execute();

I get this error:我收到此错误:

column 'creation_date' is of type timestamp with time zone but expression is of type character varying

I realize that the Users class that jooq created has the "creation_date" field as "TableField<UsersRecord, OffsetDateTime>" type instead of "TableField<UsersRecord, ZonedDateTime>"我意识到 jooq 创建的用户 class 的“creation_date”字段为“TableField<UsersRecord, OffsetDateTime>”类型而不是“TableField<UsersRecord, ZonedDateTime>”

public final TableField<UsersRecord, OffsetDateTime> CREATION_DATE= createField(DSL.name("creation_date"), SQLDataType.TIMESTAMPWITHTIMEZONE(6).nullable(false), this, "");

I tried to convert the creation_date field from ZonedDateTime to OffsetDateTime before to putting it in the "values" method but it doesn't work either, I get the same error我尝试将 creation_date 字段从 ZonedDateTime 转换为 OffsetDateTime,然后将其放入“值”方法中,但它也不起作用,我得到了同样的错误

What should I do to register the user correctly?我应该怎么做才能正确注册用户?

I think you need to add this parameter to the connection string: stringtype=unspecified我认为您需要将此参数添加到连接字符串:stringtype=unspecified

Eg: jdbc:postgresql://127.0.0.1:5432/testdb?stringtype=unspecified例如:jdbc:postgresql://127.0.0.1:5432/testdb?stringtype=unspecified

I'm using Spring boot and postgresql, this is my "pom.xml" file:我正在使用 Spring 引导和 postgresql,这是我的“pom.xml”文件:

<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jooq</artifactId>
   </dependency>
   <dependency>
    <groupId>org.postgresql</groupId>
       <artifactId>postgresql</artifactId>
       <scope>runtime</scope>
   </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
            
        <plugin>
           <groupId>org.jooq</groupId>
           <artifactId>jooq-codegen-maven</artifactId>
           <executions>
              <execution>
                 <id>generate-postgre</id>
                 <phase>generate-sources</phase>
                 <goals>
                    <goal>generate</goal>
                 </goals>
                 <configuration>
                     <jdbc>
                        <driver>org.postgresql.Driver</driver>
                        <url>jdbc:postgresql://localhost:5432/DEMO</url>
                        <user>postgres</user>
                        <password>password</password>
                        </jdbc>
                      <generator>
                         <database>                           
                            <name>org.jooq.meta.postgres.PostgresDatabase</name>
                            <includes>.*</includes>
                            <excludes></excludes>
                            <schemata>
                                <schema> <inputSchema>comun</inputSchema> </schema>
                            </schemata>
                        </database>
                        <generate>
                              <pojos>true</pojos>
                              <pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
                              <javaTimeTypes>true</javaTimeTypes>
                              <fluentSetters>true</fluentSetters>
                         </generate>
                         <target>
                              <packageName>com.firedragon.erp.comun.entities</packageName>
                              <directory>target/generated-sources/jooq</directory>
                         </target>
                      </generator>
                   </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I want to insert a User object like this:我想像这样插入一个用户 object :

User:
  userId --> Long
  name --> String
  password --> String
  creationDate --> ZonedDateTime
  modifitionDate --> ZonedDateTime

In database:在数据库中:

Users:
  user_id --> bigint
  name --> character varying
  password --> character varying
  creation_date --> timestamp with time zone
  modifition_date --> timestamp with time zone

but when I try to insert the user:但是当我尝试插入用户时:

@Autowired
DSLContext ctx;
...
Users users=Users.USERS;
ctx.insertInto(users, Arrays.asList(users.NAME, users.PASSWORD, users.CREATION_DATE, users.MODIFICATION_DATE))
   .values(Arrays.asList("user1","password",ZonedDateTime.now(),ZonedDateTime.now()))
   .execute();

I get this error:我收到此错误:

column 'creation_date' is of type timestamp with time zone but expression is of type character varying

I realize that the Users class that jooq created has the "creation_date" field as "TableField<UsersRecord, OffsetDateTime>" type instead of "TableField<UsersRecord, ZonedDateTime>"我意识到 jooq 创建的用户 class 的“creation_date”字段为“TableField<UsersRecord, OffsetDateTime>”类型而不是“TableField<UsersRecord, ZonedDateTime>”

public final TableField<UsersRecord, OffsetDateTime> CREATION_DATE= createField(DSL.name("creation_date"), SQLDataType.TIMESTAMPWITHTIMEZONE(6).nullable(false), this, "");

I tried to convert the creation_date field from ZonedDateTime to OffsetDateTime before to putting it in the "values" method but it doesn't work either, I get the same error我尝试将 creation_date 字段从 ZonedDateTime 转换为 OffsetDateTime 之前将其放入“值”方法但它也不起作用,我得到同样的错误

What should I do to register the user correctly?我应该怎么做才能正确注册用户?

暂无
暂无

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

相关问题 错误:列“DOJ”的类型是没有时区的时间戳,但表达式的类型是字符变化 - ERROR: column "DOJ" is of type timestamp without time zone but expression is of type character varying 错误: <column-name> 是没有时区的时间,但表达的类型字符是变化的 - Error: the <column-name> is of time without time zone but expression is of type character varying JOOQ-列“ id”的类型为uuid,但表达式的类型为字符变化 - JOOQ - column “id” is of type uuid but expression is of type character varying postgres中的“带时区的时间戳”的Jooq绑定 - Jooq binding for “timestamp with time zone” type in postgres java sql error列的类型为整数,但表达式的类型为字符变化 - java sql error column is of type integer but expression is of type character varying Jooq-带时间的日期列 - Jooq - Date column with time 错误:“ publish_date”列的类型为date,但表达式的类型为bytea - ERROR: column “publish_date” is of type date but expression is of type bytea jOOQ:使用TIME ZONE解析Oracle TIMESTAMP时出错 - jOOQ: Error parsing Oracle TIMESTAMP WITH TIME ZONE 列是 uuid 类型,但表达式是 Spark Scala 中不同的字符类型 - Column is of type uuid but expression is of type character varying in Spark Scala org.postgresql.util.PSQLException:错误:列“标签”是 jsonb 类型,但表达式的类型是字符变化 - org.postgresql.util.PSQLException: ERROR: column "tags" is of type jsonb but expression is of type character varying
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM