3

I am developing a fairly simple web app using Spring MVC + JSP + Hibernate. To get started I followed this tutorial: http://www.cavalr.com/blog/Spring_3_and_Annotation_Based_Hibernate_4_Example

This is working ok, but I am concerned about the verbosity of code. To support the User entity, we also have UserDao, UserDaoImpl, UserService and UserServiceImpl. I understand that for a large enterprise application you might actually want this. But for my simple web app this is unneeded (and unwanted) complexity. I could achieve all this in Python much more simply.

So, is there any way to have Hibernate use the Active Record design pattern? The sort of thing I'd be looking to do is have User inherit methods like get() from a base class. So you could do User.get(userName)

I am open minded to using a different ORM to Hibernate; I'd just started with that as it seems the common choice. And also it's supposedly similar to Python SQLAlchemy, which I am familiar with.

I am also open minded to more out of the box solutions, perhaps an IDE plugin that can autogenerate the DAO classes.

Any suggestions appreciated!

Paul

paj28
  • 2,210
  • 3
  • 25
  • 37

3 Answers3

2

Have a look at spring roo, it uses the active record pattern.

Or (after a look at roo) you can build it by your own. All you need is:

  • @Configurable at your entities to enable injection (this requires also real AspectJ)

You can use it to inject the EntityManager or hibernateSession

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • I don;t see how this answers the question – NimChimpsky Jun 21 '13 at 12:48
  • 2
    The question was:"So, is there any way to have Hibernate use the Active Record design pattern?" Roo is an example for a way how to build an active record architecture with spring. Most people learn good form examples. – Ralph Jun 21 '13 at 12:56
  • have you got an example of adding configuravble to an entity to enable its injection ? – NimChimpsky Jun 21 '13 at 12:57
  • 1
    @NimChimpsky: start the roo shell, create a new project, configure the setup and let roo create one entity (Google for any roo tutorial and follow the first steps) then have a look at the code (including roo generated aspectj files) - I do not want to be impolite but I write this on my phone (vacation on Java island), so I can't do more than writing this instructions. – Ralph Jun 21 '13 at 13:08
  • Wow, roo looks amazing! I am having a play with it now. I think I will probably end up not using it for this project, but it's good to know about. Thanks – paj28 Jun 21 '13 at 23:59
1

I think you might be looking for jOOQ. Since you're referring to Python's SQLAlchemy, jOOQ might be a slightly better match than Hibernate as it is much more SQL-centric.

Another popular ActiveRecord implementation in Java is ActiveJDBC, which (to my knowledge) offers the kind of methods you're looking for through instrumentation. An example from their website:

List<Person> people = Person.where("name = 'John'");
Person aJohn =  people.get(0);
String johnsLastName = aJohn.get("last_name");

(of course, this answer is biased, as I'm the jOOQ developer)

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • Thanks... I have decided to stick with Hibernate but it's good to know about these. jOOQ looks like a cool project. BTW, I did a lot of work on the SQLAlchemy MS-SQL support, so I have some idea of your pain as an ORM developer :-) – paj28 Jun 21 '13 at 23:45
  • @paj28: There's no pain. It's a lot of fun! I particularly like SQL Server, it's a great database! – Lukas Eder Jun 22 '13 at 07:05
1
 UserDao, UserDaoImpl, UserService and UserServiceImpl

The interfaces make testing with mock objects easier, I guess you could rid of them if you don't plan using mocks.

I have one generic DomainRepository for simple get/sets - you don;t need the service class(es) unless multple entites are involved.

To further reduce the code you write, take a look a spring data, although I guess it might be overkill here.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311