Bhuvan
Bhuvan

Reputation: 4187

How hibernate change classes or object at run time

I was trying to understand how hibernate work in the sense that how putting @Entity on a class makes it a persistent class ? i.e


    @Entity
    class A{
        private int b;

        public int getB(){
        return b; 
        }

        public void setB(int b){ 
        this.b = b;
        }
    }

behaves like below written class at runtime

    class A{
        private int b;

        public int getB(){ 
        return (SQL code to fetch b from DB)
        }

        public void setB(int b){
        (SQL code to set b in DB)(b);
        }
    }

If we say it is using reflection then how it is changing the code that is inside the methods ?

Upvotes: 1

Views: 361

Answers (1)

gerrytan
gerrytan

Reputation: 41143

Hibernate proxied / runtime-weave your class. Meaning when other classes invoke methods of your class, it doesn't invoke it directly, but it invokes the proxy. This proxy then contains logic that involves persistence context operations.

Have a look at library such as cglib or aspectj if you want to delve deeper in this topic (not necessarily the ones used by hibernate)

Upvotes: 1

Related Questions