1

I want to gradually transition a codebase to Hibernate (current code uses Spring’s NamedParameterJdbcTemplate).

1) Is it safe to start creating @Entity Hibernate for only certain tables and slowly create more entities?

2) Or does Hibernate only support “all-or-nothing” (i.e. I can’t have other code touching any entities or tables used by Hibernate)?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Glide
  • 20,235
  • 26
  • 86
  • 135

2 Answers2

1

Hibernate is not an all-or-nothing solution. You can map just as many tables you want and still use other persistence frameworks.

As long as the transaction is visible across your different DAOs (like when sharing the database connection or using JTA), you should only be concerned about flushing the EntityManager before executing a native query.

But you need to be aware of how the AUTO flush mode works, when you are running native queries from Hibernate.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • @VladMichalcea Could you elaborate on transaction is visible across DAOs? Does it mean Hibernate should use the same TransactionManager as other DAOs? – Glide Jan 12 '16 at 18:41
  • It means you have to use the Spring Jpa or Transaction Manager and use the same DataSource with both Hibernate and JdbcTemplate – Vlad Mihalcea Jan 12 '16 at 19:51
  • @VladMichalcea I see. Can you elaborate on the significance on using the same DataSource? – Glide Jan 13 '16 at 23:59
  • http://stackoverflow.com/questions/6777419/how-to-configure-spring-to-make-jpa-hibernate-and-jdbc-jdbctemplate-or-mybati – Vlad Mihalcea Jan 14 '16 at 06:46
  • Just to confirm my understanding: The same `TransactionManager` should be used so that if Hibernate and jdbcTemplate are making changes within the same method marked as `@Transactional`, they would see changes made by the other. Is my understanding correct? – Glide Jan 14 '16 at 19:13
  • It's correct because a database connection is going to be shared. – Vlad Mihalcea Jan 14 '16 at 19:29
  • @VladMichalcea And there is probably no reason when there's a need forHibernate and JdbcTemplate to use different TransactionManager? – Glide Jan 15 '16 at 20:11
  • You can create two TMs if you want. There's no limitation. – Vlad Mihalcea Jan 15 '16 at 21:29
  • @VladMichalcea My last question meant what would be a good use case to have two transaction managers? – Glide Jan 20 '16 at 01:04
  • Two transaction managers are useful when you need to integrate two databases without needing to coordinate changes across these two data sources. – Vlad Mihalcea Jan 20 '16 at 05:05
0

Officially yes, you can certainly use hibernate along side other query methods at the same time.

As for safety - that really depends on what you're doing. I'd be concerned about changes to your data. For instance if you query an object using hibernate, and then later modify the underlying table with Spring, hibernate will not know about the change. Maybe not technically unsafe but its a bit of a recipe for a hard to figure out bug.