MapStruct exclude LOT of fields from mapper - spring-boot

Suppose I have a complex entity like the follow:
class A {
private String a;
private String b;
private String c;
/**
* so on */
private B bb;
private C cc;
}
and the respective DTO:
class ADTO {
private String a;
private String b;
private String c;
/**
* so on */
private BDTO bb;
private CDTO cc;
}
Now suppose C entity (and CDTO) has many variables and I want map is with just his ID field. For example
#Mapper(componentModel = "spring")
public Interface AMapper {
BDTO bToDto(B b);
B bFromDto(BDTO bDto);
CDTO cToDto(C c); // for this I want to map just the id!!
C cFromDto(CDTO cDto); // even for this
}
How can I do?? I wouldn't write 50 times #Mapper(properties = "someField", ignore = true), is there another method??
Thank you

You can create a separate mapper for C class and add in AMapper class using uses field of #Mapper
#Mapper(componentModel = "spring", uses={"CMapper.class"})
public Interface AMapper {
BDTO bToDto(B b);
B bFromDto(BDTO bDto);
// remove below
// CDTO cToDto(C c);
// C cFromDto(CDTO cDto);
}
public CMapper{
public C fromDTO(CDTO) {
// add mapping for ID only
}
public CDTO toDTO(C) {
// add mapping for ID only
}
}

Related

Retrive a object of parent class into child class in spring boot application

