简体   繁体   English

无法确定以下表的类型:org.json.JSONObject,在表:ordersinfo中,用于列:[org.hibernate.mapping.Column(items)]

[英]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: 查看以下链接:

  1. https://stackoverflow.com/a/37946530/1187254 https://stackoverflow.com/a/37946530/1187254
  2. https://thoughts-on-java.org/persist-postgresqls-jsonb-data-type-hibernate/ https://thoughts-on-java.org/persist-postgresqls-jsonb-data-type-hibernate/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.MappingException:无法确定以下类型:表:对于列:[org.hibernate.mapping.Column(plant) - org.hibernate.MappingException: Could not determine type for: at table: for columns: [org.hibernate.mapping.Column(plant) org.hibernate.MappingException:无法确定类型:在表中:列:[org.hibernate.mapping.Column(卖家)] - org.hibernate.MappingException: Could not determine type for: at table: for columns: [org.hibernate.mapping.Column(seller)] 无法确定以下类型的字符串:字符串,在表:STUDENT,对于列:[org.hibernate.mapping.Column(SNAME)] - Could not determine type for: String, at table: STUDENT, for columns: [org.hibernate.mapping.Column(SNAME)] 无法确定类型 <DataType> 在表:TableX中,用于org.hibernate.mapping.Column(userPrefs)列 - could not determine type for <DataType> at table: TableX, for columns org.hibernate.mapping.Column(userPrefs) org.hibernate.MappingException:无法确定类型:java.util.Set,在表:Company中,用于列:[org.hibernate.mapping.Column(users)] - org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Company, for columns: [org.hibernate.mapping.Column(users)] org.hibernate.MappingException:无法确定类型:java.util.List,在表:大学,列:[org.hibernate.mapping.Column(students)] - org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)] 引起:org.hibernate.MappingException:无法确定类型:时间戳,列:[org.hibernate.mapping.Column(***)] - Caused by: org.hibernate.MappingException: Could not determine type for: Timestamp, for columns: [org.hibernate.mapping.Column(***)] org.hibernate.MappingException:无法确定类型:java.util.List,在表:user处,用于列:[org.hibernate.mapping.Column(events)] - org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(events)] 无法确定类型:java.util.List,在表:file_post,列:[org.hibernate.mapping.Column(file)] - Could not determine type for: java.util.List, at table: file_post, for columns: [org.hibernate.mapping.Column(file)] 无法确定以下类型:com.packt.cardatabase.domain.Owner,在表:car上,用于列:[org.hibernate.mapping.Column(owner)] - Could not determine type for: com.packt.cardatabase.domain.Owner, at table: car, for columns: [org.hibernate.mapping.Column(owner)]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM