问题描述
我试图将类型为Book的对象插入到数据库中,其中一列指定为日期,但根据此例外情况:
原因:org.postgresql.util.PSQLException:错误:列publish_date的类型为date,但表达式的类型为bytea
提示:您需要重写或转换表达式。
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
正在放入bytea中。我不确定为什么会出现这种情况,因为在数据库中,列本身的类型是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; //国际标准书号,唯一
@Column(name =title)
私人字符串标题;
@Column(name =author)
private String author;
@Column(name =publish_date)
私人LocalDate publish_date; //发布到网站的日期
@Column(name =price)
私人双重价格;
@Column(name =content)
private byte [] content;
//未指定日期时使用的构造函数
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;
}
//指定日期时使用的构造函数
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;
//默认构造函数
public Book(){
}
public String getIsbn_13(){
返回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自动生成的方法存根
String isbn =1234123412341;
String title =Title;
String author =Haisam;
String book_tag =科幻小说;
BookDAO database = DAOUtilities.getBookDAO();
Book tempBook = database.getBookByISBN(isbn);
BooktagDAO tagdao = DAOUtilities.getBooktagDAO();
Booktag tempBooktag = tagdao.getBookTagByISBN(isbn);
if(tempBook!= null&& tempBooktag!= null){
// ASSERT:已存在isbn的书
System.out.println (ISBN已经存在。);
} else {
Book book = new Book();
Booktag booktag = new Booktag();
book.setIsbn_13(isbn);
book.setTitle(title);
book.setAuthor(作者);
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);如果(isSuccess_book&& isSuccess_booktag){
System.out.println(Added。);
if
} else {
System.out.println(Not added。);
}
}
}
}
如果有人知道如何从bytea转换为日期,或者这个问题的症结所在,我将永远感激不尽。感谢您的时间。
TLDR:由于LocalDate publish_date与数据库中实际的列publish_date之间的键入不匹配,因此事务未提交。不知道为什么。
创建一个 LocalDateToWhateverDBTypeConverter
。 。
编辑:
定义Converter的用法有两种选择。第一个是在转换器
的
@Converter
注释中设置 autoapply = true
/ code> class。在这种情况下,JPA提供者将使用这个 Converter
来转换给定类型的所有实体属性。
如果 autoapply
设置为 false
,则需要添加 javax。 persistence.Convert
注释转换为所有必须转换的属性,并指定 Converter
类。以下代码片段显示了此方法的示例:
@Entity
public class RectangleEntity
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private Integer x;
@Column
私人整数y;
@Column
@Convert(converter = ColorConverter.class)
private color color;
...
}
I'm trying to insert objects of type Book into a database, and one of the columns is specified as date, but according to this exception:
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
is being put in as bytea. I'm not sure why this is the case, because in the database the column itself is of type date, and the column in my Book class is of type date. I can show the code below:
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;
}
}
And this is the offline class I'm testing:
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.");
}
}
}
}
If anyone knows how to convert from bytea to date, or what the crux of this problem may be, I will be forever grateful. Thank you for your time.
TLDR: The transaction is not committing because of incompatible typing between LocalDate publish_date and the actual column publish_date in the database, which is of type date. Not sure why.
Create a LocalDateToWhateverDBTypeConverter
. Here is how.
Edit:
There are two options to define the usage of a Converter. The first one is to set autoapply=true
at the @Converter
annotation of the Converter
class. In this case the JPA provider will use this Converter
to convert all entity attributes of the given type.If autoapply
is set to false
, you need to add the javax.persistence.Convert
annotation to all attributes that shall be converted and specify the Converter
class. The following code snippet shows an example for this approach:
@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;
...
}
这篇关于错误:列“publish_date”是类型日期,但表达式是bytea类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!