I am using Netbeans and I am trying to test my database created in SQL Server 2008.
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
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.
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?
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.
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()?