Getters and Setters in Lombok when serializing - java

I have this inner class defined in a service
#RunWith(SpringRunner.class)
#SpringBootTest
public class HostelIntegrationTest {
#Data
#Builder
#NoArgsConstructor
#Getter
#Setter
#AllArgsConstructor
#JsonInclude(NON_NULL)
#EqualsAndHashCode
static class User {
String property1;
Instant property2;
Integer property3;
}
but when I serialize the class
User user = User.builder().property1("property1").property2(Instant.now()).property3(5).build();
JSONSerializer.toJSON(user))
I got this error:
net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'property1' has no getter method in class..

Related

NoArgsConstructor and AllArgsConstructor annotation on Child class for parent constructors

Hello to the overflow community, I am struggling on an inheritance problem with Lombok. I'm trying to add both annotation #AllArgsConstructor and #NoArgsConstructor on a child class in order to use the parent lombok constructors but got the error "Duplicate method Child()".
Parent class:
#ToString
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#SuperBuilder
public class Parent {
private String propertyA;
private String propertyB;
}
Child class:
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
public class Child extends Parent {
#Override
public void setPropertyA(String propertyA) {
this.propertyA(StringUtils.upperCase(propertyA));
}
}
The error message:
Duplicate method Child() in type Child Java(67109219)
Thanks to the #rentox98 reply, I understand that the ArgsConstructor on my child class would always be empty, resulting on two identical constructors.
Is there a Lombok way to generate ArgsConstructors on my child class based on the parent lombok ArgsConstructors ?
In your Child class you have no attributes, so #NoArgsConstructor and #AllArgsConstructor are the same and the error occurs.
If you wanted an all-args constructor that would pass the properties to the parent class's all-args constructor, you'd have to write it yourself; Lombok won't generate it.
#SuperBuilder
#NoArgsConstructor
public class Child extends Parent {
public Child(String propertyA, String propertyB) {
super(StringUtils.upperCase(propertyA), propertyB);
}
#Override
public void setPropertyA(String propertyA) {
this.propertyA(StringUtils.upperCase(propertyA));
}
}

Can I use a java class inside protobuf file?

I have two java classes, called MyJavaClass and MyAnotherJavaClass.
MyJavaClass :
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class MyJavaClass implements Serializable {
private Integer id;
private String name;
private MyAnotherJavaClass myAnotherJavaClass;
}
MyAnotherJavaClass:
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class MyAnotherJavaClass implements Serializable {
private Integer id;
private String someField;
}
Can I have something like this?
message MyClassMessage {
MyJavaClass javaclass = 1;
}
Instead of create a message to use protobuf, since I use the same fields.

org.hibernate.MappingException: Unknown entity - Hibernate, SpringBoot, DTO pattern

I am trying to build a simple SpringBoot and Hibernate app using DAO and DTO pattern.
I am trying to save a list of users to the database.
When I am using User class it works fine, but when I am trying to use DTO CreateUserDto class I am getting the following error:
"Unknown entity: com.app.sportapp.dto.CreateUserDto; nested exception is org.hibernate.MappingException: Unknown entity: com.app.sportapp.dto.CreateUserDto"
There is a SingleTable inheritance where Player class and Coach class inherit User class.
User.java
#NoArgsConstructor
#AllArgsConstructor
#ToString
#Getter
#Setter
#Entity(name = "Users")
#ApiModel(description = "All details about user")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "User_Type", discriminatorType= DiscriminatorType.STRING)
public class User implements Seriaalizable{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private String username;
private String email;
private String password;
private String contactNumber;
}
Player.java
#NoArgsConstructor
#AllArgsConstructor
#ToString
#Getter
#Setter
#Entity(name = "Players")
#DiscriminatorValue(value = "player")
#DiscriminatorOptions(force=true)
public class Player extends User {...}
Coach.java
#Entity(name = "Coaches")
#DiscriminatorValue(value = "coach")
#DiscriminatorOptions(force=true)
public class Coach extends User{
}
And here are DTO's:
CreateUserDto.java
public class CreateUserDto {...}
PlayerDto.java
public class PlayerDto extends CreateUserDto{...}
CoachDto.java
public class CoachDto extends CreateUserDto{
}
As I am very new to DAO and DTO pattern from error I am getting I assume that it is expected to have a model with #Entity called CreateUser so same name as DTO CreateUserDto? Or can I have the example what I did to have a User model and create a new CreateUserDto?
Thanks!
The error happens because you are treating a DTO as an entity.
Remove the JPA annotations from the DTOs and don't use those classes for connecting to the db.
You will convert the results from your queries from entities to DTO and vice-versa.
I would also suggest to have a look at Mapstruct for the creation of DTO. This will probably make it easier to separate the entities from the DTOs.

