1

For api application development using Spring Boot and Hibernate on Java 8, need to store the bean object values(For Example bookId, bookName, bookDescription) into db. Please find the Sample classes

BeanObject.java

package com.example;

public class BeanObject {

    private String bookId;
    private String bookName;
    private String bookDescription;

    /**
     * @return the bookId
     */
    public String getBookId() {
        return bookId;
    }
    /**
     * @param bookId the bookId to set
     */
    public void setBookId(String bookId) {
        this.bookId = bookId;
    }
    /**
     * @return the bookName
     */
    public String getBookName() {
        return bookName;
    }
    /**
     * @param bookName the bookName to set
     */
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    /**
     * @return the bookDescription
     */
    public String getBookDescription() {
        return bookDescription;
    }
    /**
     * @param bookDescription the bookDescription to set
     */
    public void setBookDescription(String bookDescription) {
        this.bookDescription = bookDescription;
    }
}

DomainObject.java

/**
 * 
 */
package com.example;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

/**
 * @author pandiaraj
 *
 */
@Entity
@Table(name = "BOOKS")
public class DomainObject {

    private String bookId;
    private String bookName;
    private String bookDescription;

    /**
     * @return the bookId
     */
    @ID
    @Column(name = "ID", unique = true, nullable = false, length = 100)
    public String getBookId() {
        return bookId;
    }
    /**
     * @param bookId the bookId to set
     */
    public void setBookId(String bookId) {
        this.bookId = bookId;
    }
    /**
     * @return the bookName
     */
    @Column(name = "NAME", length = 30)
    public String getBookName() {
        return bookName;
    }
    /**
     * @param bookName the bookName to set
     */
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    /**
     * @return the bookDescription
     */
    @Column(name = "DESCRIPTION", length = 30)
    public String getBookDescription() {
        return bookDescription;
    }
    /**
     * @param bookDescription the bookDescription to set
     */
    public void setBookDescription(String bookDescription) {
        this.bookDescription = bookDescription;
    }

}

Which is simple and best way to mapping the request object values to hibernate dao object(domain model), a indentation is fastest way to execute line of code it means reduce the api response time. We followed below ways

  1. Using getter and setter methods.
  2. Using Apache Commons - BeanUtils.copyProperties(toBean, fromBean);

Is there any other ways to mapping the objects!

pandiaraj
  • 580
  • 1
  • 6
  • 18

3 Answers3

0

As I know Java has multiple mapping frameworks. I personaly have experiance with Dozer Mapping. Also you can check this article. But I suppose all this frameworks use reflection and may be slower then setter/getter aproach.

But all this frameworks can provide you better visualisation of what fields are mapped. And reduse number of Builder or Mapper classes :)

Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
0

BeanUtils.copyProperties uses reflection that is not really good for performance. You can find this in the java documentation :

Utility methods for populating JavaBeans properties via reflection.

What you can do is to use builder pattern instead of getter/setter. It will be far better in term of performance. To learn more about this, I strongly advise you to read "Effective Java" from Joshua Bloch.

Documentation :

BeanUtils : https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BeanUtils.html

Reflection performance : Java Reflection Performance

Builder pattern : http://www.informit.com/articles/article.aspx?p=1216151&seqNum=2

Community
  • 1
  • 1
Thomas Betous
  • 4,633
  • 2
  • 24
  • 45
0

Use MapStruct and JMapper as good choices for this case. JMapper have the best average working times.
All so have these,

Framework Name Average running time (in ms per operation) MapStruct 10 -5 JMapper 10 -5 Orika 0.001 ModelMapper 0.001 Dozer 0.002 check this linksenter link description here https://www.baeldung.com/java-performance-mapping-frameworks

DulajMj
  • 1
  • 1