aspose file tools*
The moose likes Object Relational Mapping and the fly likes how can hibernate update be done using Load when load method takes from cache and not actual value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how can hibernate update be done using Load when load method takes from cache and not actual value." Watch "how can hibernate update be done using Load when load method takes from cache and not actual value." New topic
Author

how can hibernate update be done using Load when load method takes from cache and not actual value.

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Load method takes from cache and does not work with actual value (unlike get method).In such a case how can load method be used for update purpose when you are not dealing with actual value and only from cache?

thanks.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

Method load does not do update!
However if you loaded object and then changed its persistent values then hibernate detects these changes and flushes them to database when transaction is committed!
You must read chapter 10! It explains load as well as all object states!


True person is moral, false is right!
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Ok thanks but one is able to delete using the below code:




Why is this possible when load method does not work with actual copy only from cache.?

Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

Method delete can delete persistent,detached and transient entity!

If entity was made persistent by load() method then delete(loadedEntity) method goes to db to check if this
proxy has corresponding row in database or not. If it is present then it is deleted!
This is worst case because to do delete you need two queries.
If you know identifier of entity to delete then it is quicker to create new entity assign id and delete it:

But if you need entity for more than just delete then you can use get() method that always returns initialized instance.

Sometimes you will have to delete detached entity:

a)if detached entity identifier is not present in database then you will get excpetion:


b)if detached entity identifier was set to null then hibernate assumes that this is transient instance and does
delete like for transient. In this case no exception is thrown and no changes to database are done(this is the case for me!).

The last option is to delete transient entity :

a)if entity is transient then hibernate assumes that identifier value of your entity is present in database in which case delete is OK.
But if it is not present you will get exception on transaction commit :

b)if transient entity has identifier null then nothing will happen as described above

Finally, this is extract from HibernateTemplate doc :
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Thanks.

Using the code below one is able to delete using the load method.




What I am not understanding is if Load reads only from cache then how is one able to delete using it?


Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

Eh, Monica... My quotation:
If entity was made persistent by load() method then delete(loadedEntity) method goes to db to check if this
proxy has corresponding row in database or not. If it is present then it is deleted!

So if proxy is not initialized from database hibernate's delete method goes to db to initialise it and if initialization is successful (meaning proxy is present in db) hibernate deletes row.
But this is good when you need entity for more than just delete. If you ONLY need to delete you may use transient instance as decribed before.

FInally do the following steps:
1. In your Maven project open pom.xml and paste
Put log4j.properties into src/main/java folder.
Next create log4j.properties file and paste there :

This will allow you to see almost all information generated by hibernate including all queries and their database values.

Put cursor on line where you call hibernate delete in java class and press CTRL+SHIFT+B. This will setup breakpoint at which execution will suspend.
Next if you use main method to start your application you need to start it by ALT+SHIFT+D and in popup window select 'Debug as Java application'.
When eclipse suspends you need to press F5 to go into delete method or F6 to step over it.
Do this way and you will see what is happenning in hibernate delete method and when queries are generated and what all hibernate logs mean.
Of course you need to download source code of hibernate jar. Source code is in jar named hibernate-core-4.2.2.Final-sources.jar. Search this jar in google or change only version(4.2.2) if you use another !

So browse source code of hibernate and you will know hibernate better!!!
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Thanks.

For my application delete functionality, should I go ahead with this kind of delete:
a)


Or
b)
Should I do delete using HQL as "delete from Course where ...

Which one should be done a) or b)?
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

I covered my reasons what to use and when above but you even don't read carefully. Do you know Java Core?
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Thanks. I have understood now and yes I know core java.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

I want to share my own experience. I have been recently invited to take the interview for vacancy Junior Java developer.
It justs turned out that interviewer was primarily interested in my understanding of Java Core rather than Hibernate,Spring.

Sincerely every interview I have ever taken were not about Hibernate but interface,abstract class,static,final. And believe me there is alot more to say about this.
Junior Java Developer is primarily required to know Java Core, multithreading,SQL queries,JDBC, Java Servlets but not Hibernate,Spring,Struts at all.
You may just for fun find a few vacancies for juniors on your site.
I shared my a little bitter experience.
If you really sure you don't need to know what is static,final it is OK. Just for fun try to answer not what is interface, but why do I need interface?

