This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet doesn't persist on db

 
Enrico La Cava
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new to JavaEE and I'm trying to write a servlet with a simple interface with MySQL. This servlet actually read the db without any problems but actually it doesn't write any change on db.
I've read the JavaEE tutorial but I cannot find useful thinghs about persistence in servlets.

My code is that:



The first two lines works properly because the value printed changes but the db remain unchanged.

This is the autogenerated code from NetBeans for the persist method:



but also a simple em.persist(object) doesn't work.

Can someone help me please? I use NetBeans with glassfish for develop.

Thanks in advance,

Enrico
 
Rj Salicco
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error, if any, are you getting? Can you post the code in which you are doing your select statement?
 
Enrico La Cava
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Salicco wrote:What error, if any, are you getting?


Actually NetBeans doesn't write any error.

Can you post the code in which you are doing your select statement?


I don't think I've understood. I use Java Persistence so I have an autogenerated Entity class with setter and getter for all field of my table. Netbeans wrote this class.

Now I've tryed to use JDBC instead of Java Persistence and it works fine. Anyone else can help me?

Thanks,

Enrico
 
Rj Salicco
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, JPA, understood. Just trying to see what Netbeans is generating for your entity and which JPA method you are calling to get the data into the instance of your entity.
 
Enrico La Cava
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Salicco wrote:Yes, JPA, understood. Just trying to see what Netbeans is generating for your entity and which JPA method you are calling to get the data into the instance of your entity.


Ok, this is the code:



Two examples of setter and getter:




This is another class (like the example of Java Tutorial) that create and interact with the entity manager:




 
Enrico La Cava
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why my thread was moved...

Now I've try to create a new servlet, very very simple:



This is the error:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named FluidoDemoWebPU: Provider named org.hibernate.ejb.HibernatePersistence threw unexpected exception at create EntityManagerFactory:
javax.persistence.PersistenceException
javax.persistence.PersistenceException: [PersistenceUnit: FluidoDemoWebPU] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at biz.fluido.demo.dbTest.processRequest(dbTest.java:63)
at biz.fluido.demo.dbTest.doGet(dbTest.java:94)

This is my persistence.xml



The Assegni class is the same.

 
Rj Salicco
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there may be a couple of paths to look into:

persistence.xml
- location? where is it in your app structure? META-INF?
- <class> </class> i think you need your class or package for your persistent entities declared here
- Hibernate; There may be some properties that need to be configured because you are specifically using the Hibernate provider.

Also, just something you may also want to do:
- extract your persistence calls to another class that you call from your Servlet.
- then you can write a unit test on that class to make sure the persistence is working.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic