yaroslavTir
yaroslavTir

Reputation: 721

optimization hibernate

I have entity that use storage function. It's good, but some case I don't need in this fields (fieldStoreFunc1, fieldStoreFunc2..) and use Class B and fieldStoreFuncs only when it's necessary:

@Entity
@Table(name = "table")
@SequenceGenerator(name = "JDE_SEQUENCE", sequenceName = "JDE_SEQUENCE", allocationSize = 1)
public class EntityClass implements Trackable {

    @Id
    @GeneratedValue(generator = "JDE_SEQUENCE", strategy = GenerationType.SEQUENCE)
    private Long id;

    @Column(name = "name_field")
    private String field1;


    @Column(name = "name_field")
    private String field2;

    @Column(name = "name_field")
    private String field3;
    ......

    //read only fields
    @Formula("store_func(value)")
    private String fieldStoreFunc1;

    @Formula("store_func(value)")
    private String fieldStoreFunc2;

    @Formula("store_func(value)")
    private String fieldStoreFunc3;
}

so can I divide class into to

class A{
    @Column(name = "name_field")
        private String field1;


        @Column(name = "name_field")
        private String field2;

        .....
}

class B extends A{
        //read only fields
        @Formula("store_func(value)")
        private String fieldStoreFunc1;

        @Formula("store_func(value)")
        private String fieldStoreFunc2;

        @Formula("store_func(value)")
        private String fieldStoreFunc3;
}

Upvotes: 2

Views: 437

Answers (2)

ssedano
ssedano

Reputation: 8432

Although I agree with JB Nizet if you are still willing to load those properties on demand. And just for the record.

You can achieve in a few ways. The more simplistic one is to create a constructor in your class with the property you always want to load

Please Note that is just an example and is not properly developed

public class A {
  ...
  @Column(name = "name_field")
  private String field1;

  @Formula("store_func(value)")
  private String fieldStoreFunc1;

  ...
  public A(String id, String field1) {
     this id = id; 
     this.field1 = field1;
  }

And then in the DAO create a function that loads entities like

public class DAO {
    getLight(Long id) {
       String named_query = "select a.id, a.field1 from A a where a.id = :id"
       // Execute query
    }

Another way is to take a look at the Hibernate event or write a custom entity loader.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

No, that's not how you should do.

First, measure, profile, and prove that loading these additional three fields causes a significant overhead and performance problem.

Once, and only once you have this proof, then consider lazy-loading the additional three fields as explained in the documentation. Then measure once again, and prove that the lazy-loading of these three fields does no cause an even bigger overhead and performance problem.

As stated in the documentation:

Hibernate3 supports the lazy fetching of individual properties. This optimization technique is also known as fetch groups. Please note that this is mostly a marketing feature; optimizing row reads is much more important than optimization of column reads. However, only loading some properties of a class could be useful in extreme cases. For example, when legacy tables have hundreds of columns and the data model cannot be improved.

Unless the store_func formula is very expensive, you probably should avoid trying to optimize anything.

Upvotes: 2

Related Questions