Why is BeanUtils.copyProperties dependency of spring not working? - spring

#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.

Related

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();
...
}

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))

ConcurrentModificationException: When update spring data jpa parent entity

I have two jpa entities: Family and FamilyMembers.
Family:
#Entity
#Table(name = "family")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Family implements Serializable {
#OneToMany(mappedBy = "family")
private Set<FamilyMember> familyMembers = new HashSet<>();
}
FamilyMember
#Entity
#Table(name = "family_member")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class FamilyMember implements Serializable {
#ManyToOne()
#JoinColumn(name="family_id")
private Family family;
}
The family entity is the parent entity of family members.
My problem came on when I try to update the parent entity "family" using the below code:
familyRepository.save(family);
I got the below exception:
java.util.ConcurrentModificationException:
Please advise!
I have solved the problem.
I was calling below method within the same transaction:
#Override
#Transactional(readOnly = true)
public Optional<EntityAuditConfiguration> findByEntityName(String entityName) {
return entityAuditConfigurationRepository.findByEntityName(entityName);
}
I changed the method to create a new spring transaction as follows:
#Override
#Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public Optional<EntityAuditConfiguration> findByEntityName(String entityName) {
return entityAuditConfigurationRepository.findByEntityName(entityName);
}

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

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

Spring execute method after hibernate lazy loading

I am using Spring Boot 1.3.1 including Spring Data JPA. I'd like to execute a method after any lazy loading to do some translations on the loaded object.
Example:
#Entity
#Table(name = "commune")
public class Commune extends CommuneBase {
}
#MappedSuperclass
public abstract class CommuneBase {
private Region region;
}
#Entity
#Table(name = "region")
public class Region extends RegionBase {
}
#MappedSuperclass
public abstract class RegionBase {
private String name;
}
Testcode:
Commune commune = communeRepository.findOne(communeId);
Region region = commune.getRegion();
The result of getRegion() should now be translated.
I tried it with an aspect and the following pointcut:
#AfterReturning(pointcut="execution(* com.mycompany.application.data.domain.Commune.getRegion(..))", returning="returnValue")
The pointcut is not called. Other pointcuts in the same project are working as expected.
Any help is appreciated

Resources