• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Persistence error at new entry from Netbeans CRUD application

 
Greenhorn
Posts: 18
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am using Netbeans and I am trying to test my database created in SQL Server 2008.

The Steps:
1. Created the DB
2. Created new Web Project
3. Created Entity Classes from Database
4. Created JSF pages from Entity Classes.

The resulting CRUD application lists the data that exist in the tables.

But when I try to create a new entry in a table I get a persistence error ("A persistence error occurred.").

The error occurs at the point that the EntityManager is trying to persist

e.g. getEntityManager().persist(entity)

I really do not understand how to spot the problem from this point onwards so that I try to solve it. Any ideas? It's like a black box to me.

Also, Is it a good idea to use these auto generated classes, jsf pages etc to built my final web application? Or should I re-write them? I am feeling I will loose control especially in cases like this that I don't know why the error occurs.

thanks.

 
Maria Michael
Greenhorn
Posts: 18
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so I understood that it throws an exception at the persistence provider's constraints (sql server).

The contraints I have for the specific table are:
1. only the Identity Primary key (automatic generation of primary key by sql) as not nul = true

Isn;t it here that I have to state that SQL will handle the generation of the primary key so jpa dont do anything about it?



Is there anywhere else I have to modify the code?

 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

that I have to state that SQL will handle the generation of the primary key so jpa dont do anything about it?


What you meant by this? Generally when you ask the provider(that's JPA provider) to generate the IDs for you it decides the best mechanism based on the RDBMS provider etc...
 
Maria Michael
Greenhorn
Posts: 18
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijitha thanks for replying,
What I meant was that I want SQL to do its thing regarding the generation of IDs as I have my primary keys as Identities that increament automatically.

It seems that I found the solution by doing the following:


So by saying insertable=false it does not try to insert a value for the id and therefore it succeeds without errors.

The id gets generated ok and is returned back ok as it is displayed in the table with all records.

The problem that still exists, is the timestamp I have in SQL Server in all tables (for each record that gets inserted I keep the date and time by using the getdate() of SQL server.
This value is not displayed in my jsf page whereas the ID is displayed immedietly.
I have to redeploy my application for it to display.

So what am I doing wrong??

Do I have to call a EntityManager.refresh() somewhere?

thanks
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first error was thrown by the Java Validation Framework from your @NotNull constraint, not from the database. This is because the validation is done before the JPA persist/insert which will assign the id. Removing the @NotNull as you seem to have done will remove the error.

If you are defaulting a timestamp on the database, then you will not get this back in your object model unless you refresh the object. So, if you want to do this, then you would need to call refresh.
It would be much more efficient to instead assign the timestamp in your Java object's constructor, then there would be no need to refresh.
If you use @Version locking with a timestamp then JPA will auto assign the value, which may be another option.
 
Maria Michael
Greenhorn
Posts: 18
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James thank you,

Just refresh after persist was not working and managed it to work with the following code



It needed a flush before the refresh as from what I understood the refresh was executed before the entity manager committed the changes and therefore I got nothing.

I see what you are saying about not being a very efficient way. My application is very small and my end users are not many. Do you think I might still have performace problems if I do for each persist() a flush() and a refresh()?

Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic