Impetus / kundera

A JPA 2.1 compliant Polyglot Object-Datastore Mapping Library for NoSQL Datastores.Please subscribe to:
http://groups.google.com/group/kundera-discuss/subscribe
Apache License 2.0
903 stars 233 forks source link

Unable to write Entity property as JSON in Mongodb #1004

Open muditskyfactor opened 6 years ago

muditskyfactor commented 6 years ago

I am creating a simple Entity model with one string field that contains JSON. I want to store this as a JSON object/node and not as a string in mongodb

My Entity model is as below:

public class SystemRawData {
private UUID id;
private Date acquiredTime;
private String metaData;
private String rawData;
private Map<String,String> hostInfo = new HashMap<>();

/**
 * @return the id
 */
public UUID getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setId(UUID id) {
    this.id = id;
}

/**
 * @return acquiredTime
 * return the acquired time
 */
public Date getAcquiredTime() {
    return acquiredTime;
}

/**
 * @param acquiredTime
 * set the acquired time
 */
public void setAcquiredTime(Date acquiredTime) {
    this.acquiredTime = acquiredTime;
}

/**
 * @return metaData
 * return the meta data
 */
public String getMetaData() {
    return metaData;
}

/**
 * @param metaData
 * set the meta data
 */
public void setMetaData(String metaData) {
    this.metaData = metaData;
}

/**
 * @return rawData
 * return the raw data
 */
public String getRawData() {
    return rawData;
}

/**
 * @param rawData
 * set the raw data
 */
public void setRawData(String rawData) {
    this.rawData = rawData;
}

/**
 * @return the hostInfo
 * return the host information
 *           key:ip address, value:port number of the host
 */
public Map<String, String> getHostInfo() {
    return hostInfo;
}

/**
 * @param hostInfo the hostInfo to set
 * set the host information 
 *          key:ip address, value:port number of the host
 */
public void setHostInfo(Map<String, String> hostInfo) {
    this.hostInfo = hostInfo;
}

}

The rawData field will contain JSON as string. While the data is getting persisted in database. The rawData is saved as string form and not as a JSON document.

Please suggest.

karthikprasad13 commented 6 years ago

Hi @muditskyfactor,

You can define rawData as Map<String, String>. It will create a JSON Object in MongoDB. Can you provide your sample data?

-Karthik

muditskyfactor commented 6 years ago

Thanks! Karthik,

Following is the sample json data that would come in the rawData field

{ "_id" : "4692c8fd-1ebf-4b0b-a9a4-d75e114fdabb", "box_volume" : 11176.0, "object_host_message" : [ "]C10259612850147114161000158" ], "device_id" : 32, "bar_codes" : { "vcc" : "3", "cc" : "3" }, "object_gap_value" : 69.5199966430664, "poly_unit" : "inch", "object_id" : "70e7b35c-d6da-4fb8-8c0c-335c5ef56af4", "real_volume" : 11154.0, "object_conditions" : [ "PDFw9612", "Shape", "ValidWeight", "ValidRead", "LFT", "ValidDim" ], "angle" : -3.0, "object_secondary_id" : 7154, "position" : [ { "x" : 0.0, "y" : 41.2999992370605, "z" : 2.6800000667572 }, { "x" : 0.0500000007450581, "y" : 30.4799995422363, "z" : 2.6800000667572 }, { "x" : 39.3800010681152, "y" : 41.4799995422363, "z" : 2.6800000667572 }, { "x" : 39.4300003051758, "y" : 30.6599998474121, "z" : 2.6800000667572 } ], "object_speed" : 544.489990234375, "device_name" : "01", "udf_fields" : { "uds4" : "00", "uds5" : "OK", "uds2" : "<owe unit=\"lbs\">20.60", "uds3" : "0", "uds1" : "02/10/2012;17:58:53;0206;00;4059;070;00155493466.80000206OcsRx,OcsInvalid,OcsGood000206", "udi4" : "10", "udi5" : "0", "udi2" : "206", "udi3" : "1" }, "object_weight_unit" : "lbs", "object_dimentions_unit" : "inch", "object_speed_unit" : "ft/min", "object_dimensions" : { "OBJECT_WIDTH" : 10.8000001907349, "OBJECT_LENGTH" : 39.7999992370605, "OBJECT_HEIGHT" : 2.59999990463257 }, "angle_unit" : "degree/10", "seqnb" : 7155, "object_scan_time" : ISODate("2018-05-08T13:57:36.840Z"), "object_weight_value" : 20.6000003814697, "object_gap_unit" : "inch", "object_index" : 14718 }

-Mudit

devender-yadav commented 6 years ago

Hi @muditskyfactor,

I am assuming you don't know the structure of the objects to be stored under rawData field. So, you cant define its structure.

It's not convertible to Map<String, String> as JSON structure is complex (containing embedded objects, lists, etc).

Saving to JSON object is not possible as of now using Kundera.

Although, using native java driver, you can convert JSON string in Document and store it.

Document myDoc = Document.parse(jsonString)

Anyways how will user define this need of conversion to JSON field in Entity Class. Any thoughts on this?