I am building REST API using spring boot application. I have connected application to Mongodb database. I have created a database named "Employee" and collection as "Employee" itself. Now i want to create a document. I have three class. Class A, Class B and class C.
Class A is the parent Class having property (id,name,password). Class B is child class and extends Class A with property(address,phoneNumber) and class C is child class which also extends class A with property (fatherName,MotherName).
Now i want to add the data to database as object of B or object of C and also want to retrive the data from database as object of B or Object of C.
here is code of Class A:
package com.example.webproject;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection="Employee")
public class A {
#Id
private String id;
private String passwd;
private String username;
public String getId() {
return id;
}
public void setIp(String string) {
this.ip = string;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
class B:
package com.example.webproject;
public class B extends A {
private String address;
private String phoneNumber;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber= phoneNumber;
}
}
Class C :
package com.example.webproject;
public class C extends A {
private String fatherName;
private String motherName;
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public String getMotherName() {
return motherName;
}
public void setMotherName(String motherName) {
this.motherName = motherName;
}
}
EmployeeRepository.java
package com.example.webproject;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface EmployeeRepository extends MongoRepository<A,String> {}
EmployeeController.java
#RestController
public class EmployeeController {
#Autowired
private EmployeeRepository repo;
#PostMapping("/addByB")
public String addDataByB(#RequestBody B res) {
repo.save(res);
return "added";
}
#PostMapping("/addByC")
public String addDataByC(#RequestBody C res) {
repo.save(res);
return "added";
}
#GetMapping("/getByB")
public List<B> getDataByB(){
List<B> b= repo.findAll(); #Here it throws error because repo.findAll return object of A.
return b;
}
When i try to add data as B object or C object using swagger , the data is getting stored in database. Now i want to retrieve the data as B object or C object, how to achieve this?
Because you just create Repository of class A and call it, you nedd to creat two another repo of class B and C then call them like you call " EmployeeRepository " so you can use them and get the data.

Why might Room database IDs all show as 0 in a log/Toast message?

I'm trying to get the id of the last inserted object into a database using Room with Android. I can fetch the last object using an SQL query and can call other methods to get the various properties of that object which the user has set when saving the object. But getId() always returns 0. When I examine the table contents in Android Studio's app inspector, I can clearly see that Room is generating a unique primary key for each row, but I just can't get at it. Can anyone suggest what the problem might be?
Here's the Dao query:
#Query("SELECT * FROM gamebooks_table WHERE gamebookId=gamebookId ORDER BY gamebookId DESC LIMIT 1")
LiveData<Gamebook> getSingleGamebookByID();
And here's the annotated entity class:
#Entity(tableName = "gamebooks_table")
public class Gamebook {
#PrimaryKey(autoGenerate = true)
private long gamebookId;
private String gamebookName;
private String gamebookComment;
private String gamebookPublisher;
private float gamebookStarRating;
public Gamebook(String gamebookName, String gamebookComment, String gamebookPublisher, float gamebookStarRating) {
this.gamebookName = gamebookName;
this.gamebookComment = gamebookComment;
this.gamebookPublisher = gamebookPublisher;
this.gamebookStarRating = gamebookStarRating;
}
public long getGamebookId() {
return gamebookId;
}
public String getGamebookName() {
return gamebookName;
}
public String getGamebookComment() {
return gamebookComment;
}
public String getGamebookPublisher() {
return gamebookPublisher;
}
public float getGamebookStarRating(){
return gamebookStarRating;
}
public void setGamebookId(long gamebookId) {
this.gamebookId = gamebookId;
}
}
SOLVED
Finally sorted this by adding an Observer to my DAO method which returns a single gamebook. Within the Observer's onChanged() method, I can loop through all Gamebooks in the LiveData List (even though there's only one because I'm limiting it to one in the SQL query) and call getId() to get their respective IDs.
mainViewModel.getSingleGamebook().observe(this, new Observer<List<Gamebook>>() {
#Override
public void onChanged(List<Gamebook> gamebooks) {
int i=0;
for(Gamebook gamebook : gamebooks){
gamebookId= gamebook.getGamebookId();
Log.d(TAG, "Gamebook Name: "+gamebook.getGamebookName()+ " Database ID: " +gamebookId);
i++;
}
}
});
I believe that your issue is due to the only constructor being available not setting the id so the LiveData uses the default value of 0 for a long.
I'd suggest having a default constructor and thus all setters/getters and (optionally) using #Ignore annotation for one of the constructors..
without #Ignore you get warnings Gamebook.java:8: warning: There are multiple good constructors and Room will pick the no-arg constructor. You can use the #Ignore annotation to eliminate unwanted constructors. public class Gamebook {
e.g. :-
#Entity(tableName = "gamebooks_table")
public class Gamebook {
#PrimaryKey(autoGenerate = true)
private long gamebookId;
private String gamebookName;
private String gamebookComment;
private String gamebookPublisher;
private float gamebookStarRating;
public Gamebook(){} /*<<<<< ADDED */
#Ignore /*<<<<< ADDED - is not required - could be on the default constructor but not both*/
public Gamebook(String gamebookName, String gamebookComment, String gamebookPublisher, float gamebookStarRating) {
this.gamebookName = gamebookName;
this.gamebookComment = gamebookComment;
this.gamebookPublisher = gamebookPublisher;
this.gamebookStarRating = gamebookStarRating;
}
public long getGamebookId() {
return gamebookId;
}
public String getGamebookName() {
return gamebookName;
}
public String getGamebookComment() {
return gamebookComment;
}
public String getGamebookPublisher() {
return gamebookPublisher;
}
public float getGamebookStarRating(){
return gamebookStarRating;
}
public void setGamebookId(long gamebookId) {
this.gamebookId = gamebookId;
}
/* ADDED setters */
public void setGamebookName(String gamebookName) {
this.gamebookName = gamebookName;
}
public void setGamebookComment(String gamebookComment) {
this.gamebookComment = gamebookComment;
}
public void setGamebookPublisher(String gamebookPublisher) {
this.gamebookPublisher = gamebookPublisher;
}
public void setGamebookStarRating(float gamebookStarRating) {
this.gamebookStarRating = gamebookStarRating;
}
}
You also probably want to be able to pass the respective id to the getSingleGamebookByID, so you may wish to change this to:-
#Query("SELECT * FROM gamebooks_table WHERE gamebookId=:gamebookId /*<<<<< ADDED to use id passed */ ORDER BY gamebookId DESC LIMIT 1")
LiveData<Gamebook> getSingleGamebookByID(long gamebookId /*<<<<< ADDED to use id passed */);
you would probably want to remove the comments.
Note the LiveData aspect has not been tested and is conjecture.
Example
This example shows that room is fine with your original code but that the issues is on the LiveData/Viewmodel side :-
public class MainActivity extends AppCompatActivity {
TheDatabase db;
GamebookDao dao;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Note The Database has .allowMainThreadQueries */
db = TheDatabase.getInstance(this);
dao = db.getGamebookDao();
long gb1id = dao.insert(new Gamebook("Gamebook1","blah","Gamebook1 Publisher", 10.1F));
long gb2id = dao.insert(new Gamebook("Gamebook2","blah","Gamebook2 Publisher", 6.1F));
long gb3id = dao.insert(new Gamebook("Gamebook3","blah","Gamebook3 Publisher", 10.1F));
logGameBook(dao.getSingleGamebookByID());
logGameBook(dao.getSingleGamebookByID());
logGameBook(dao.getSingleGamebookByID());
/* Alternative that allows the ID to be specified */
logGameBook(dao.getSingleGamebookByIDAlternative(gb1id));
logGameBook(dao.getSingleGamebookByIDAlternative(gb2id));
logGameBook(dao.getSingleGamebookByIDAlternative(gb3id));
}
void logGameBook(Gamebook gb) {
Log.d("GAMEBOOKINFO","Gamebook is " + gb.getGamebookName() + " id is " + gb.getGamebookId());
}
}
The above uses your original code, the TheDatabase is a basic #Database annotated class BUT with .allowMainThreadQueries so it is run on the main thread.
The log, after running, includes:-
2022-03-12 08:16:12.556 D/GAMEBOOKINFO: Gamebook is Gamebook3 id is 3
2022-03-12 08:16:12.558 I/chatty: uid=10132(a.a.so71429144javaroomidreturnedaszero) identical 1 line
2022-03-12 08:16:12.561 D/GAMEBOOKINFO: Gamebook is Gamebook3 id is 3
2022-03-12 08:16:12.568 D/GAMEBOOKINFO: Gamebook is Gamebook1 id is 1
2022-03-12 08:16:12.572 D/GAMEBOOKINFO: Gamebook is Gamebook2 id is 2
2022-03-12 08:16:12.574 D/GAMEBOOKINFO: Gamebook is Gamebook3 id is 3
Note how the first just returns the same object and thus id.

MapStruct: mapping from object with a list of complex object

Supposing I have the following classes:
public class A {
private String id;
private List<B> related;
}
public class B {
private String id;
private String name;
}
public class ADTO {
private String id;
private List<BDTO> relations;
}
public class BDTO {
private String identificator;
private String relatedName;
}
How can I create a mapper that given an A object type returns me an ADTO object with all the information? I have to create two different mappers? Can it be done in only one mapper? I think it would be something like the following, but I don't know how to map the atributtes from the list:
#Mapper
public interface MyMapper {
#Mappings({ #Mapping(source = "related", target = "relations") })
ADTO mapperA(A obj);
}
Thanks in advance.
try this (not tested but should work properly)
when you mapping lists you should make a map for both the class element and the list to map all the elements of the list)
#Mapper
public interface MyMapper {
#Mappings({ #Mapping(source = "related", target = "relations") })
ADTO mapperA(A obj);
#Mappings(
{ #Mapping(source = "id", target = "identificator") },
{ #Mapping(source = "name", target = "relatedName") })
BDTO bDTOMapping(B b);
List<BDTO> bDTOListMapping(List<B> bList);
}

how do I solve the problem of bind enum in spring controller

when I use common param to do some common logic by using spring. I found I can`t use enum for input. like postman or other.
To solve this problem,I try lots of ways. finally,thanks god.I success.
This is only for RequestMethod.POST.And your param must be object (maybe RequestMethod.GET or single param also available.but I haven`t found how to do that )
example
#Data
#Builder
#AllArgsConstructor
#RequiredArgsConstructor
public class CommonParam implements Serializable {
/**
*
*/
#Size(min = 1, max = CommonConstants.MAX_PARTITION_SIZE)
private List<String> texts;
/**
*
*/
#NotNull
private KeyTypeEnum keyTypeEnum;
}
#PostMapping("/do")
public RpcResult do(#RequestBody #Valid CommonParam commonParam) {
.....
}
the last but not the least
public enum KeyTypeEnum {
/**
* 手机号
*/
PHONE(1, "phone");
private int value;
private String desc;
KeyTypeEnum(int value, String desc) {
this.value = value;
this.desc = desc;
}
public static KeyTypeEnum getByDesc(String desc) {
for (KeyTypeEnum b : KeyTypeEnum.values()) {
if (b.getDesc()
.equals(desc)) {
return b;
}
}
return null;
}
#JsonCreator
public static KeyTypeEnum getByValue(int value) {
for (KeyTypeEnum b : KeyTypeEnum.values()) {
if (Objects.equals(b.getValue(), value)) {
return b;
}
}
return null;
}
#JsonValue
public int getValue() {
return value;
}
public String getDesc() {
return desc;
}
}
By Using #JsonCreator we can ensure the input like '1'(this code is the KeyTypeEnum`s already defined value) can be success convert to enum.
#JsonValue ensure '1' can be success get for spring.
The value of KeyTypeEnum should be PHONE in the JSON payload. It's not feasible to uniquely resolve by the value 1 as you are expecting. You can have multiple enum with the same value.

Group validation

Is it possible to validate a bean ensuring that at least one of three fields is not null without implementing a custom validator?
So:
public class Foo {
#NotNull(groups = {AtLeastOne.class})
private Bar b1;
#NotNull(groups = {AtLeastOne.class})
private Bar b2;
#NotNull(groups = {AtLeastOne.class})
private Bar b3;
}
But without the groups meaning that I want to validate them all in one go. I want either b1 or b2 or b3 to be not null.
Cheers,
You need annotation #Validated. example:
public class Foo {
#NotNull(groups = {AtLeastOne.class})
private Bar b1;
#NotNull(groups = {AtLeastTwo.class})
private Bar b2;
#NotNull(groups = {AtLeastThree.class})
private Bar b3;
}
#Validated(value=AtLeastOne.class) will validate only b1
#Validated(value=AtLeastTwo.class) will validate only b2
UPDATE
#NotAllNull(value={"b1", "b2", "b3"})
public class Foo {
private Bar b1;
private Bar b2;
private Bar b3;
}
#Documented
#Constraint(validatedBy = NotAllNullValidator.class)
#Target( { ElementType.TYPE })
#Retention(RetentionPolicy.RUNTIME)
public #interface NotAllNull {
String[] value;
}
public class NotAllNullValidator implements ConstraintValidator<NotAllNull, Object> {
private String[] fields;
#Override
public void initialize(final NotAllNull constraintAnnotation) {
fields = constraintAnnotation.value();
}
#Override
public boolean isValid(final Object instance, final ConstraintValidatorContext context) {
boolean result = false;
for(int i = 0 ; i < fields.length; i++) {
result |= org.apache.commons.beanutils.BeanUtils.getProperty(instance, fields[i])!=null;
}
return result;
}
}
I don't have IDE here, there may some error in the code, but hope you can see the idea behind code
take a look at #Valid and #Validated annotations on you class

Resources