Spring QuerydslPredicate's naming convention to snake case - spring-data

I'm using Predicate from QueryDsl. Backends internally uses camelCase, but promised to use snake_case when communicating with clients.
I want to use snake_case as query parameters like
http://localhost:8080/inputmethod?protocol_type=SDK
if I pass protocol_type as snake_case, Query Dsl Predicate doesn't parse it. It parse only fields of the type(target entity class) as camelCase. so protocol_type is skipped by getPredicate() of QuerydslPredicateBuilder.
/**
* Creates a Querydsl {#link Predicate} for the given values, {#link QuerydslBindings} on the given
* {#link TypeInformation}.
*
* #param type the type to create a predicate for.
* #param values the values to bind.
* #param bindings the {#link QuerydslBindings} for the predicate.
* #return
*/
#Nullable
public Predicate getPredicate(TypeInformation<?> type, MultiValueMap<String, String> values,
QuerydslBindings bindings) {
...
if (!bindings.isPathAvailable(path, type)) { // <-- here!
// path : "protocol_type"
// type : my.domain.entity.InputMethod
continue;
}
...
}
but it works in cameCase.
http://localhost:8080/inputmethod?protocolType=SDK
How to set the naming convention of Predicate to snake_case?
Controller
#GetMapping
public List<InputMethodDto.Response> getInputMethodTypeList(
#QuerydslPredicate(root = InputMethod.class) Predicate predicate) {
return service.getInputMethodList(predicate);
}
Entity
#Getter
#NoArgsConstructor
#AllArgsConstructor
#Entity
public class InputMethod {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Enumerated(EnumType.STRING)
#Column
private RecordType recordType;
#Enumerated(EnumType.STRING)
#Column
private ProtocolType protocolType;
#Column
private String name;
Config
#EnableWebMvc
#EnableSwagger2
#Configuration
public class SwaggerConfig implements WebMvcConfigurer {
...
}
Jackson naming strategy
Same issue with me.
https://stackoverflow.com/questions/53273966/spring-jpa-querydslpredicate-snake-case
I also set spring.jackson.property-naming-strategy=SNAKE_CASE

Unfortunately I am afraid you can't use snake_case here, simply because _ is considered as property splitter in spring data mapping. So mapper will convert it to protocol and type

Related

Spring Data JPA order by value from OneToMany relation

I am trying to sort a result by nested collection element value. I have a very simple model:
#Entity
public class User {
#Id
#NotNull
#Column(name = "userid")
private Long id;
#OneToMany(mappedBy = "user")
private Collection<Setting> settings = new HashSet<>();
// getters and setters
}
#Entity
public class Setting {
#Id
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "userid")
private User user;
private String key;
private String value;
// getters and setters
}
public interface UserRepository extends JpaRepository<User, Long>, QuerydslPredicateExecutor<User> {
}
I want to have a result returned sorted by the value of one setting.
Is it possible to order by user.settings.value where settings.name = 'SampleName' using Spring Data JPA with QueryDSL?
I've used JpaSpecificationExecutor. let's see findAll for example.
Page<T> findAll(#Nullable Specification<T> spec, Pageable pageable);
Before call this method you can create your specification dynamically (where condition) and Pageable object with dynamic Sort information.
For example
...
Specification<T> whereSpecifications = Specification.where(yourWhereSpeficiation);
Sort sortByProperty = Sort.by(Sort.Order.asc("property"));
PageRequest orderedPageRequest = PageRequest.of(1, 100, sortByProperty);
userRepository.findAll(whereSpecifications, PageRequest.of(page, limit, orderedPageRequest));

spring data jpa fine granular auditing, custom audit

I have requirement where I need to insert user name and group name to which the user belongs (both available in SecurityContext) in the same table.
class Entity
{
#createdBy
String username
#createdBy
String groupname
other fields ...
}
As per requirement. I cant solve this issue by making a user class and referencing it through a foreign key.
With current implementation of AuditingHandler both fields are getting the same value. How do I make sure they get respective values.
Can this be achieved using current implementation ?
If not thn how can I provide custom implementation of AuditingHandler ?
You could make a separate embeddable class and annotate it with #CreatedBy in your parent class. One way is to define a bean implementing AuditorAware, then you can make it return custom object, containing your two required fields. For example, your parent class would look like this (note the listener annotation):
#Entity
#EntityListeners(AuditingEntityListener.class)
public class AuditedEntity {
#Id
#GeneratedValue(generator = "uuid")
#GenericGenerator(name = "uuid", strategy = "uuid")
private String id;
#Embedded
#CreatedBy
private AuditorDetails createdBy;
// setters and getters
}
where AuditorDetails is:
#Embeddable
public class AuditorDetails {
private String username;
private String groupname;
// setters and getters
}
and finally, your AuditorAware bean:
#Component
class AuditorAwareImpl implements AuditorAware<AuditorDetails> {
#Override
public AuditorDetails getCurrentAuditor() {
return new AuditorDetails()
.setUsername("someUser")
.setGroupname("someGroup");
}
}
AuditingHandler fetches your custom AuditorDetails from your AuditorAware bean (it must be single bean implementing it) and sets it in your auditable entity.

How to adapt a child node in sling model of aem6

I am learning to use one of the new features of AEM6 - Sling Models. I have already fetched the properties of a node following the steps described here
#Model(adaptables = Resource.class)
public class UserInfo {
#Inject #Named("jcr:title")
private String title;
#Inject #Default(values = "xyz")
private String firstName;
#Inject #Default(values = "xyz")
private String lastName;
#Inject #Default(values = "xyz")
private String city;
#Inject #Default(values = "aem")
private String technology;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getTechnology() {
return technology;
}
public String getTitle() {
return title;
}
}
and adapted it from a resource
UserInfo userInfo = resource.adaptTo(UserInfo.class);
Now i have the hierarchy as -
+ UserInfo (firstName, lastName, technology)
|
+ UserAddress (houseNo, locality, city, state)
Now I want to fetch the properties of the UserAddress.
I had got some hints from the documentation page, such as -
If the injected object does not match the desired type and the object implements the Adaptable interface, Sling Models will try to adapt it. This provides the ability to create rich object graphs. For example:
#Model(adaptables = Resource.class)
public interface MyModel {
#Inject
ImageModel getImage();
}
#Model(adaptables = Resource.class)
public interface ImageModel {
#Inject
String getPath();
}
When a resource is adapted to MyModel, a child resource named image is automatically adapted to an instance of ImageModel.
but I don't know how to implement it in my own classes. Please help me out with this.
It sounds like you need a separate class for the UserAddress to wrap the houseNo, city, state and locality properties.
+ UserInfo (firstName, lastName, technology)
|
+ UserAddress (houseNo, locality, city, state)
Just mirror the structure you outlined in your Sling Models.
Create the UserAddress model:
#Model(adaptables = Resource.class)
public class UserAddress {
#Inject
private String houseNo;
#Inject
private String locality;
#Inject
private String city;
#Inject
private String state;
//getters
}
This model can then be used in your UserInfo class:
#Model(adaptables = Resource.class)
public class UserInfo {
/*
* This assumes the hierarchy you described is
* mirrored in the content structure.
* The resource you're adapting to UserInfo
* is expected to have a child resource named
* userAddress. The #Named annotation should
* also work here if you need it for some reason.
*/
#Inject
#Optional
private UserAddress userAddress;
public UserAddress getUserAddress() {
return this.userAddress;
}
//simple properties (Strings and built-in types) omitted for brevity
}
You can tweak the behaviour with additional annotations for default values and optional fields but this is the general idea.
In general, Sling Models should be able to handle an injection of another model as long as it finds an appropriate adaptable. In this case, it's another Sling Model but I've done it with legacy classes based on adapter factories as well.

Cannot use an #IdClass attribute for a #ManyToOne relationship

I have a Gfh_i18n entity, with a composite key (#IdClass):
#Entity #IdClass(es.caib.gesma.petcom.data.entity.id.Gfh_i18n_id.class)
public class Gfh_i18n implements Serializable {
#Id #Column(length=10, nullable = false)
private String localeId = null;
#Id <-- This is the attribute causing issues
private Gfh gfh = null;
....
}
And the id class
public class Gfh_i18n_id implements Serializable {
private String localeId = null;
private Gfh gfh = null;
...
}
As this is written, this works. The issue is that I also have a Gfh class which will have a #OneToMany relationship to Gfh_i18n:
#OneToMany(mappedBy="gfh")
#MapKey(name="localeId")
private Map<String, Gfh_i18n> descriptions = null;
Using Eclipse Dali, this gives me the following error:
In attribute 'descriptions', the "mapped by" attribute 'gfh' has an invalid mapping type for this relationship.
If I just try to do, in Gfh_1i8n
#Id #ManyToOne
private Gfh gfh = null;
it solves the previous error but gives one in Gfh_i18n, stating that
The attribute matching the ID class attribute gfh does not have the correct type es.caib.gesma.petcom.data.entity.Gfh
This question is similar to mine, but I do not fully understand why I should be using #EmbeddedId (or if there is some way to use #IdClass with #ManyToOne).
I am using JPA 2.0 over Hibernate (JBoss 6.1)
Any ideas? Thanks in advance.
You are dealing with a "derived identity" (described in the JPA 2.0 spec, section 2.4.1).
You need to change your ID class so the field corresponding to the "parent" entity field in the "child" entity (in your case gfh) has a type that corresponds to either the "parent" entity's single #Id field (e.g. String) or, if the "parent" entity uses an IdClass, the IdClass (e.g. Gfh_id).
In Gfh_1i8n, you should declare gfh like this:
#Id #ManyToOne
private Gfh gfh = null;
Assuming GFH has a single #Id field of type String, your ID class should look like this:
public class Gfh_i18n_id implements Serializable {
private String localeId = null;
private String gfh = null;
...
}

JPA #OneToMany Mapping Problem

I am trying to do do JPA/Hibernate mappings to map two tables, but am getting this error. any help would be greatly appreciated!!
Restaurants.java
#Entity
#Table(name="RESTAURANTS")
public class Restaurants{
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
#OneToMany(mappedBy="restaurant")
private LinkedList<Menus> menus = new LinkedList<Menus>();
/* constructors **/
public Restaurants(){
this.dateJoined = new Date();
};
/* getters and setters **/
#Id
#GeneratedValue(generator="increment")
#GenericGenerator(name="increment", strategy = "increment")
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public LinkedList<Menus> getMenus() {return menus;}
public void setMenus(LinkedList<Menus> menus) {this.menus = menus;}
}
Menus.java
#Entity
#Table(name = "MENUS")
public class Menus {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private Long restaurantID;
#OneToMany
#JoinColumn(name="restaurant")
private Restaurants restaurant;
/* constructors */
public Menus(){}
/* getters and setters */
#Id
#GeneratedValue(generator="increment")
#GenericGenerator(name="increment", strategy = "increment")
#Column(nullable = false)
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public Long getRestaurantID() {return restaurantID;}
public void setRestaurantID(Long restaurantID) {this.restaurantID = restaurantID;}
public void setRestaurant(Restaurants restaurant) {this.restaurant = restaurant;}
public Restaurants getRestaurant() {return restaurant;}
}
With this error
Exception in thread "main" org.hibernate.MappingException: Could not
determine type for: bb.entities.Restaurants, at table: MENUS, for
columns: [org.hibernate.mapping.Column(restaurant)] at
org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306) at
org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290) at
org.hibernate.mapping.Property.isValid(Property.java:217) at
org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:464)
at org.hibernate.mapping.RootClass.validate(RootClass.java:235) at
org.hibernate.cfg.Configuration.validate(Configuration.java:1362) at
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
at bb.TestMain.setUp(TestMain.java:26) at
bb.TestMain.main(TestMain.java:59)
Thanks.
It appears to be a misconception in the use of the #OneToMany annotation. The #OneToMany annotation is used to represent the 1-side in a 1:M relationship, and the inverse #ManyToOne relationship is used to represent the M-side. Therefore, a #OneToMany annotation should be defined on a collection-type in an entity and not on a normal reference type.
You should therefore:
use a #OneToOne association if that is the nature of the relationship between the entities.
or, decide which entity represents the 1-side in the 1:M relationship. Going by the use of the LinkedList class in Restaurants, I would consider the Restaurants class to be the 1-side, and use the #OneToMany annotation in the Restaurants class, while using the inverse #ManyToOne relationship in the Menus class. The refined code would be:
Restaurants.java
...
#OneToMany(mappedBy="restaurant")
private List<Menus> menus = new LinkedList<Menus>();
Menus.java
#ManyToOne
#JoinColumn(name="restaurant")
private Restaurants restaurant;
Note the change in the declaration of the menus member variable from LinkedList<Menus> to List<Menus>. Apparently, in this case, it is wiser to declare any collection with the interface-type of the collection, instead of the concrete collection class. The rationale is that the underlying JPA provider will use it's own concrete collection types at runtime, for the purpose of proxying the collection values. Hibernate for instance, will use a PeristentList at runtime, to represent the List in a managed entity, and not a LinkedList as created by the entity. If you use the concrete type, Hibernate might fail in mapping the column, or might fail in retrieving the associated records from the database; I'm not sure about the specifics of the runtime behavior, except that I know of the eventual failure.