首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我在Jooq中插入一个日期时,我得到这个错误:列creation_date的类型是timestamp with time zone,但表达式的类型是character varying

当我在Jooq中插入一个日期时,我得到这个错误:列creation_date的类型是timestamp with time zone,但表达式的类型是character varying
EN

Stack Overflow用户
提问于 2021-02-16 16:47:37
回答 1查看 205关注 0票数 0

我使用Spring boot和postgresql,这是我的"pom.xml“文件:

代码语言:javascript
复制
<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>

我想插入一个用户对象,如下所示:

代码语言:javascript
复制
User:
  userId --> Long
  name --> String
  password --> String
  creationDate --> ZonedDateTime
  modifitionDate --> ZonedDateTime

在数据库中:

代码语言:javascript
复制
Users:
  user_id --> bigint
  name --> character varying
  password --> character varying
  creation_date --> timestamp with time zone
  modifition_date --> timestamp with time zone

但是当我尝试插入用户时:

代码语言:javascript
复制
@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();

我得到了这个错误:

代码语言:javascript
复制
column 'creation_date' is of type timestamp with time zone but expression is of type character varying

我意识到jooq创建的Users类将"creation_date“字段设置为"TableField”类型,而不是"TableField“

代码语言:javascript
复制
public final TableField<UsersRecord, OffsetDateTime> CREATION_DATE= createField(DSL.name("creation_date"), SQLDataType.TIMESTAMPWITHTIMEZONE(6).nullable(false), this, "");

我尝试将creation_date字段从ZonedDateTime转换为OffsetDateTime,然后将其放入"values“方法中,但也不起作用,我得到了同样的错误

我应该怎么做才能正确地注册用户?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-16 16:55:41

我认为您需要将此参数添加到连接字符串中: stringtype=unspecified

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66221185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档