Error when saving composites id inside a for loop - spring

I am trying to persist some objects that have a composite id. If I am only sending an array with one element it works fine, but it the array has more than one it throws an exception when saving the first one. public boolean
addParamsToChart(List<ChartParams> chartParams, Long chartId) {
List<ChartParams> chartParamsList = new ArrayList<>();
for(ChartParams chartParam: chartParams) {
ChartParamsId id = new ChartParamsId();
// id.setChartId(chartId);
id.setChartId(chartParam.getChart().getId());
id.setParamId(chartParam.getParam().getId());
id.setContextSourceId(chartParam.getContextSource().getId());
chartParam.setChartParamsId(id);
if(chartParamsRepository.save(chartParams) !=null) {
chartParamsList.add(chartParam);
}
}
if(chartParamsList.size()!=chartParams.size()) {
// something went wrong, delete previous inserted
deleteChartParams(chartParamsList);
chartRepository.delete(chartId);
return false;
}
return true;
}
[{
"chart": {
"id": 49,
"cv": {
"id": 1,
"name": "Money",
"category": {
"id": 1,
"name": "Euros"
},
"definition": "\"European curreny.\" [EU:euro]",
"enabled": true,
"cvid": "CC:1010"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Euro saving charts"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 3,
"name": "euro",
"internal": "eu",
"abbreviatedName": "eu"
} }]
But for this having two objects inside instead of one is not working, throwing an exception at the first save.
[{
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 6,
"name": "stock",
"internal": "st",
"abbreviatedName": "st"
} }, {
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 7,
"name": "Sold stock",
"internal": "st_sold",
"abbreviatedSequence": "st_sold"
} }]
The exception I got is:
org.hibernate.id.IdentifierGenerationException: null id generated for:class eu.stocks.chart.chartParams.ChartParams

Related

How to filter out only entries of an embedded list with elastic search?

I want to filter out only entries from a list "NestedRecords" inside each "Record" entry.
Let's say I have these Id's of nested records which should be returned:
nestedRecordsToBeReturned = [1,3]
This is the given table with records and nestedRecords inside each record:
{
"Records": [
{
"Record": "record1",
"NestedRecords": [
{
"id": 1,
"label": "l1"
},
{
"id": 2,
"label": "l2"
},
{
"id": 3,
"label": "l3"
},
{
"id": 4,
"label": "l4"
},
{
"id": 6,
"label": "l6"
}
]
},
{
"Record": "record2",
"Records": [
{
"id": 1,
"label": "l1"
},
{
"id": 2,
"label": "l2"
},
{
"id": 4,
"label": "l4"
},
{
"id": 7,
"label": "l7"
}
]
},
{
"Record": "record3",
"Records": [
{
"id": 2,
"label": "l2"
},
{
"id": 3,
"label": "l3"
},
{
"id": 9,
"label": "l9"
},
{
"id": 10,
"label": "l10"
}
]
}
]
}
This is what I want to be returned:
{
"Records": [
{
"Record": "record1",
"NestedRecords": [
{
"id": 1,
"label": "l1"
},
{
"id": 3,
"label": "l3"
},
]
},
{
"Record": "record2",
"Records": [
{
"id": 1,
"label": "l1"
},
]
},
{
"Record": "record3",
"Records": [
{
"id": 3,
"label": "l3"
},
]
}
]
}
How do I achieve this with an elastic query?
Is it even possible to filter a list which is embedded in a document?

How to groupBy the collection of Laravel with the nested field of type array?

