微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我在 Jooq 中插入日期时,出现此错误:列创建日期的类型为带时区的时间戳,但表达式的类型为字符变化

如何解决当我在 Jooq 中插入日期时,出现此错误:列创建日期的类型为带时区的时间戳,但表达式的类型为字符变化

我正在使用 Spring boot 和 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>

我想插入一个像这样的用户对象:

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

数据库中:

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

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

@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();

我收到此错误

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

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

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

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

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

解决方法

我认为你需要在连接字符串中加入这个参数:stringtype=unspecified

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。