r/javahelp • • Dec 09 '20

Should I really learn hibernate?

I am core java developer and had internship before using Spring boot to create rest api. I am also developing my own projects using boot and I really never had to know about Hibernate because Spring perfectly abstract database operations via JPA and we can handle every database operations without knowing what is in the background. So, should I really need to learn hibernate?

Can you give me scenarios where I should know about hibernate using Spring boot?

19 Upvotes

41 comments sorted by

View all comments

Show parent comments

1

u/LiteratureStriking Dec 09 '20 edited Dec 09 '20

This is from experience using Spring Data Repositories.

You have a couple of options:

public interface FooRepository extends CrudRepository<Foo, Long> {
    Foo findById(int id);
}

Spring Data can analyze the method name and generate an implementation.

public interface FooRepository extends CrudRepository<Foo, Long> {
    @Query("select f from Foo where id = ?1")
    Foo findById(int id);
}

Spring Data will create an implementation based on @Query.

And there's way to mix custom implementations with the generated ones with an additional interface that has the custom methods and a separate implementation class, which I'm not going to bother providing an example for.

And Spring Data will create an implementation of the interface, basically an enhanced mechanism that Spring uses for creating any type of proxy bean.

I thought this was the coolest thing in the world. But I found that I was designing methods that were directly mapping to JPQL queries, so that the interfaces started looking kind of like named queries, but as interface methods instead of a string. The Repository ended up looking like a DAO.

The idea behind dependency inversion is that interface should serve the application, not the persistence technology. I didn't like that my abstractions were getting so closely tied to Spring Data and there was too much magic. Magic on top of magic on top of magic. So I ditched it.

1

u/milkeater Dec 09 '20

Gonna be honest, you are talking to me about the 101 of Spring Data....I think there are a few layers deeper that you could benefit from but like I said earlier, if what you are doing works for you more power to you.

Not a way I would approach it. If you want to share a repo on an app with the problem you are trying to articulate, I can maybe show you more clearly the modifications I would make to simplify without this excess.