Be watchful and try be sure what you do is what you need !
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Now I am preparing series about hot topic for any Junior Java developer - Collections.
At your leasure it might be quite useful to cover my Internal Life of LinkedList in Java tutorial.
It is already finished !
Also I am going to investigate other popular types of Collections.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Thanks.


It justs turned out that interviewer was primarily interested in my understanding of Java Core rather than Hibernate,Spring.
Sincerely every interview I have ever taken were not about Hibernate but interface,abstract class,static,final. And believe me there is alot more to say about this.
Junior Java Developer is primarily required to know Java Core, multithreading,SQL queries,JDBC, Java Servlets but not Hibernate,Spring,Struts at all.


Good that you shared your experience. In my experience and view a good interviewer will judge you on anything be it core java,advanced java.Not just for juniors for seniors too core java too is asked. It is in core java where you can ask twisting questions more.Yes most questions may be about interface,abstract class,static,final or anything. It the interviewver is skilled he will judge you by asking anything.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
If you really sure you don't need to know what is static,final it is OK. Just for fun try to answer not what is interface, but why do I need interface?


It can never be OK to not know these things. I have worked on these things, answered these things in many interviews and asked these in many interviews.

We need an interface to define a contract for classes to implement (which might not be related to each other). E.g for DAO We can define a DAO Interface with method declarations for read,delete,update,insert and DAOImpl would implement these. I have done this in projects.
thanks
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

In my experience interviewer will judge Junior Java developer on Java Core !

Do you believe someone let you do hibernate without knowing SQL or Struts without understanding Java Servlets, HTTP ?

Don't you want knowing hibernate and spring become immediately senior java developer ...

At least try to visit one interview and ask what you really need to know to be junior and what to be senior and what to be lead !

Never mind! You will be there !

Finally if you feel like improving on java collections here is my post on LinkedList - hot topic for juniors!
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Yes the post is good.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Which one is good?

It seems to me that my previous reply is good!

I give 90% that you won't be asked on hibernate and spring with such big understanding of Java Core and Java Servlets and SQL.

It is up to you to sit days long in vain trying to play with rifle without bullets or be sure what you do is what you need!

Don't try to fit in with the Internet.

Do code mainly and less theory.

This is just what my bitter experience says!

Good luck!
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I meant this link of yours is good:

http://volodial.blogspot.in/2013/05/internal-life-of-linkedlist-in-java.html
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello Monica!

If you like them you may share it via facebook or google blog.

Keep an eye on my future tutorials I'll end today with ArrayList and procede to mysterious HashMap.

These tutorials will definitely help you to pass interview for Junior Java developer
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
These tutorials will definitely help you to pass interview for Junior Java developer


I do not want to create any misunderstanding here. I do not know why you thought I do not have a job when I never said so. I do have job. But your posts are knowledable and indepth.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

I am glad to hear you have a job.

I am mainly talking to those like me newbies who try studying Hibernate and Spring to grasp java core or java servlets along the way.

Junior Java developer is required to be very well knowledable in Java Core!

The way to start is from console (not serious) programs and move further to smth like Java Swing to realise what statis,final,abstract classes are!

Again I started with JavaEE more than year ago. So I must know smth of JPA,Hibernate...

To be Junior everybody needs to code on Java Core a lot...
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Yeah I have job and I know core Java but yes like you I too want to know things deeply. I know concepts like interface,abstract class,static,final which you mentioned above. If you want we can have discussions on the mutual doubts if any.

Thanks a lot.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Great that you want to know things deeply !

At your leasure pass my tutorials on Java Collections framework.

Now I am moving to Java Core thus Hibernate,Spring is not what I can assist you much. But if I can I reply to your topic.

Can you share about your way to learn Java Core deeply here
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I can think of below few ways for this:

1) Solve SCJP sample questions and analyze each question and concepts related to it.
2) Read browse "java in general" forum and try to answer questions.
3) Keep Playing code raunch Round game available at home page at spare time.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10222
    
166

Volodymyr Levytskyi,
Your post was moved to a new topic.


[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can hibernate update be done using Load when load method takes from cache and not actual value.