I have this collection, and I want to change this data in order that the key will be the branches id and the value will be the items:
{
"data": [
{
"id": 5020,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
{
"id": 6,
"title": "vegetable",
},
},
{
"id": 5025,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
}
]
}
I want to group by this data by branches -> id , something like this:
{
"data": [
"7" : [
{
"id": 5020,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
{
"id": 6,
"title": "vegetable",
},
},
{
"id": 5025,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
]
}
],
"6" : [
{
"id": 5020,
"category_id": 577,
"branches": [
{
"id": 7,
"title": "water",
},
{
"id": 6,
"title": "vegetable",
},
},
],
}
How can I use the group By method of collection in Laravel? I want to have data like this and my data is the type of collection
You can use a callback in groupBy method to provide custom logic.
Example:
collect($data)->groupBy(fn($item, $key) => $item['branches'][0]['id']);
But in this example I want to suggest you to use loop:
$myGroups = [];
foreach($data as $item) {
foreach($item['branches'] as $branch) {
$myGroups[$branch['id']][] = $item;
}
}

How to use filter in map

I have following code:
%dw 2.0 output application/json var attributeIdMapping =
${vault::attributeIdMapping}
--- {
"objectTypeId": 3,
"attributes": vars.requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name == key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
attributeIdMapping:
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
vars.requestBody:
{
"displayName": "TestMulesoft2",
"description": "Test"
}
But my filter shows null in the end. As I understand key is not passed to level below map itself. How can I get this working?
I put all the input data inside the script to simplify reproduction. The problem seems to be using the operator == to compare a string and a key types returns empty. Using the operator ~= which attempts to coerce appears to return the expected result:
%dw 2.0
output application/json
var attributeIdMapping =
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
var requestBody = {
"displayName": "TestMulesoft2",
"description": "Test"
}
--- {
"objectTypeId": 3,
"attributes": requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name ~= key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
Output:
{
"objectTypeId": 3,
"attributes": {
"objectTypeAttributeId": "18",
"objectAttributeValues": [
{
"value": "TestMulesoft2",
"key": "displayName"
}
],
"objectTypeAttributeId": "86",
"objectAttributeValues": [
{
"value": "Test",
"key": "description"
}
]
}
}

rethinkdb, How could I pluck the result by a value in particular "array index"?

sample data
[
{
"createdDate": 1508588333821,
"data": {
"image_extension": "png",
"name": "Golden",
"qty": 1,
"remark": "#296-2",
"status": "RETURN",
"owner": [
{
"name": "app1emaker",
"location": 1
},
{
"name": "simss92_lmao",
"location": 31
}
]
},
"deleted": false,
"docId": 307,
"docType": "product",
"id": "db0131f9-9359-4aa3-b6ed-cd9f3ff4aa3e",
"updatedDate": 1553155281691
},
{
"createdDate": 1508588333324,
"data": {
"image_extension": "png",
"name": "Golden",
"qty": 1,
"remark": "#296-2",
"status": "DISCARD",
"owner": [
{
"name": "At533",
"location": 7
},
{
"name": "madsimon",
"location": 64
},
{
"name": "boyboy96",
"location": 1
},
{
"name": "xinfengCN",
"location": 5
}
]
},
"deleted": false,
"docId": 308,
"docType": "product",
"id": "3790bdaa-5347-4ab0-8149-37332c23c6ea",
"updatedDate": 1554555231691
},
...
...
]
And said that, I would like to select the data.owner on array index 0 only (or I should say data.owner[0]), which are
{
"name": "app1emaker",
"location": 1
}
and
{
"name": "At533",
"location": 7
}
in this case. I have a failed code below.
r.db('carbon').table("items").pluck(['id', 'docId', 'createdDate',{data:{name: true, owner:[0]}}])
I saw that for some functions like orderBy, rethinkdb allowed to use orderBy(r.row('data')('owner')(0)('name')) for access nested object, but I have no idea how to do this for pluck? could anyone give me some hints?
Thanks a lot
pluck can not do that, but you can fall back to use the map:
r.db("carbon").table("items").map(function(doc){
return {
"id": doc("id"),
"docId": doc("docId"),
"createdDate": doc("createdDate"),
"data": {
"name": doc("data")("name"),
"owner": doc("data")("owner")(0)
}
}
})

datatables number of results from JSON

I have a pretty simple question on datatables for which I couldn't find an answer.
I'm using Spring MVC to generate a JSON, which has the following format:
{
"content": [
{
"id": 1,
"firstName": "\u00d6sten",
"lastName": "Dixon",
"email": "hdixon0#forbes.com",
"country": "Belarus",
"ipAddress": "196.228.168.184",
"creditCard": "060479202511002980",
"company": "Trunyx",
"timezone": "Atlantic\/South_Georgia",
"iban": "LV55 ECXH IGFF GSEW YHMU G",
"latitude": "52.644",
"longitude": "25.2027",
"race": "Blackfeet"
},
{
"id": 2,
"firstName": "Agn\u00e8s",
"lastName": "Perry",
"email": "wperry1#blogspot.com",
"country": null,
"ipAddress": "75.63.229.226",
"creditCard": "5191970963453131",
"company": "Tanoodle",
"timezone": "Asia\/Kabul",
"iban": "TN75 8279 0633 7374 0209 2905",
"latitude": "11.97444",
"longitude": "-86.09417",
"race": null
},
// more
],
"totalPages": 51,
"last": false,
"totalElements": 503,
"size": 10,
"number": 0,
"sort": null,
"numberOfElements": 10,
"first": true
}
I'd like to display the result using datatables, however it looks like it's expecting a "iTotalRecords" for example, where my JSON has a "totalElements"
How can I specify that mapping?
$(document).ready(function () {
$('#datatable').DataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "members",
"sAjaxDataProp" : 'content',
"columns": [
{ "data": "id" },
{ "data": "firstName" },
{ "data": "lastName" },
{ "data": "email" },
{ "data": "country" },
{ "data": "ipAddress" },
{ "data": "creditCard" },
{ "data": "company" },
{ "data": "timezone" },
{ "data": "iban" },
{ "data": "latitude" },
{ "data": "longitude" },
{ "data": "race" },
]
});

Resources