Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

Richard Chesterwood

Virtual Pair Programmers Rep
+ Follow
since May 28, 2014
Richard likes ...
Hibernate Spring Java
Cows and Likes
Cows
Total received
7
In last 30 days
0
Total given
0
Likes
Total received
5
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Richard Chesterwood

Well done everyone! I hope the course proves to be useful - keep in touch.

Richard.

Carlos Borrero wrote:Hi Richard,

What do you envision for Hibernate in a near future compared to other options in the Market and what is the best advised you can give for a beginner before jumping into Hibernate,

Thank you Richard,



Hi Carlos, to be honest I've stopped trying to predict the future because I'm always wrong!

To be a beginner, I would recommend:

1) favour JPA over the classic Hibernate API, although I would still learn both. Try not to get to wound up over the difference, it was largely a political thing and we just have to deal with the differences.

2) Don't think that JPA is a "keep SQL simple" tool, you need to invest just as much time in learning efficient SQL. Or have a friendly DBA type on hand.

3) The other options in the market right now are of course the "NoSQL" databases, so if you can also invest some time in learning some of the leading NoSQL databases like Neo4j, Mongo etc. Some predictions are for a polyglot database world, so the programmer of the future may need to be fluent in them all.

That's just my opinion - good luck!
Richard.
Hi Paul,

Yes we explore both 1-Many, Many-1 and in particular the problems in choosing between uni-directional and bi-directional mappings (which I think addresses your question about whether to use 1-many or many-1). I believe bi-directional relationships are the most common cause of confusion in Hibernate/JPA so we devote plenty of time to it.

We talk a lot about performance and efficiency, the final third of the course is pretty deep, with material that basic courses wouldn't cover, such as Criteria API, Locking, Lazy Initialisation and Second Level Cache.

I hope that helps, good luck in the competition!

Richard.
Hi,

We'll need to see your entity mappings - either your annotations or your .hbm.xml.

Also, your full entity classes.

Are you using bi-directional or uni-directional for your association?

Thanks,
Richard.
Sounds like SpringMVC? If so it sounds like you should concentrate on using RequestParam for now:

eg



RequestBody is used for handling the entire request body, for example if you want to handle a PUT request sending something more complex than a parameter. An example of its use here: http://www.beabetterdeveloper.com/2013/07/spring-mvc-requestbody-and-responsebody.html
6 years ago

Chris Nowakowski wrote:Hi geezers,
I have some experience with java and I want to learn spring and hibernate, so I wonder if this course is a good place to start ? btw. I know some sql too.



Hi Chris,

Yes this is an excellent place to start! The only question is whether to start with Spring or Hibernate? It's a bit of a chicken/egg question, you can really start with either. We have a well regarded Spring Fundamentals course as well - this covers a bit of Hibernate but the focus here on how it integrates with Spring.

I hope that helps - good luck with the training!

Richard.
Hi Pawel,

You sound perfect for the course! You can start with absolutely zero Hibernate experience. Java is the only requirement - we even do an overview of SQL (but you won't be needing that by the sounds of it!)

Cheers,
Richard.
Can you show us your full entity code, Device and Dummy? The full XML would be useful also.

Pay particular attention to your management of whichever attribute you're using for an ID. It's easy for Hibernate to think you mean a different row if the ID isn't set correctly.
The queries are cached in the second level cache so yes you need to configure a full 2LC before you switch on Query caching.

Off topic, but be careful using a query cache: http://tech.puredanger.com/2009/07/10/hibernate-query-cache/
I see, thanks for the additional information. It does sound from what you've said that the design has gone astray, a class with 200 attributes is something of a red flag! Plus the null columns.

> In one context you need C1, C9, C22
> In another context you need C2, C5, C9, C29, C52

This is a clue, it suggests that you need one class mapped to C1, C9, C22, and a second class mapped to C2, C5 ,C29, C52. Then (I suspect) your queries will end up much simpler. The presence of null columns suggests some inheritance might be helpful also.

However, it's the real world and if your first cut solution is working fine, it might just be a case of accepting the pain of writing the large collection of queries?

Yes it is indeed a common source of confusion. Jaikiran's links should help, but briefly:

JPA is just a specification, effectively a collection of Java interfaces that specify that, for example, to call JPA you will use an interface called "Entity", with a fixed collection of methods.

Anybody is free to implement JPA, and they can then compete with the other implementations. Perhaps one implementation of JPA is fast, perhaps another has better documentation.

The big implementations are Hibernate (although it has a non standard "classic" mode which pre dates JPA and many projects are still using), EclipseLink (not related to the IDE, it used to be called TopLink from Oracle) and OpenJPA from Apache.

So JPA will provide you will the interfaces, but the concrete implementations will be a set of JAR files on your classpath.

Cheers
Richard.
Hi,

A good deep question. I've never come across a situation where I've needed to use an embeddable as a key on map.

I'm not sure it's about completeness, there are still some important things missing from JPA (example at my blog here). Sometimes committee standards have eccentric elements that can often be explained away by "design by committee". Perhaps someone on the committee decided embeddable map keys were of critical importance and they just had a louder voice than everyone else?

If anyone at JavaRanch can give a good example of embeddable map key I'd be very interested. We don't cover them on the course by the way - honestly, I'd almost forgotten they existed!

Cheers,
Richard.
Hi Jana,

Hibernate keeps an "old" copy in the session and when the transaction commits, it compares the state of the two. If the object has changed then an update statement is issued.

If someone (another transaction) has changed the database at the same time, then you need to study at least how optimistic locking works to understand what is going to happen.

Assuming that your database has a loose level of transaction isolation (read committed or repeatable read), THEN by default, the last transaction to commit will simply overwrite the previous transaction.

If you switch versions on (this is where you need to study optimistic locking), then the second transaction to commit will throw an exception which can be caught by the programmer and handled "however" - eg abort the transaction with a warning to the user (sorry, someone got there first) or you can restart a new transaction and go again.

Usually proper locking (pessimistic locking) isn't used because of the deadlocking/liveness/performance problems it causes but it is worth studying this once you've understood what happens in the optimistic case.

Richard.
Hi Joe,

Hope I can help. The general idea is that you write code all the way through the video. We don't want it to be a passive experience, which is why we call ourselves Virtual Pair Programmers! A few of the chapters are "fly solo" chapters where we just give you a few clues and then let you do the work (but we do give a walkthrough).

So in short, it's not traditional lab style exercises, but more applied to the job at hand. Be sure that at the end of the course, you'll have running code on your computer that you will have written yourself (we do give you some of the basic files such as the boring XML config!)

Because of the nature of Hibernate, we don't build a big application, we sort of focus on a small object model and see how Hibernate works in detail, the idea being that you can then scale up from there. The final few chapters (how to integrate with Spring, EJB and Servlet based web applications) are a bit "bigger" but these are less practical - I give you a tour of a prebuilt architecture in that one.

I hope that helps, do ask if you need to know more!

Richard.

Will Myers wrote:
I think it's a common misunderstanding when someone is thrown onto a project that uses Hibernate that you must use it everywhere, the view seems to be either use Hibernate for all database interaction or use JDBC but never mix.



Spot on! I think in the early days Hibernate was sold as a "hide the SQL" tool, but it isn't that at all. If anything, with Hbernate your understanding of SQL needs to increase - although the routine statements are generated, you need to keep on top of what is happening. We do a lot of "log inspection" on the course!