0

Since its no longer recommended to use Spring's Hibernate template, we are using plain hibernate based DAOS.

But Spring's Hibernate template used to provide lots of convenient finders methods etc.

If we don't use Spring's Hibernate template, we lose those features.

Is there any alternatives for Spring's Hibernate template?

ajm
  • 12,863
  • 58
  • 163
  • 234
  • you wouldn´t like to use Spring + JPA + Hibernate? http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/ – bovino Marcelo Bezerra Jun 27 '14 at 05:14
  • Well, you can always drop back to plain Spring JDBC template, but that offers no persistence whatsoever. What are you looking for specifically? A persistence provider, an ORM or just something to talk to the database? – JamesENL Jun 27 '14 at 05:15
  • @Marcelo Bezerra we are using spring and Hibernate. Never used JPA though. – ajm Jun 27 '14 at 05:15
  • @James Massey We do want to use ORM i.e. Hibernate. Till now we were using spring HibernateDAOSupport and Hibernate Template. In this new project we have upgraded both spring and hibernate versions i.e. version 4 of each. – ajm Jun 27 '14 at 05:17
  • The only 2 finder methods I would call useful would be `findByExample` and `findByCriteria`. However is that really harder to use then the normal hibernate session. – M. Deinum Jun 27 '14 at 05:38

3 Answers3

2

Spring+JPA+Hibernate (JPA is a specification that Hibernate and others frameworks implements) is a great alternative for the old Hibernate template, you should give it a try

Of course, you have others options, but considering that you already know Hibernate, JPA seems to be a natural choice.

0

If you are already familiar with Hibernate, then its going to give you a leg up on trying integrating any other ORM's. I use Hibernate 4 for my projects and find it fairly flexable for 90% of common tasks. I'll give you a quick rundown of why I like Hibernate compared to using JDBC Template's or going back to the stone age and doing vanilla JDBC access.

Pro's:

  • You can use JPA annotations to map your objects right onto tables.

  • Highly configurable within the Spring application context. Can be configured by XML or Java code.

  • Hibernate transparently manages your transaction sessions, rollbacks and object persistence.

  • Great API's for database manipulation.

  • Lots of documentation available. The Hibernate documentation is of a pretty high standard, even if finding what you want can be tricky.

  • Portable, you can throw out your Oracle database and move to a MySQL database without having to change a single line of code.

Con's:

  • Tons of old tutorials (for Hibernate 3) that can be confusing.

  • Tons of old documentation (for Hibernate 3) that can get in the way trying to find solutions to problems.

  • Relatively high time cost required in learning the how to apply the Criteria API for building custom complex queries.

  • Pain to get object relationships working correctly (but once you understand how the relationships work its fairly straightforward)

Personally I think that if you are familiar with Hibernate template, you'd be silly to try and integrate another ORM. It would be possible to do, but I imagine that you already have Spring configuration for your existing system.

In an act of shameless self-promotion check out these Q&A's I wrote about configuring Hibernate 4 with Spring, and mapping relationships on objects with annotations

Community
  • 1
  • 1
JamesENL
  • 6,400
  • 6
  • 39
  • 64
0

In my project we use the combination of Spring data + Hibernate annotations. http://projects.spring.io/spring-data-jpa/

Here an example:

       @Repository
     public interface InformCandidacyRepository extends CrudRepository<InformCandidacy, Integer> {

            List<EvaluationCandidacy> findByCandidacyPublicProcurementId(Integer publicProcurementId);

            InformCandidacy findByCandidacyId(Integer id);

     }

And the hibernate mapping done by annotations

              @OneToOne
              @JoinColumn(name = "candidacy_id", nullable = false, insertable = false, updatable = false)
              @Getter
              @Setter
              private Candidacy candidacy;


              @OneToOne(orphanRemoval = true, optional = true, cascade = {CascadeType.ALL}, mappedBy = "candidacy")
              @XmlElement
              @Getter
              @Setter
              private InformCandidacy informCandidacy;
paul
  • 12,873
  • 23
  • 91
  • 153