Spring Data Mongo DB applying #Indexed(unique = true) on nested object - java

I want to insert an object (of PayLoad)for each unique timeStamp value in Log . Annotated timeStamp with #Indexed(unique = true, sparse = true) and log with #Valid.
However , I see duplicates getting inserted. The MongoDB collection used is PayLoad.Here's the code snippet. How do I enforce this unique constrain ?
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
#Jacksonized
#Builder
#Document(collection = "PayLoad")
public class PayLoad implements Serializable {
private static final long serialVersionUID = -1238163054776439285L;
#Id
private String payLoadId;
private String sid;
#JsonAlias({"results_link"})
private String resultsLink;
private Result result;
}
import org.springframework.data.annotation.Id;
import javax.validation.Valid;
#Jacksonized
#Builder
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class SplunkResult implements Serializable {
private static final long serialVersionUID = -1698863054778439285L;
#Id
String resultId;
#JsonAlias({"DC"})
private String dc;
#JsonAlias({"URL"})
private String url;
private String raw;
private String _raw;
#Valid
private List<Log> log;
}
import org.springframework.data.mongodb.core.index.Indexed;
#Value
#Builder(toBuilder = true)
#Jacksonized
#JsonIgnoreProperties(ignoreUnknown = true)
public class Log implements Serializable {
private static final long serialVersionUID = -5238163054776439285L;
#Id
String logId;
#Indexed(unique = true, sparse = true)
String timeStamp;
String dc;
CallStack stk;
}
Also tried using CompoundIndexes like so , but still it does not work.
#CompoundIndexes({
#CompoundIndex(name = "payload_ts_idx", def = "{'result.perfLog.timeStamp' : 1} ",
unique = true, background = true)})
public class PayLoad implements Serializable {

the index not be created,try update this config.
spring.data.mongodb.auto-index-creation=true

Related

Internal Server Error - The given id must not be null

I have this problem now and I would like to ask for help, I can't acess te value of dto.getIdCliente(), but this is passed like JSON for the ServicoPrestadoDTO dto.
Codes bellow...
ServicoPrestadoController.java:
#RestController
#RequestMapping("/api/servicos-prestados")
#RequiredArgsConstructor
public class ServicoPrestadoController {
private final ClienteRepository clienteRepository;
private final ServicoPrestadoRepository servicoPrestadoRepository;
private final BigDecimalConverter bigDecimalConverter;
#PostMapping
#ResponseStatus(HttpStatus.CREATED)
public ServicoPrestado salvar(#RequestBody ServicoPrestadoDTO dto){
LocalDate data = LocalDate.parse(dto.getData(), DateTimeFormatter.ofPattern("dd/MM/yyyy"));
Integer idCliente = dto.getIdCLiente();
Cliente cliente =
clienteRepository.findById(idCliente)
.orElseThrow(() ->
new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cliente não encontrado!"));
ServicoPrestado servicoPrestado = new ServicoPrestado();
servicoPrestado.setDescricao(dto.getDescricao());
servicoPrestado.setData(data);
servicoPrestado.setCliente(cliente);
servicoPrestado.setValor(bigDecimalConverter.converter(dto.getPreco()));
return servicoPrestado;
}
#GetMapping
public List<ServicoPrestado> pesquisar(
#RequestParam(value = "nome", required = false) String nome,
#RequestParam(value = "mes", required = false) Integer mes
){
return servicoPrestadoRepository.findByNomeClienteAndMes("%" + nome + "%", mes);
}
}
ServicoPrestadoDTO.java:
#Data
#NoArgsConstructor
public class ServicoPrestadoDTO {
private String descricao;
private String preco;
private String data;
private Integer idCLiente;
}
ServicoPrestado.java:
#Entity
#Data
public class ServicoPrestado {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(nullable = false, length = 150)
private String descricao;
#ManyToOne
#JoinColumn(name = "id_cliente")
private Cliente cliente;
#Column
private BigDecimal valor;
#Column
#JsonFormat(pattern = "dd/MM/yyyy")
private LocalDate data;
}
I don't undertand why is not getting the servico send by my servico-prestado-form.ts like JSON.
Your ServicoPrestadoDTO class has a field idCLiente and not idCliente (you have an uppercase "L").
Try changing it to the proper casing of idCliente.
Does that fix it for you?
While using lombok, add the #AllArgsConstructor annotation to your ServicoPrestadoDTO class.
This way, Jackson deserializer will be able to deserialize your JSON into an object.

Getting nulls while using ModelMapper

I'm trying to utilize the ModelMapper in my convertion process. What I need to do is to convert the Sample entity to SampleDTO object.
I have the Sample entity like the following:
#Entity
#Table(name = "sample", schema = "sample_schema")
#Data
#NoArgsConstructor
public class Sample {
private static final String SEQUENCE = "SAMPLE_SEQUENCE";
#Id
#SequenceGenerator(sequenceName = SEQUENCE, name = SEQUENCE, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
private Long id;
#Column(name = "name")
private String name;
#Column
private String surname;
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinColumn(name = "id_deetails")
private Details details;
}
Which holds the Details one:
#Entity
#Table(name = "details", schema = "sample_schema")
#Data
#NoArgsConstructor
public class Details {
private static final String SEQUENCE = "DETAILS_SEQUENCE";
#Id
#SequenceGenerator(sequenceName = SEQUENCE, name = SEQUENCE, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
private Long id;
#Column(name = "street_name")
private String streetName;
#Column
private String city;
}
I'd like the DTO to be this format:
#NoArgsConstructor
#AllArgsConstructor
#Data
public class SampleDTO {
private Long id;
private String name;
private String surname;
private String streetName;
private String city;
}
I also made a ModelMapper bean like:
#Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
And I made a converter component:
#Component
public class EntityDtoConverter {
private final ModelMapper modelMapper;
#Autowired
public EntityDtoConverter(ModelMapper modelMapper) {
this.modelMapper = modelMapper;
}
public SampleDTO sampleToDto(Sample entity) {
return modelMapper.map(entity, SampleDTO.class);
}
}
The problem is
when I try to use this mapper converter in my service
#Service
public class SampleService {
private final SampleRepository sampleRepository;
private final EntityDtoConverter entityDtoConverter;
#Autowired
public SampleService(SampleRepository sampleRepository, EntityDtoConverter entityDtoConverter) {
this.sampleRepository = sampleRepository;
this.entityDtoConverter = entityDtoConverter;
}
public List<SampleDTO> getSamples() {
List<SampleDTO> samples = sampleRepository.findAll()
.map(entityDtoConverter::sampleToDto);
return new List<SampleDTO>(samplesPage);
}
}
I get nulls in places of Details fields.
I have followed Baeldung's tutorial about model-to-dto conversion with ModelMapper and the documentation of it as well but the least wasn't much of help. There is something I'm missing and I have no idea what it is.
I'm working on:
Java 11
Spring Boot 2.3.0
ModelMapper 2.3.8
Try:
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
Also check: Modelmapper: How to apply custom mapping when source object is null?

Specification Predicate to Search Nested jsonb column Objects

I have stored jsonb object in postgresql like
{"sample": {"lastName": "Sahani", "firstName": "Sanjay"}, "address": "Address2", "bedrooms": 2, "postcode": "40 BS", "propertyType": "Type 2"}
My table name is valuation_report_json
and jsonb column name is params
I can get address using Specification but unable to get sample's firstName
My specification is
public class ValuationReportJSONSpecification implements Specification<ValuationReportJSON>{
private String locale;
private String fieldToSearch;
private String localeParameter;
public ValuationReportJSONSpecification(String locale, String fieldToSearch,String localeParameter) {
this.locale = locale;
this.fieldToSearch = fieldToSearch;
this.localeParameter=localeParameter;
}
#Override
public Predicate toPredicate(Root<ValuationReportJSON> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
// TODO Auto-generated method stub
System.err.println("INSIDE");
// return cb.equal(cb.function("jsonb_extract_path_text", String.class, root.<String>get("params"), cb.literal(this.locale)), this.fieldToSearch);
return cb.equal(cb.function("jsonb_extract_path_text", String.class,root.<String>get("params"), cb.literal(this.locale),cb.literal(this.localeParameter)),this.fieldToSearch);
}
}
My models are
ValuationReportJSON :
#Data
#NoArgsConstructor
#Entity
#JsonIgnoreProperties(ignoreUnknown = true)
#Table(name = "valuation_report_json")
#TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class ValuationReportJSON implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Type(type = "jsonb")
#Column(name = "parameters", nullable = false,columnDefinition = "jsonb")
private Params params;
#OneToOne(cascade = CascadeType.PERSIST)
#JoinColumn(name = "property_id", nullable = true)
private Property property;
#Column(name = "entry_id", nullable = true)
private Integer entryId;
#Column(name = "report_data", nullable = true, columnDefinition = "text")
private String reportData;
#Column(name = "entry_date", nullable = true)
#CreationTimestamp
private Timestamp entryDate;
#Column(name = "modified_date", nullable = true)
#UpdateTimestamp
private Timestamp modifiedDate;
//getter setter
}
Params
#Data
#NoArgsConstructor
#AllArgsConstructor
public class Params implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String address;
private String postcode;
private SampleObj sample;
private int bedrooms;
//getter setter
}
SampleObj:
#Data
#NoArgsConstructor
#AllArgsConstructor
public class SampleObj implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
//getter setter
}
I cannot use join in predicate as my objects are not having mapping or not an entity.
because i have seen same question but objects were entity :Specification/Predicate to Search Nested Objects

