LOMBOK default field value doesn't work with ORIKA-MAPPER - spring

My contract class
#JsonIgnoreProperties(ignoreUnknown = true)
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class PaginationRequest {
private String sortBy;
}
My service class
#JsonIgnoreProperties(ignoreUnknown = true)
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class PaginationRequest {
#Default private String sortBy = "publishDate";
}
After Orika-mapper converting I get sortBy with null value.
When user submit nothing, how can we have sortBy come with its default value?

I end up having my contract class added the default constructor with default field value:
public PaginationRequest() {
sortBy = "publishDate";
}
Because it can't help even I tried to have #Default private String sortBy = "publishDate"; with my contract field

Related

Mongodb created date return null

When I create product createdDate return correct, but on update createdDate return null, I tried to implements persistable but still not working
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#SuperBuilder
public class BaseEntity implements Persistable<String> {
#MongoId(value = FieldType.OBJECT_ID)
private String id;
#CreatedDate
private Date createdDate;
#LastModifiedDate
private Date updatedDate;
#Override
public boolean isNew() {
return StringUtils.isEmpty(id);
}
}
To get value for createdDate you need to add #EnableMongoAuditing to main spring class
#SpringBootApplication
#EnableMongoAuditing

Spring data JPA: embedded ID error when saving the entity

I have the following entity classes:
#Embeddable
#Getter
#Setter
public class OrganizationCyclePlageKey implements Serializable {
#Column(name = "organization_id")
Long organizationId;
#Column(name = "cycle_plages_id")
Long cyclePlagesId;
...
equals() and hashCode() methods come here
#Entity
#Table(name = "organization_cycle_plages")
#Getter
#Setter
public class OrganizationCyclePlage {
#EmbeddedId
private OrganizationCyclePlageKey id;
#ManyToOne
#MapsId("organizationId")
#JoinColumn(name = "organization_id")
Organization organization;
#ManyToOne
#MapsId("cyclePlagesId")
#JoinColumn(name = "cycle_plages_id")
CyclePlage cyclePlage;
...
other attributes
}
#Entity
#Getter
#Setter
public class CyclePlage extends AbstractEntity {
#OneToMany(mappedBy = "cyclePlage")
private Set<OrganizationCyclePlage> organizationCyclePlages;
...
}
#Entity
#DynamicUpdate
#Getter
#Setter
public class Organization extends AbstractEntity {
#OneToMany(mappedBy = "organization")
private Set<OrganizationCyclePlage> organizationCyclePlages = new HashSet<>();
...
}
SpringBoot app starts up normally without errors.
But when I try to save an instance of OrganizationCyclePlage:
OrganizationCyclePlage ocp = new OrganizationCyclePlage();
ocp.setOrganization(organization);
ocp.setCyclePlage(cyclePlage);
organizationCyclePlageRepository.save(ocp);
it raises the error when calling organizationCyclePlageRepository.save(ocp):
org.hibernate.PropertyAccessException: Could not set field value [361] value by reflection : [class com.XXXX.OrganizationCyclePlageKey.cyclePlagesId] setter of com.XXXX.OrganizationCyclePlageKey.cyclePlagesId
What's wrong with these relations?
I had to add the constructor into the OrganizationCyclePlageKey class to init the foreign keys values as well a default constructor via #NoArgsConstructor annotation:
public OrganizationCyclePlageKey(Long organizationId, Long cyclePlagesId) {
this.organizationId = organizationId;
this.cyclePlagesId = cyclePlagesId;
}
and init the OrganizationCyclePlageKey instance in the OrganizationCyclePlage class:
public class OrganizationCyclePlage {
private OrganizationCyclePlageKey id = new OrganizationCyclePlageKey();
...
}

Why is BeanUtils.copyProperties dependency of spring not working?

#Table(name = "obj")
#Entity
#Data
#EqualsAndHashCode(callSuper = false)
#Audited(name = "Obj")
#JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Obj {
//PROPERTIES
}
BeanUtils.copyProperties is not working to copy the associations for this entity. It is unable to find getters and setters even when they get automatically defined using #Data of Lombok.

Cannot map nested #Data with MapStruct

When trying to map nested Object using #Data and #Builder, mapStruct throw the following errors: "No read accessor found for property "profile" in target type."
#Mapper(componentModel = "spring")
public interface AuthMapper {
// only for testing mapping is working
#Mapping(target = "profile.organization", source = "organization")
RequestCreateOktaUser toEntity(Integer organization);
// only for testing mapping is working
#Mapping(target = "profile.login", source = "request.profile.email")
RequestCreateOktaUser toEntity(RequestMobilePreRegisterLocation.User request);
// Throw error "No read accessor found for property "profile" in target type" at compile time
#Mapping(target = "profile.organization", source = "organization")
#Mapping(target = "profile.login", source = "request.profile.email")
RequestCreateOktaUser toEntity(RequestMobilePreRegisterLocation.User request, Integer organization);
}
Model using Lombok simplified for simplicity
#Data
#NoArgsConstructor
#AllArgsConstructor(access = AccessLevel.PRIVATE)
#Builder
public class RequestCreateOktaUser {
private Profile profile;
#Data
#NoArgsConstructor
#AllArgsConstructor(access = AccessLevel.PRIVATE)
#Builder
public static class Profile {
private String login;
private String email;
private Integer organization;
}
}
#Data
#NoArgsConstructor
#AllArgsConstructor(access = AccessLevel.PRIVATE)
#Builder
public class RequestMobilePreRegisterUser {
#Valid
private User user;
#Data
#NoArgsConstructor
#AllArgsConstructor(access = AccessLevel.PRIVATE)
#Builder
public static class User {
#Valid
private Profile profile;
#Data
#NoArgsConstructor
#AllArgsConstructor(access = AccessLevel.PRIVATE)
#Builder
public static class Profile {
#NotEmpty
#Email
private String email;
}
}
The two first Mapping are working as expected, but when trying to combine the two the following errors is being thrown at compile time "No read accessor found for property "profile" in target type."
If someone could help me on this one I would really appreciate.
Thanks a lot,
Jonathan.
solution:
#Mapper(componentModel = "spring", builder = #Builder(disableBuilder = true))

Embeddable is creating 2 tables jpa springs

I have below entites
AbstractOrderEntry :
#Getter #Setter
public class AbstractOrderEntry implements Serializable
{
#ElementCollection
private List<DiscountValue> discountValues = new ArrayList<>();}
}
CartEntry extending AbstractOrderEntry
#Entity
#EntityListeners(value = AuditListener.class)
#Table(name = "cartentry")
#Getter #Setter
public class CartEntry extends AbstractOrderEntry implements Serializable,Cloneable,Auditable
{
#Id
#Column(name="CartEntryID",nullable = false)
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
}
DiscountValueEntity:
#Embeddable
#Getter #Setter
public class DiscountValue {
private String code;
private BigDecimal value;
private BigDecimal appliedValue;
}
When I start the server it is generating to tables like
cart_discountValue
cart_discountvalue
one with camelcase and other is with lowercase.
We are also extending AbstractOrderEntry for order entity as well hence for order also 2 tables are getting created.
How can overcome this issue.Because of this issue table data is not properly persisting.
Thanks , Inadvance.
Sree

Resources