File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Persistence error at new entry from Netbeans CRUD application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Persistence error at new entry from Netbeans CRUD application" Watch "Persistence error at new entry from Netbeans CRUD application" New topic
Author

Persistence error at new entry from Netbeans CRUD application

Maria Michael
Greenhorn

Joined: Sep 07, 2011
Posts: 18

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

Joined: Sep 07, 2011
Posts: 18

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?

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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...


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Maria Michael
Greenhorn

Joined: Sep 07, 2011
Posts: 18

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
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
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.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Maria Michael
Greenhorn

Joined: Sep 07, 2011
Posts: 18

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Persistence error at new entry from Netbeans CRUD application
 
Similar Threads
javax.mail error
Connection Multiple Databases with diynamically persistence unit names
EJB local injection and transactions
ProgrammaticLogin in EJB - Oracle Glassfish 3.1 and JSP
Target Unreachable, 'null' returned null