Not exposing the path of entities that have a composite primary key to the front end when using the Springframework Page object

I'm working on an API endpoint that returns a Springframework Page response. I want the front end to be able to sort the data but I can't expect the front end to know that the column they want to sort on is actually inside a composite primary key.
In the example below (a simplified version of what I'm working on) you can see that the startDate column is inside a RouteEntityPk class, which is linked to the RouteEntity class with the #EmbeddedId annotation. To Sort on that column the front end would need to add ?sort=pk.startdate,asc to the request. I want the front end to only have to provide ?sort=startdate,asc.
Is there a way - using Spring magic - of having the repository know that startdate == pk.startdate, or will I have to write a translator which will remove the pk when showing the sort column to the front end, and add it where necessary when reading it from the request?
Controller:
#GetMapping(value = "routes/{routeId}", produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Page<Route>> getRouteByRouteId(#PathVariable(value = "routeId") final String routeId,
#PageableDefault(size = 20) #SortDefault.SortDefaults({
#SortDefault(sort = "order", direction = Sort.Direction.DESC),
#SortDefault(sort = "endDate", direction = Sort.Direction.DESC)
}) final Pageable pageable) {
return ResponseEntity.ok(routeService.getRouteByRouteId(routeId, pageable));
}
Service:
public Page<Route> getRouteByRouteId(String routeId, Pageable pageable) {
Page<RouteEntity> routeEntities = routeRepository.findByRouteId(routeId, pageable);
return new PageImpl<>(
Collections.singletonList(routeTransformer.toRoute(routeId, routeEntities)),
pageable,
routeEntities.getContent().size()
);
}
Repository:
#Repository
public interface RouteRepository extends JpaRepository<RouteEntity, RouteEntityPk> {
#Query(value = " SELECT re FROM RouteEntity re"
+ " AND re.pk.routeId = :routeId")
Page<RouteEntity> findByRouteId(#Param("routeId") final String routeId,
Pageable pageable);
}
Entities:
Route:
#Data
#Entity
#Builder(toBuilder = true)
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "ROUTE", schema = "NAV")
public class RouteEntity {
#EmbeddedId
private RouteEntityPk pk;
#Column(name = "NAME")
private String name;
#Column(name = "ORDER")
private Integer order;
#Column(name = "END_DTE")
private LocalDate endDate;
}
RoutePk:
#Data
#Builder(toBuilder = true)
#Embeddable
#NoArgsConstructor
#AllArgsConstructor
public class RouteEntityPk implements Serializable {
private static final long serialVersionUID = 1L;
#Column(name = "ROUTE_ID")
private String routeId;
#Column(name = "STRT_DTE")
private LocalDate startDate;
}
Models:
Route:
#Data
#Builder
public class Route {
public String name;
public String routeId;
public List<RouteItem> items;
}
Item:
#Data
#Builder
public class Item {
public Integer order;
public LocalDate startDate;
public LocalDate endDate;
}
Transformer:
public Route toRoute(String routeId, Page<RouteEntity> routeEntities) {
return Route.builder()
.name(getRouteName(routeEntities))
.routeId(routeId)
.items(routeEntities.getContent().stream()
.map(this::toRouteItem)
.collect(Collectors.toList()))
.build();
}
private Item toRouteItem(RouteEntity item) {
return ParcelshopDrop.builder()
.order(item.getOrder())
.startDate(item.getStartDate())
.endDate(item.getEndDate())
.build();
}
So it looks like the way to do this is to use the other way you can deal with composite primary key's in JPA, the annotation #IdClass. This way you can put the fields in the main entity and refer to them as such.
Below is a link to the baeldung article I followed and the changes to the entities I posted above that make this work:
https://www.baeldung.com/jpa-composite-primary-keys
Entities:
Route:
#Data
#Entity
#Builder(toBuilder = true)
#NoArgsConstructor
#AllArgsConstructor
#IdClass(RouteEntityPk.class)
#Table(name = "ROUTE", schema = "NAV")
public class RouteEntity {
#Id
#Column(name = "ROUTE_ID")
private String routeId;
#Id
#Column(name = "STRT_DTE")
private LocalDate startDate;
#Column(name = "NAME")
private String name;
#Column(name = "ORDER")
private Integer order;
#Column(name = "END_DTE")
private LocalDate endDate;
}
RoutePk:
#Data
#Builder(toBuilder = true)
#NoArgsConstructor
#AllArgsConstructor
public class RouteEntityPk implements Serializable {
private static final long serialVersionUID = 1L;
private String routeId;
private LocalDate startDate;
}
This is one solution, probably not the best, but you can transform Pageable object in order to replace the field name like this :
In your controller getRouteByRouteId method :
List<Order> orders = pageable.getSort().stream().map(o -> o.getProperty().equals("startdate") ? new Order(o.getDirection(), "pk.startdate"): o).collect(Collectors.toList());
Then you can call the service with the modified object :
return ResponseEntity.ok(routeService.getRouteByRouteId(routeId, PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(orders))));

How to update existing value in table without inserting new using Java SpringBoot

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.

Categories