File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes CDI question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "CDI question" Watch "CDI question" New topic

CDI question

Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 40
I have a simple java program. There are 10 classes, 4 of which are @Entity beans using hibernate 4 to interface with the database. It runs the old-fashioned way from the command line, eg "java -jar myapp.jar". When I implemented hibernate in this app I followed some rather old examples that include a class.

Hibernate sessions are grabbed via

boilerplate stuff, as far as I can tell.

It works fine the way it is, but I decided to re-design the thing using CDI. I'm wondering if there is some nifty, new way to grab those hibernate sessions, maybe using CDI? But this is not an J2EE app, there is nothing enterprise-y about it. When I try to inject a persistence context or entity manager it comes up "null". And I can't find a single discussion or example of doing this that is not a J2EE app.

Is it possible to acquire a hibernate session using CDI or some other nifty annotation in a plain old java program? Or is this only possible in a J2EE context?

Am I misunderstanding something fundamental? (would not be the first time!)

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

From Introduction to Contexts and Dependency Injection
Chapter 28
Introduction to Contexts and Dependency Injection for the Java EE Platform
Contexts and Dependency Injection (CDI) for the Java EE platform is one of several Java EE 6 features that help to knit together the web tier and the transactional tier of the Java EE platform.

Looks like CDI is a Java EE feature - you need to be using Java EE 6 to use it.

I agree. Here's the link:
subject: CDI question
It's not a secret anymore!