I am strucked with this issue. I have created a POJO for nested JSON and I am getting data in MarketPrice object where marketPrices is an ArrayList which has two elements.
This is MarketPrice POJO class and actually I need to save it into the MarketPrice table. I.e, entire JSON object. But I have two entities. How can this be possible?
MarketPrice.java
#Entity
#Table(name = "MarketPrice")
public class MarketPrice {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "itemId")
private Long itemId;
#Column(name = "analysisDate")
private Date analysisDate;
#Column(name = "marketName")
private String marketName;
#Column(name = "category")
private String category;
#Column(name = "marketPlace")
private String marketPlace;
#Column(name = "state")
private String state;
#Column(name = "district")
private String district;
public ArrayList<Items> marketPrices;
Items.java
public class Items implements Serializable {
private static final long serialVersionUID = -2428562977284114465L;
#Id
#Column(name="id")
private int id;
#Column(name = "itemName")
private String itemName;
#Column(name = "unitofPrice")
private String unitofPrice;
#Column(name = "minimumPrice",columnDefinition = "Float(10,2)")
private Float minimumPrice;
#Column(name = "maximumPrice",columnDefinition = "Float(10,2)")
private Float maximumPrice;
This is my nested JSON data I'm getting from at the server side in the controller:
JSON data in marketPrices
{
"marketPrices": [{
"itemName": "Mango",
"unitofPrice": "Kg",
"minimumPrice": "10",
"maximumPrice": "20"
}, {
"itemName": "Grapes",
"unitofPrice": "Kg",
"minimumPrice": "30",
"maximumPrice": "40"
}],
"state": "xyz",
"district": 4,
"marketPlace": 5001,
"marketName": "pmc",
"category": "Fruits"
}
Controller.java
#RequestMapping(value = {"/saveAnalysis"} , method = RequestMethod.POST,consumes = "application/json")
#ResponseBody
public MarketPrice bulkSaveMarketAnalysis(#RequestBody
String marketPrices, HttpServletResponse response,
HttpServletRequest request) throws JsonProcessingException, IOException, JSONException{
MarketPrice marketPrice1 = new MarketPrice();
System.out.println("Json Data"+marketPrices);//here am getting valid nested json from UI
Gson gson = new Gson();
MarketPrice marketPrice = gson.fromJson(marketPrices, MarketPrice.class);//converting it into Entity type all values are present in it.
//Am strucked after this,How to save nested json into DB.
String marketDataResponse = analyserService.saveListOfMarketPrice(marketPrice);
marketPrice1.setStatusMessage("success");
return marketPrice1;
}
DAO.java
public String saveListOfMarketPrice(MarketPrice marketPrice) {
System.out.println("In Analyser DAO fro bulk saving");
final Session session = getSession();
session.beginTransaction();
marketPrice.setAnalysisDate(new Date());
for (Items item : marketPrice.marketPrices) {
marketPrice.currentItem = item;
marketPrice.setItemName(marketPrice.currentItem.getItemName());
marketPrice.setUnitofPrice(marketPrice.currentItem.getUnitofPrice());
marketPrice.setMinimumPrice(marketPrice.currentItem.getMinimumPrice());
marketPrice.setMaximumPrice(marketPrice.currentItem.getMaximumPrice());
session.save(marketPrice);
}
session.getTransaction().commit();
session.close();
return "success";
}
After making these changes to DAO it saved finally
Thank you.
As discussed in the comments, you can modify your code as below to make it work as expected.
MarketPrice.java
#Entity
#Table(name = "MarketPrice")
public class MarketPrice {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "itemId")
private Long itemId;
#Column(name = "analysisDate")
private Date analysisDate;
#Column(name = "marketName")
private String marketName;
#Column(name = "category")
private String category;
#Column(name = "marketPlace")
private String marketPlace;
#Column(name = "state")
private String state;
#Column(name = "district")
private String district;
#Transient
public Items currentItem;
#Column(name = "itemName")
public String getItemName() {
return this.currentItem.itemName;
}
#Column(name = "unitofPrice")
public String getUnitofPrice() {
return this.currentItem.unitofPrice;
}
#Column(name = "minimumPrice",columnDefinition = "Float(10,2)")
public Float getMinimumPrice() {
return this.currentItem.minimumPrice;
}
#Column(name = "maximumPrice",columnDefinition = "Float(10,2)")
public Float getMaximumPrice() {
return this.currentItem.maximumPrice;
}
#Transient
public ArrayList<Items> marketPrices;
Items.java
public class Items implements Serializable {
private static final long serialVersionUID = -2428562977284114465L;
#Id
#Column(name="id")
private int id;
public String itemName;
public String unitofPrice;
public Float minimumPrice;
public Float maximumPrice;
DAO.java
public String saveListOfMarketPrice(MarketPrice marketPrice) {
System.out.println("In Analyser DAO fro bulk saving");
final Session session = getSession();
session.beginTransaction();
for (Items item : marketPrice.marketPrices) {
marketPrice.currentItem = item;
session.save(marketPrice);
}
session.getTransaction().commit();
session.close();
return "success";
}
You should create one bigger entity for the whole Json ant it will have a list of you marketPrice objects #onetomany. Read about that annotation. And also parse your whole incoming object at once not only the list of marketPrices inside. You can read how to parse your whole object here: GSON parsing without a lot of classes
You need something like:
JsonObject rootObj = parser.parse(json).getAsJsonObject();
Then you should describe that structure in an entity:
#Entity
#Data
#Table(name = "your_table")
public class YourEntity{
//you should describe your parameters here too
//of the parsed json. it has other data in it not only the list of `MarketPrices`...
#OneToMany(mappedBy = "yourEntity",
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
fetch = FetchType.LAZY, orphanRemoval = true)
private List<MarketPrice> prices;
.....
Related
Problem
I am trying to store an object in my Postgres database. This consists of the Order.class, (List) OrderDevice.class, and a Department.class.
The important thing is that the OrderDevices are always stored new in the DB, but a Department may already exist.
When I try to save the object to my database using save I get the following error message: (shown below)
I get the error message "detached entity passed to persist: com.niclas.model.OrderDevice" if the department does not exist yet, if the department exists the error message looks like this: "detached entity passed to persist: com.niclas.model.Department".
Solution attempts
This solution cannot be used because I do not use bidirectional mapping.
(I don't want to use a bidirectional mapping because I want to access the departments without an order.)
I also tried to change the Cascade types to MERGE like in this solution
I also tried using #Transactional on the method
I also tried to save the children in the database first and then the parent like this:
departmentRepository.save(order.getDepartment()); orderDeviceRepository.saveAll(order.getDevices()); orderRepository.save(order);
I hope I have described my good enough and I am happy about suggestions for solutions
Error.log
The log can be viewed here. (The formatting did not work here)
Order.class
#Entity
#Table(name = "orders")
public class Order extends AuditModel {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO) //TODO better config for GenerationType
private long id;
#Column(name = "order_id")
private String orderId;
#Column(name = "department_id")
private long departmentId;
#OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = "department", referencedColumnName = "id")
private Department department;
#JsonProperty("deviceList")
#OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = "order_id", referencedColumnName = "order_id")
private List<OrderDevice> devices;
#JsonProperty("forename")
#Column(name = "sender_forename")
private String senderForename;
#JsonProperty("surname")
#Column(name = "sender_surname")
private String senderSurname;
#Column(name = "notes", columnDefinition = "TEXT")
private String notes;
#Column(name = "month")
private int month;
#Column(name = "year")
private int year;
public Order() {
}
... Getter/Setters
}
OrderDevice.class
#Entity
#Table(name = "order_devices")
public class OrderDevice extends AuditModel{
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO) //TODO better config for GenerationType
private long id;
#Column( name = "order_id", insertable = false, updatable = false )
private String orderId;
#Column(name = "device_id")
private long deviceId;
#Column(name = "device_name")
private String deviceName;
#Column(name = "priceName")
private String priceName;
#Column(name = "price")
private double price;
#Column(name = "count")
private int count;
public OrderDevice() {
}
... Getters/Setters
}
Department.class
#Entity
#Table(name = "departments")
public class Department {
//TODO add Form Validation
//TODO better Naming for From Attributes on Frontend and Backend
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO) //TODO better config for GenerationType
private long id;
#Column(name = "department_name")
private String department;
#Column(name = "contact_person_forename")
private String forename;
#Column(name = "contact_person_surname")
private String surname;
#Column(name = "contact_person_mail")
private String mail;
#Column(name = "street")
private String street;
#Column(name = "house_number")
private String houseNumber;
#Column(name = "location")
private String location;
#Column(name = "postal_code")
private int postalCode;
#Column(name = "country")
private String country;
#Column(name = "auto_send_invoice")
private boolean autoSend;
#Column(name = "registered")
private boolean registered;
public Department() {
}
... Getter/Setters
}
OrderController.class
#Slf4j
#RestController
public class OrderController {
private final DepartmentRepository departmentRepository;
private final OrderRepository orderRepository;
private final OrderDeviceRepository orderDeviceRepository;
public OrderController(OrderRepository orderRepository, DepartmentRepository departmentRepository,
OrderDeviceRepository orderDeviceRepository) {
this.orderRepository = orderRepository;
this.departmentRepository = departmentRepository;
this.orderDeviceRepository = orderDeviceRepository;
}
#PostMapping("/orders/add")
public ResponseEntity<Order> addDepartment(#RequestBody Order order) throws JsonProcessingException {
order.setOrderId(order.generateOrderId());
DateTime dateTime = new DateTime();
order.setMonth(dateTime.getMonthOfYear());
order.setYear(dateTime.getYear());
order.getDevices().forEach(orderDevice -> {
orderDevice.setOrderId(order.getOrderId());
});
//departmentRepository.save(order.getDepartment());
//orderDeviceRepository.saveAll(order.getDevices());
orderRepository.save(order);
return new ResponseEntity<>(order, HttpStatus.CREATED);
}
Update
If the objects are created in this way, no error will occur and the order will be successfully saved in the database.
However, I don't understand why it works this way and not via ObjectMapper. Does anyone know why?
#PostMapping("/orders/add")
public ResponseEntity<Order> addDepartment(#RequestBody JsonNode jsonNode) throws JsonProcessingException {
Order order = new Order();
JsonNode departmentJson = jsonNode.get("department");
Department department;
if ( departmentJson.get("id").canConvertToInt() ) {
department = departmentRepository.findDepartmentById(departmentJson.get("id").asInt());
} else {
department = new Department();
department.setDepartment(departmentJson.get("department").asText());
department.setForename(departmentJson.get("forename").asText());
department.setSurname(departmentJson.get("surname").asText());
department.setMail(departmentJson.get("mail").asText());
department.setStreet(departmentJson.get("street").asText());
department.setHouseNumber(departmentJson.get("houseNumber").asText());
department.setLocation(departmentJson.get("location").asText());
department.setPostalCode(departmentJson.get("postalCode").asInt());
department.setCountry(departmentJson.get("country").asText());
department.setAutoSend(departmentJson.get("autoSend").asBoolean());
department.setRegistered(departmentJson.get("registered").asBoolean());
}
order.setDepartment(department);
order.setOrderId(order.generateOrderId());
order.setDepartmentId(department.getId());
List<OrderDevice> orderDevices = new ArrayList<>();
JsonNode devices = jsonNode.get("deviceList");
for (JsonNode node : devices) {
//TODO replace this mess with objectMapper
if (node.has("count") && node.get("count").asInt() != 0){
OrderDevice device = new OrderDevice();
device.setOrderId(order.getOrderId());
device.setDeviceId(node.get("id").asLong());
device.setDeviceName(node.get("deviceName").asText());
device.setPriceName(node.get("priceName").asText());
device.setPrice(node.get("price").asDouble());
device.setCount(node.get("count").asInt());
orderDevices.add(device);
}
}
order.setDevices(orderDevices);
order.setSenderForename(jsonNode.get("forename").asText());
order.setSenderSurname(jsonNode.get("surname").asText());
order.setNotes(jsonNode.get("notes").asText());
DateTime dateTime = new DateTime();
order.setMonth(dateTime.getMonthOfYear());
order.setYear(dateTime.getYear());
orderRepository.save(order);
return new ResponseEntity<>(order, HttpStatus.CREATED);
}
You can try to use instead of orderRepository.save(order) use orderRespostiory.saveOrUpdate(order).
How can I map the one to many relationship using org.mapstruct framework?
DTO classes:
#Data
public class ScheduledJobDTO {
private String jobName;
private String jobGroup;
private String jobClass;
private String cronExpression;
private Boolean cronJob;
private Long repeatTime;
private Integer repeatCount;
private Set<ScheduledJobParamsDTO> paramtersDTOs;
}
#Data
#EqualsAndHashCode
public class ScheduledJobParamsDTO {
String name;
String value;
}
Domain Classes -
#Data
#Entity
#Table(name = "scheduled_job")
public class ScheduledJob {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "job_name")
private String jobName;
#Column(name = "job_group")
private String jobGroup;
#Column(name = "job_class")
private String jobClass;
#Column(name = "cron_expression")
private String cronExpression;
#Column(name = "is_cron_job")
private Boolean cronJob;
#Column(name = "repeat_time")
private Long repeatTime;
#Column(name = "repeat_count")
private Integer repeatCount;
#Column(name = "trigger_start_date")
private LocalDate triggerStartDate;
#Column(name = "trigger_end_date")
private LocalDate triggerEndDate;
#Column(name = "created_at")
private LocalDate createdAt;
#Column(name = "modified_at")
private LocalDate modifiedAt;
#Column(name = "is_active")
private Boolean active;
#OneToMany(mappedBy = "scheduledJob")
private Set<ScheduledJobParams> parameters;
}
#Data
#Entity
#Table(name = "scheduled_job_params")
#EqualsAndHashCode
public class ScheduledJobParams {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "scheduled_job_id", nullable = false)
ScheduledJob scheduledJob;
String name;
String value;
}
Mapper Class -
#Mapping(source = ".", target = ".")
#Mapping(source = "paramtersDTOs", target = "parameters")
ScheduledJob mapToDomain(ScheduledJobDTO scheduledJobDTO);
Now, the above mapper is mapping the ScheduledJob & ScheduledJobParams, but the ScheduledJobParams has reference of ScheduledJob.
How can I map the reference ScheduledJob to ScheduledJobParams?
You can achieve that through #AfterMapping with #MappedTarget. This is described in the reference documentation: 12.2. Mapping customization with before-mapping and after-mapping methods.
// Java 8+ otherwise you need to use an abstract class and a for-loop instead
#Mapper(componentModel = "spring")
public interface ScheduledJobMapper {
#Mapping(target = "parameters", source = "paramtersDTOs")
ScheduledJob mapToDomain(ScheduledJobDTO dto);
#AfterMapping
default void after(#MappingTarget ScheduledJob domain, ScheduledJobDTO dto) {
domain.getParameters().forEach(scheduledJobParams -> {
scheduledJobParams.setScheduledJob(domain);
});
}
}
However, I am sure you don't need to fill the bidirectional relationship when you map back from the DTO into the entity (this is what I understand as you refer to "domain"). Note printing out or serializing such object i.e. into JSON or XML throws java.lang.StackOverflowError if not properly handled.
I have a simple spring-boot app where Product needs to be stored and conversion between DTO and Entity needs to happen. I am using the ModelMapper dependency. User can attach a ProductCategory to the Product or leave it empty. Similarly Product can have multiple ReplaceNumber or empty. If I dont attach category it gives error. If I attach category it saves the product with the attached category. If I leave the replaceNumbers array empty it saves. If I fill it it gives errors. Errors are described below.
ProductCategory
#Entity
#Table(name = "product_categories")
public class ProductCategory
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotBlank
#Column(name = "name", nullable = false)
#Size(max = 20)
private String name;
public ProductCategory()
{
}
public ProductCategory(String name)
{
this.name = name;
}
}
ReplaceNumber
#Entity
#Table(name = "replace_numbers")
public class ReplaceNumber
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotBlank
#Size(max = 20)
private String partNumber;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "product_id", nullable = false)
private Product product;
public ReplaceNumber()
{
}
public ReplaceNumber(String partNumber)
{
this.partNumber = partNumber;
}
}
Product
#Entity
#Table(name = "products", indexes = {#Index(name= "part_number_index", columnList = "part_number", unique = true)})
public class Product
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotBlank
#Column(name = "part_number", nullable = false)
#Size(max = 20)
private String partNumber;
#NotBlank
#Size(max = 255)
private String description;
#OneToMany(
mappedBy = "product",
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
orphanRemoval = true
)
#Fetch(FetchMode.SELECT)
private List<ReplaceNumber> replaceNumbers = new ArrayList<>();
#ManyToOne
#JoinColumn(name = "product_category_id", referencedColumnName = "id")
private ProductCategory category;
}
Following are the DTO Classes that need to be converted.
ReplaceNumberRequest
public class ReplaceNumberRequest
{
#NotBlank
#Size(max = 20)
private String partNumber;
public String getPartNumber()
{
return partNumber;
}
public void setPartNumber(String partNumber)
{
this.partNumber = partNumber;
}
}
ProductCategoryResponse
public class ProductCategoryResponse
{
private Long id;
private String name;
public ProductCategoryResponse()
{
}
public ProductCategoryResponse(String name)
{
this.name = name;
}
}
ProductRequest
public class ProductRequest
{
#NotBlank
#Size(max = 20)
private String partNumber;
#NotBlank
#Size(max = 255)
private String description;
private List<ReplaceNumberRequest> replaceNumbers = new ArrayList<>();
private ProductCategoryResponse category;
}
ProductService
#Service
public class ProductService
{
#Autowired
ProductRepository productRepository;
public Product create(ProductRequest productRequest)
{
Product product = new Product();
org.modelmapper.ModelMapper modelMapper = new org.modelmapper.ModelMapper();
modelMapper.map(productRequest, product);
return productRepository.save(product);
}
}
If I post the following JSON from Postman
{
"partNumber": "443455783",
"description": "443434",
"replaceNumbers": [],
"category": ""
}
It goes for saving the empty category and produces the following error.
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : org.walana.GP.model.Product.category -> org.walana.GP.model.ProductCategory; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : org.walana.GP.model.Product.category -> org.walana.GP.model.ProductCategory
If I post the following JSON from Postman
{
"partNumber": "443455783",
"description": "443434",
"replaceNumbers": [
{
"partNumber": "123455"
},
{
"partNumber": "343435"
}
],
"category": {
"id": 1,
"name": "Mounting"
}
}
It gives following error.
could not execute statement; SQL [n/a]; constraint [part_number_index]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
I have two tables one is parent and other one is child. When I am trying to save initially, I am able to insert values in both the tables if values not present in Parent table. But at the time of update/insert the values in child table, it is inserting duplicate values.
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class RuleApi {
Long id;
private String market;
private int modelYear;
private String vehicleLine;
private String vehicleLineName;
private String locale;
private String binding;
private String description;
private String createUser;
private String updateUser;
}
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class DescriptorSaveRequest {
#Valid
#NotNull
RuleApi rule;
}
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "MNAVS03_DESCRIPTOR_CONTEXT")
#EntityListeners(AuditingEntityListener.class)
public class DescriptorContext implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Setter(value = AccessLevel.NONE)
#Column(name = "NAVS03_DESCRIPTOR_CONTEXT_K")
private Long id;
#Column(name = "NAVS03_MARKET_N")
private String market;
#Column(name = "NAVS03_MODEL_YEAR_R")
private Integer modelYear;
#Column(name = "NAVS03_VEHICLE_LINE_C")
private String vehicleLine;
#Column(name = "NAVS03_VEHICLE_LINE_N")
private String vehicleLineName;
#Column(name = "NAVS03_LOCALE_N")
private String locale;
#Column(name = "NAVS03_CREATE_USER_C", nullable = false)
private String createUserId;
#CreationTimestamp
#Column(name = "NAVS03_CREATE_S")
private Timestamp createTimestamp;
#Column(name = "NAVS03_LAST_UPDT_USER_C", nullable = false)
private String updateUserId;
#UpdateTimestamp
#Column(name = "NAVS03_LAST_UPDT_S")
private Timestamp updateTimestamp;
}
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "MNAVS04_DESCRIPTOR_RULE")
#EntityListeners(AuditingEntityListener.class)
public class DescriptorRule implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Setter(value = AccessLevel.NONE)
#Column(name = "NAVS04_DESCRIPTOR_RULE_K")
private Long id;
#JoinColumn(name = "NAVS03_DESCRIPTOR_CONTEXT_K", nullable = false)
#ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
private DescriptorContext descriptorContextId;
#Column(name = "NAVS04_BINDING_N",
unique = true)
private String binding;
#Column(name = "NAVS04_DESCRIPTOR_RULE_X")
private String description;
#Column(name = "NAVS04_CREATE_USER_C", nullable = false)
private String createUserId;
#CreationTimestamp
#Column(name = "NAVS04_CREATE_S")
private Timestamp createTimestamp;
#Column(name = "NAVS04_LAST_UPDT_USER_C", nullable = false)
private String updateUserId;
#UpdateTimestamp
#Column(name = "NAVS04_LAST_UPDT_S")
private Timestamp updateTimestamp;
}
#ApiOperation(value = "Create/Update Feature Descriptions", notes = "Create/Update a descriptions based on the given input")
#PostMapping("/descriptor/saveFeatures")
public ResponseEntity<BaseBodyResponse<String>> saveFeatureDescriptions(#Valid #RequestBody DescriptorSaveRequest descriptorSaveRequest) throws Exception {
this.descriptorContextService.saveFeatureDescriptions(
this.descriptorContextMapper.mapDescriptorContext(descriptorSaveRequest),
this.descriptorContextMapper.mapDescriptorRule(descriptorSaveRequest)
);
return ResponseEntity.ok(BaseBodyResponse.result("Saved Successfully"));
}
#Service
public class DescriptorContextService {
//SaveFeatureDescriptions
public void saveFeatureDescriptions(DescriptorContext descriptorContext, DescriptorRule descriptorRule) throws Exception {
DescriptorContext descriptorContext1 =
this.descriptorContextRepository.findByMarketAndModelYearAndVehicleLineAndVehicleLineNameAndLocale(
descriptorContext.getMarket(),
descriptorContext.getModelYear(),
descriptorContext.getVehicleLine(),
descriptorContext.getVehicleLineName(),
descriptorContext.getLocale());
if (descriptorContext1 == null) {
// add a new context
descriptorContext1 = descriptorContextRepository.save(DescriptorContext.builder()
.market(descriptorContext.getMarket())
.modelYear(descriptorContext.getModelYear())
.vehicleLine(descriptorContext.getVehicleLine())
.vehicleLineName(descriptorContext.getVehicleLineName())
.locale(descriptorContext.getLocale())
.createUserId(descriptorContext.getCreateUserId())
.updateUserId(descriptorContext.getUpdateUserId())
.build());
}
Long contextId = descriptorContext1.getId();
List<DescriptorRule> rule = this.descriptorRuleRepository.findByDescriptorContextId(contextId);
if (rule.size() == 0) {
// add a new rule
this.descriptorRuleRepository.save(DescriptorRule.builder()
.descriptorContextId(descriptorContext1)
.binding(descriptorRule.getBinding())
.description(descriptorRule.getDescription())
.createUserId(descriptorContext.getCreateUserId())
.updateUserId(descriptorContext.getUpdateUserId())
.build());
} else {
// update a existing rule
for (DescriptorRule descriptorRule1 : rule) {
if (descriptorRule1.getBinding().equals(descriptorRule.getBinding())) {
descriptorRule1.setDescription(descriptorRule.getDescription());
descriptorRule1.setupdateUserId(descriptorRule.getupdateUserId());
this.descriptorRuleRepository.save(descriptorRule1);
} else {
this.descriptorRuleRepository.save(DescriptorRule.builder()
.descriptorContextId(descriptorContext1)
.binding(descriptorRule.getBinding())
.description(descriptorRule.getDescription())
.createUserId(descriptorContext.getCreateUserId())
.updateUserId(descriptorContext.getUpdateUserId())
.build());
}
}
}
}
}
}
#Component
public class DescriptorContextMapper {
public DescriptorContext mapDescriptorContext(DescriptorSaveRequest descriptorSaveRequest) {
return DescriptorContext.builder()
.market(descriptorSaveRequest.getRule().getMarket())
.vehicleLine(descriptorSaveRequest.getRule().getVehicleLine())
.vehicleLineName(descriptorSaveRequest.getRule().getVehicleLineName())
.modelYear(descriptorSaveRequest.getRule().getModelYear())
.locale(descriptorSaveRequest.getRule().getLocale())
.createUserId(descriptorSaveRequest.getRule().getCreateUser())
.updateUserId(descriptorSaveRequest.getRule().getUpdateUser())
.build();
}
public DescriptorRule mapDescriptorRule(DescriptorSaveRequest descriptorSaveRequest) {
return DescriptorRule.builder()
.id(descriptorSaveRequest.getRule().getId())
.binding(descriptorSaveRequest.getRule().getBinding())
.description(descriptorSaveRequest.getRule().getDescription())
.createUserId(descriptorSaveRequest.getRule().getCreateUser())
.updateUserId(descriptorSaveRequest.getRule().getUpdateUser())
.build();
}
}
{
"rule": {
"binding": "5003",
"description": "Test new 5003-2023 Escape",
"locale": "fr_CA",
"market": "WANAC",
"modelYear": 2023,
"vehicleLine": "TMC",
"vehicleLineName": "Escape",
"createUser": "rdongre",
"updateUser": "rdongre"
}
}
If I am passing this request and values are not present in both the tables then it should insert the values in both the tables which is working as expected with above code. But at the time of update it is going inside the loop and inserting duplicate values. I am trying to update DESCRIPTION in child table if BINDING is present if not it should insert BINDING plus DESCRIPTION
I fixed this by separating Save and Update methods. Thanks to all.
i have faced with the problem, that JSON doesn't containe the list of nested objects.
I have 2 classes, one of them is carrying information about auto service, another one containes information about services.
One autoservice can has many services. So, we have the relation - one to many .
AutoService class:
#Entity
#Table(name = "AutoRate")
public class AutoService {
public AutoService() {
}
#Id
#GeneratedValue(generator = "increment")
#GenericGenerator(name = "increment", strategy = "increment")
private long id;
#Column(name = "serviceName", nullable = false)
private String serviceName;
#Column(name = "imageURL", nullable = false)
private String imageURL;
#Column(name = "mapCoordinate", nullable = false)
private String mapCoordinate;
#Column(name = "websiteURL", nullable = false)
private String websiteURL;
#Column(name = "phoneNumber", nullable = false)
private String phoneNumber;
#OneToMany(fetch = FetchType.EAGER)
#JoinColumn(name = "autoServiceId")
private List<Service> services;
public long getId() {
return id;
}
public String getServiceName() {
return serviceName;
}
public String getImageURL() {
return imageURL;
}
public String getMapCoordinate() {
return mapCoordinate;
}
public String getWebsiteURL() {
return websiteURL;
}
public String getPhoneNumber() {
return phoneNumber;
}
}
Service class:
#Entity
#Table(name = "Service")
public class Service {
public Service() {
}
#Id
#GeneratedValue(generator = "increment")
#GenericGenerator(name = "increment", strategy = "increment")
#Column(name = "serviceId", unique = true, nullable = false)
private long serviceId;
#Column(name = "serviceName", nullable = false)
private String serviceName;
#Column(name = "category", nullable = false)
private String category;
#Column(name = "price", nullable = false)
private int price;
#Column(name = "autoServiceId", nullable = false)
private long autoServiceId;
public long getId() {
return serviceId;
}
public String getCategory() {
return category;
}
public int getPrice() {
return price;
}
public String getServiceName() {
return serviceName;
}
public long getAutoServiceId() {
return autoServiceId;
}
}
Also i am using the JpaRepository to get objects from db:
public interface AutoRateRepository extends JpaRepository<AutoService, Long> {
}
Here is Controller class:
#RestController
#RequestMapping("/directory")
public class ServiceController {
#Autowired
private AutoRateService dataBaseService;
#RequestMapping(value = "/get", method = RequestMethod.GET)
#ResponseBody
public List<AutoService> getData(){
List<AutoService> dataList = dataBaseService.getAll();
return dataList;
}
}
But then when i am trying to get JSON object i am getting next:
[
{
"id": 1,
"serviceName": "SpeedyName",
"imageURL": "Url for speedy",
"mapCoordinate": "123123 44121 ",
"websiteURL": "speedy.com",
"phoneNumber": "1231251"
},
{
"id": 2,
"serviceName": "Другой сервис",
"imageURL": "Урл для второго сервиса",
"mapCoordinate": "123 12фывфы",
"websiteURL": "другойсервис.ком",
"phoneNumber": "12312333"
}
]
There is not lists of nested objects from database ( List<Services>
Can you help me to resolve this problem ?
I was told that i had missed the #JsonManagedReference annotation, but it didn't helped me (
As specified in the comments, you're missing a getServices() method in the AutoService entity exposing the collection for serialization.
As a side note, be weary if your Service entity has a back reference to the parent AutoService. If it does, you'll need to make sure your JSON framework knows how to handle cyclic loops or you will need to trigger ignoring the back reference when you serialize the AutoService instances to avoid a stack overflow.