Forums Register Login

Persistence error at new entry from Netbeans CRUD application

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
If you're gonna buy things, buy this thing and I get a fat kickback:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6914 times.
Similar Threads
Connection Multiple Databases with diynamically persistence unit names
javax.mail error
ProgrammaticLogin in EJB - Oracle Glassfish 3.1 and JSP
Target Unreachable, 'null' returned null
EJB local injection and transactions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:50:29.