Reputation: 1143
I have done do get all my data and convert from object to objectDto. But, somehow, I feel like my code is not good yet. Here I need your help, I need any reference/advice to make my code better (in performance). Here is my working code:
@Override
public List<BookDto> findAll() throws Exception {
try {
List<Book> list = bookDao.findAll();
List<BookDto> listDto = new ArrayList<>();
for (Book book : list) {
BookDto bookDto = new BookDto();
Set<AuthorDto> listAuthorDto = new HashSet<AuthorDto>();
Set<Author> dataAuthor = new HashSet<Author>();
book.getAuthor().iterator().forEachRemaining(dataAuthor::add);
BeanUtils.copyProperties(book, bookDto, "author", "category");
bookDto.setCategory(book.getCategory().getCategory());
for (Author author : dataAuthor) {
AuthorDto authorDto = new AuthorDto();
BeanUtils.copyProperties(author, authorDto);
listAuthorDto.add(authorDto);
}
bookDto.setAuthor(listAuthorDto);
listDto.add(bookDto);
}
return listDto;
} catch (Exception e) {
throw new Exception(e);
}
}
and here is the output that I need (already achieved with above code):
[
{
"title": "book1",
"year": "2013",
"author": [
{
"name": "john",
"address": "NY"
},
{
"name": "angel",
"address": "LA"
}
],
"category": "science"
},
{
"title": "book2",
"year": "2014",
"author": [
{
"name": "john",
"address": "NY"
}
],
"category": "science"
},
{
"title": "book3",
"year": "2009",
"author": [
{
"name": "angel",
"address": "LA"
}
],
"category": "comedy"
}
]
Upvotes: 2
Views: 24954
Reputation: 734
First, this code:
} catch (Exception e) {
throw new Exception(e);
}
is ridiculous, I would suggest not to show this code to anyone.
Second -
List<Book> list = bookDao.findAll();
List<BookDto> listDto = new ArrayList<>();
you can allocate listDto with exact size here, since you list.size() == listDto.size(), and in every other place when you know exact size or target collection, you may have to use it. (For hash set pay attention to load factor too).
BeanUtils.copyProperties(
uses unnecessary reflection here, calling methods directly will gain performance advance.
If you want more - you can consider to replace using Hash-sets to find unique authors, there is possibly more efficient way to do this.
And as a general suggestion, you may want to use http://mapstruct.org/documentation/stable/reference/html/ to map entities to dto's to avoid writing boilerplate code.
All this question looks like a task for a code review. Isn't it?
Upvotes: 0
Reputation: 32175
Instead of re-inventing the wheel and writing this by yourself you better use an existing library/tool, which is made for this.
I would recommend to use ModelMapper which is a great library for DTO/entity mapping, you will just need one line to convert your entity to DTO, something like:
ModelMapper modelMapper = new ModelMapper();
BookDto bookDTO = modelMapper.map(book, BookDto.class);
You can check the library's Examples page or the Entity To DTO Conversion for a Spring REST API tutorial to get deeper into this library.
Upvotes: 8