简体   繁体   中英

Could not determine type for: org.json.JSONObject, at table: ordersinfo, for columns: [org.hibernate.mapping.Column(items)]

My entity class in springboot

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?

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.

@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 .

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

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. You will have to implement a Hibernate UserType . Sadly, org.json.JSONObject isn't

Check out these links:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM