Java 错误:列";发布“U日期”;类型为date,但表达式类型为bytea

Java 错误:列";发布“U日期”;类型为date,但表达式类型为bytea,java,hibernate,type-conversion,localdate,bytea,Java,Hibernate,Type Conversion,Localdate,Bytea,我试图将Book类型的对象插入数据库,其中一列被指定为date,但根据此异常: Caused by: org.postgresql.util.PSQLException: ERROR: column "publish_date" is of type date but expression is of type bytea Hint: You will need to rewrite or cast the expression. Position: 94 at org.post

我试图将Book类型的对象插入数据库,其中一列被指定为date,但根据此异常:

Caused by: org.postgresql.util.PSQLException: ERROR: column "publish_date" is of type date but expression is of type bytea
  Hint: You will need to rewrite or cast the expression.
  Position: 94
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2412)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2125)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:297)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169)
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:136)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 11 more
被当作茶放进去了。我不确定为什么会出现这种情况,因为在数据库中,列本身的类型是date,而我的Book类中的列的类型是date。我可以在下面显示代码:

package examples.pubhub.model;

import java.time.LocalDate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="books")
public class Book {

    @Id
    @Column(name="isbn_13")
    public String isbn_13;  // International Standard Book Number, unique

    @Column(name="title")
    private String title;

    @Column(name="author")
    private String author;

    @Column(name="publish_date")
    private LocalDate publish_date; // Date of publish to the website

    @Column(name="price")
    private double price;

    @Column(name="content")
    private byte[] content;

    // Constructor used when no date is specified
    public Book(String isbn, String title, String author, byte[] content, double price) {
        super();
        this.isbn_13 = isbn;
        this.title = title;
        this.author = author;
        this.publish_date = LocalDate.now();
        this.content = content;
        this.price = price;
    }

    // Constructor used when a date is specified
    public Book(String isbn, String title, String author, LocalDate publishDate, double price, byte[] content) {
        super();
        this.isbn_13 = isbn;
        this.title = title;
        this.author = author;
        this.publish_date = publishDate;
        this.content = content;
        this.price = price;
    }

    // Default constructor
    public Book() {

    }

    public String getIsbn_13() {
        return isbn_13;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public LocalDate getPublish_date() {
        return publish_date;
    }


    public void setPublish_date(LocalDate publishDate) {
        this.publish_date = publishDate;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public void setIsbn_13(String isbn) {
        this.isbn_13 = isbn;

    }

}
这是我正在测试的离线类:

package examples.pubhub.utilities;

import java.time.LocalDate;


import examples.pubhub.dao.BookDAO;
import examples.pubhub.dao.BooktagDAO;
import examples.pubhub.model.Book;
import examples.pubhub.model.Booktag;

public class PublishBookTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String isbn = "1234123412341";
        String title = "Title";
        String author = "Haisam";
        String book_tag = "Science Fiction";


        BookDAO database = DAOUtilities.getBookDAO();
        Book tempBook = database.getBookByISBN(isbn);
        BooktagDAO tagdao = DAOUtilities.getBooktagDAO();
        Booktag tempBooktag = tagdao.getBookTagByISBN(isbn);

        if (tempBook != null && tempBooktag != null) {
            // ASSERT: book with isbn already exists

            System.out.println("ISBN already exists.");


        } else {

            Book book = new Book();
            Booktag booktag = new Booktag();
            book.setIsbn_13(isbn);
            book.setTitle(title);
            book.setAuthor(author);
            book.setPrice(124);
            book.setPublish_date(LocalDate.now());
            book.setContent(null);
            booktag.setBook_tag(book_tag);
            booktag.setBook_isbn(isbn);
            booktag.setBook_title(title);


            boolean isSuccess_booktag = tagdao.addTag(booktag);
            boolean isSuccess_book = database.addBook(book);



        if (isSuccess_book && isSuccess_booktag) {

            System.out.println("Added.");

        } else {

            System.out.println("Not added.");
        }


            }
        }

    }
如果有人知道如何从bytea转换到现在,或者这个问题的症结是什么,我将永远感激。谢谢你抽出时间


TLDR:事务未提交,因为LocalDate publish_date与数据库中实际列publish_date(类型为date)之间的类型不兼容。不确定原因。

创建一个
LocalDateToWhateverDBTypeConverter
。就是这样

编辑

有两个选项可以定义转换器的使用。第一个是在
Converter
类的
@Converter
注释处设置
autoapply=true
。在这种情况下,JPA提供者将使用此
转换器
转换给定类型的所有实体属性。 如果
autoapply
设置为
false
,则需要将
javax.persistence.Convert
注释添加到所有应转换的属性,并指定
转换器
类。以下代码段显示了此方法的示例:

@Entity
public class RectangleEntity
{

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;

   @Column
   private Integer x;

   @Column
   private Integer y;

   @Column
   @Convert(converter = ColorConverter.class)
   private Color color;

   ...
}

创建一个
LocalDateToWhateverDBTypeConverter
。就是这样

编辑

有两个选项可以定义转换器的使用。第一个是在
Converter
类的
@Converter
注释处设置
autoapply=true
。在这种情况下,JPA提供者将使用此
转换器
转换给定类型的所有实体属性。 如果
autoapply
设置为
false
,则需要将
javax.persistence.Convert
注释添加到所有应转换的属性,并指定
转换器
类。以下代码段显示了此方法的示例:

@Entity
public class RectangleEntity
{

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;

   @Column
   private Integer x;

   @Column
   private Integer y;

   @Column
   @Convert(converter = ColorConverter.class)
   private Color color;

   ...
}

我是否只需在方法book.setPublish_Date()中调用该类将LocalDate转换为Date?@haisamtarekekewidy我提供的链接的说明从第40行开始;您要我帮您读一下吗?我可以读一下说明书,先生。我在这个项目中没有使用spring,所以,从外观上看,我需要查找课程中提到的JAR。我会尽我所能让它工作。如果我理解正确,我会创建一个从LocalDate转换为date的类,然后在book的publish_date变量中,我像这样注释它?Column Convert(converter=LocalDatetoDate.class)假设LocalDatetoDate是类的名称,并且它们使用springframework JAR,因此我需要添加它们。好的,那么,我将尝试实现这一点。我是否可以简单地调用该类在方法书中从LocalDate转换为Date。setPublish_Date()?@HaisamTarekElkewidy我提供的链接的说明从第40行开始;您要我帮您读一下吗?我可以读一下说明书,先生。我在这个项目中没有使用spring,所以,从外观上看,我需要查找课程中提到的JAR。我会尽我所能让它工作。如果我理解正确,我会创建一个从LocalDate转换为date的类,然后在book的publish_date变量中,我像这样注释它?Column Convert(converter=LocalDatetoDate.class)假设LocalDatetoDate是类的名称,并且它们使用springframework JAR,因此我需要添加它们。好吧,那么,我会努力让这一切顺利进行。