Is this good practise for Generic-Typesafety in Java?

This is the first time I tried to use generics and luckily, it works for me, but I don't know if this is really supposed to be like this (below) or if there is another - more elegant - way to solve this nestings/abstraction.
If so, could you please give me a hint / link or how to ease this without destroying the structure?
This is just a simplification of the original structure, but it is kind of similar nested in one graph. The original code is about physics / mechanics (I'm sorry about the lack of senselessness in this example).
I'm working with:
Hibernate -> Entities
Java-GraphQL -> API / DTO-Serialization
MapStruct -> Entities <-bidirectional with ignorings, etc-> DTO's
Lombok -> lombok.experimental is good enough
Spring
My abstract classes are looking like:
#Getter #Setter #AllArgsConstructor #SuperBuilder #MappedSuperclass
public abstract class City<HM extends Humanoides<LB>, AN extends Animal<LB>, LB extends Limbs> {
SortedSet<HM> humans;
SortedSet<AN> animals;
SortedSet<LB> limbs;
}
#Getter #Setter #AllArgsConstructor #SuperBuilder #MappedSuperclass
public abstract class Mammal<LB extends Limbs> {
private SortedSet<LB> extremities;
}
#Getter #Setter #AllArgsConstructor #SuperBuilder #MappedSuperclass
public abstract class Humanoides<LB extends Limbs> extends Mammal<LB> implements Comparable<Humanoides<LB>> {
private int iq;
//compareTo(Humanoides<LB> h)
}
#Getter #Setter #AllArgsConstructor #SuperBuilder #MappedSuperclass
public abstract class Animal<LB extends Limbs> extends Mamal<LB> implements Comparable<Animal<LB>> {
private int speed;
//compareTo(Animal<LB> a)
}
#Getter #Setter #AllArgsConstructor #SuperBuilder #MappedSuperclass
public abstract class Limbs implements Comparable<Limbs> {
private int length;
//compareTo(Limbs l)
}
my Entities are:
#Getter #Setter #NoArgsConstructor #SuperBuilder #IdClass(CityStuff.class)
public class CityEntity extends City<HumanoideEntity, AnimalEntity, LimbsEntity> {}
#Getter #Setter #NoArgsConstructor #SuperBuilder #IdClass(HumanoideStuff.class)
public class HumanoideEntity extends Humanoide<LimbsEntity> {}
#Getter #Setter #NoArgsConstructor #SuperBuilder #IdClass(AnimalStuff.class)
public class AnimalEntity extends Animal<LimbsEntity> {}
#Getter #Setter #NoArgsConstructor #SuperBuilder #IdClass(LimbsStuff.class)
public class LimbsEntity extends Limbs{}
and my DTO's are:
#Getter #Setter #NoArgsConstructor #SuperBuilder
public class CityDto extends City<HumanoideEntity, AnimalEntity, LimbsEntity> {
//manipulate()
}
#Getter #Setter #NoArgsConstructor #SuperBuilder
public class HumanoideDto extends Humanoide<LimbsEntity> {
//manipulate()
}
#Getter #Setter #NoArgsConstructor #SuperBuilder
public class AnimalDto extends Animal<LimbsEntity> {
//manipulate()
}
#Getter #Setter #NoArgsConstructor #SuperBuilder
public class LimbsDto extends Limbs {
//manipulate()
}

Why #Data and #Builder doesnt work together

I have this simple class
public class ErrorDetails {
private String param = null;
private String moreInfo = null;
private String reason = null;
...
}
After refactoring, I added #Data and #Builder, but all the instantiations doesn't work any more
ErrorDetails errorDetails = new ErrorDetails();
'ErrorDetails(java.lang.String, java.lang.String, java.lang.String)'
is not public in
'com.nordea.openbanking.payments.common.ndf.client.model.error.ErrorDetails'.
Cannot be accessed from outside package
If I removed #Builder, then it will work fine,
Why I cannot use #Data and #Builder together?
Lombok's #Builder must have #AllArgsConstructor in order to work
Adding also #AllArgsConstructor should do
Under the hood it build all fields using constructor with all fields
applying #Builder to a class is as if you added #AllArgsConstructor(access = AccessLevel.PACKAGE) to the class and applied the #Builder annotation to this all-args-constructor. This only works if you haven't written any explicit constructors yourself.
The full config should be :
#Data
#Builder(toBuilder = true)
#AllArgsConstructor
#NoArgsConstructor
class ErrorDetails {
private String param; // no need to initiate with null
private String moreInfo;
private String reason;
}

Categories