• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

PersistenceContext null  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ALL,

I'm using Wildfly to create a simple crud app to manage books (find and persist)
But EntityManager is always returning null.

I tried Persistence.createEntityManagerFactory("PU") method, it's working fine.
I need to know why my code is not working..What is missing or wrong ?

Thank you

Personel level in Jee = beginner
Wildfly version 14
IDE Intellij
Java 7
JEE 7
ORM EclipseLink



Book.java:



BookService.java



persistence.xml



Test Main


index.jsp



StackTrace:





Code structure:



 
Master Rancher
Posts: 4042
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, do not put Java code in a JSP page.
That processing code (finding books) should be part of a servlet, which would call the Service code itself then forward the result to a JSP for display.

Now, the reason you have an issue here is because nothing is injecting the EntityManager into the BookService class.
This is because Test creates a new instance of it, and so the PersistenceContext annotation plays no part.
 
A. Baaz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First off, do not put Java code in a JSP page.


What's the problem of putting java in a JSP ?

That processing code (finding books) should be part of a servlet, which would call the Service code itself then forward the result to a JSP for display.


Do you mind writing me an example ?

Now, the reason you have an issue here is because nothing is injecting the EntityManager into the BookService class.
This is because Test creates a new instance of it, and so the PersistenceContext annotation plays no part.


I will change my code and come back to you.
Thanks
 
Dave Tolls
Master Rancher
Posts: 4042
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A. Baaz wrote:

First off, do not put Java code in a JSP page.


What's the problem of putting java in a JSP ?



Lots.
In this case, you can't inject things like your EntityManager.
But also, it's almost impossible to debug.

JSPs were not designed to be the processor side.  They were designed to handle the display side of things.

A. Baaz wrote:

That processing code (finding books) should be part of a servlet, which would call the Service code itself then forward the result to a JSP for display.


Do you mind writing me an example ?



Pretty much any proper servlet example on the web will show you.
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!