This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB DataSourceDefinition annotation  RSS feed

 
Ashley Lester
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a simple application to do CRUD on a database (in Netbeans). I have this workflow:

JSF xhtml page - Backing Bean - EJB - Entity Class - Database

I can ping the database using Glassfish.

I have not yet worked on the JSF page or the backing bean. I am developing an EJB class now.

I read that I have to annotate the EJB with @DataSourceDefinition to define the database connection properties.

I have two questions:

- Is this necessary given that I have a persistence.xml which already defines the JNDI data source name
- Do I need to give this data source definition to all the EJB classes I may want to create

Is there an... easier way?

Thanks

Ashley
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never used @DataSourceDefinition. Instead, I use a container-specific mechanism to define the data source. In JBoss I both used data source XML files in the deployments folder, and defining the data sources in the JBoss configuration (which can be configured using JBoss CLI, among others). In WebLogic and GlassFish I defined data sources in the WebLogic / GlassFish console application.
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for using persistence.xml, this does not contain the data source itself. It only contains the JNDI name to the data source. If you don't define the data source, your persistence unit will be useless (and probably will cause errors during application deployment).
 
Ashley Lester
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I have never used @DataSourceDefinition. Instead, I use a container-specific mechanism to define the data source. In JBoss I both used data source XML files in the deployments folder, and defining the data sources in the JBoss configuration (which can be configured using JBoss CLI, among others). In WebLogic and GlassFish I defined data sources in the WebLogic / GlassFish console application.


Ok, cool. My difficulty is figuring out the "correct" way to connect to a data source, and so have access to entity classes, from an EJB.

If I declare a persistence unit through @PersistenceContext, is this sufficient?

For example:



That's just an example. In my own code, I have declared the entity manager through a separate class:



As I have already provided the persistence unit through the declaration of the entity manager, does this mean I can access my entity class without extra annotations? Like the following:



edit: To clarify, as you can see, I already have the database connection attributes defined in Glassfish:

 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't worked with GlassFish for a while, but I assume that data source has a JNDI name. You then use that JNDI name in your persistence.xml file, and the persistence unit in your EntityManager declaration. You already have that, so it should work now.
 
Ashley Lester
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I haven't worked with GlassFish for a while, but I assume that data source has a JNDI name. You then use that JNDI name in your persistence.xml file, and the persistence unit in your EntityManager declaration. You already have that, so it should work now.


Indeed it does, thanks for your help
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!