[英]Could not determine type for: org.json.JSONObject, at table: ordersinfo, for columns: [org.hibernate.mapping.Column(items)]
My entity class in springboot 我在springboot中的实体类
Order.java: Order.java:
package com.demo.orderservice.model;
import java.math.BigInteger;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.json.JSONObject;
@Entity
@Table(name = "ordersinfo")
public class Order {
@Id
@Column(columnDefinition = "bigint")
@GeneratedValue(strategy=GenerationType.AUTO)
private BigInteger orderId;
@Column(nullable = false)
private String userId;
@Column(columnDefinition = "jsonb",nullable = false)
private JSONObject items;
@Column(nullable = false)
private String branch_id;
@Column(precision=8, scale=2,nullable = false)
private float price;
@Column(columnDefinition = "varchar(255) default 'CASH'",nullable = false)
private String payment;
@Column(columnDefinition = "timestamptz default current_timestamp",nullable = false)
private String time;
public BigInteger getOrderId() {
return orderId;
}
public void setOrderId(BigInteger orderId) {
this.orderId = orderId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public JSONObject getItems() {
return items;
}
public void setItems(JSONObject items) {
this.items = items;
}
public String getBranch_id() {
return branch_id;
}
public void setBranch_id(String branch_id) {
this.branch_id = branch_id;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getPayment() {
return payment;
}
public void setPayment(String payment) {
this.payment = payment;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@Override
public String toString()
{
JSONObject obj = new JSONObject();
obj.put("orderid", orderId);
obj.put("userid", userId);
obj.put("items", items);
obj.put("branchid",branch_id);
obj.put("price", price);
obj.put("payment",payment);
obj.put("time",time);
return obj.toString();
}
}
When i am running my springboot application.I am getting the following error.How to resolve it? 当我运行springboot应用程序时,出现以下错误,如何解决?
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-08-06 14:30:34.254 ERROR 17024 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: org.json.JSONObject, at table: ordersinfo, for columns: [org.hibernate.mapping.Column(items)]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
I want an attribute item with jsonb data type in my postgres db. 我想在我的postgres数据库中使用jsonb数据类型的属性项 。
@Column(columnDefinition = "jsonb",nullable = false)
private String items;
keeping this in my entity class does not make any problem but while displaying entity class object it is being represented as String,but i want it to be represented as JSON object only . 将其保留在我的实体类中没有任何问题,但是在显示实体类对象时将其表示为String,但是我希望仅将其表示为JSON对象。
Exactly happened like this.. 就是这样
{
"orderId": 4,
"userId": "PN250023",
"items": "{\"items\": [{\"item_id\": \"ITM124\", \"quantity\": \"2\", \"unit_price\": \"120\"}, {\"item_id\": \"ITM126\", \"quantity\": \"1\", \"unit_price\": \"123\"}]}",
"branch_id": "BR123099",
"price": 363.0,
"payment": "CASH",
"time": "2019-08-06 11:14:54.51044+05:30"
}
How to solve this issue? 如何解决这个问题?
Another issue while using POST API is 使用POST API时的另一个问题是
org.postgresql.util.PSQLException: ERROR: column "items" is of type jsonb but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 96
How to solve this issue? 如何解决这个问题?
Hibernate does not have OOB support for jsonb
type in postgres. Hibernate在postgres中不支持
jsonb
类型的OOB。 You will have to implement a Hibernate UserType
. 您将必须实现Hibernate
UserType
。 Sadly, org.json.JSONObject
isn't 不幸的是,
org.json.JSONObject
不是
Check out these links: 查看以下链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.