aspose file tools*
The moose likes JDBC and the fly likes Saving database records problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Saving database records problem" Watch "Saving database records problem" New topic
Author

Saving database records problem

Milutin Markovic
Greenhorn

Joined: Mar 18, 2010
Posts: 12
I have 3 tables (MySQL). Invoice, product and JoinTable InvoiceItems.
When I have new Invoice, I select one ore more products from Product table.
I need insert one row in Invoice table and one or more rows in InvoiceItems table. Product table not change.
I geting error that column 'idinvoice' cannot be null. Can somebody help me please please please.
You may see picture of tables relationship below (attachment).







Backing bean part



Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'idinvoice' cannot be null
Error Code: 1048
Call: INSERT INTO fakstavke_u (CenaStr, trosakproc, mpc, Nabavna, pdv, Kolicina, vpc, marza, Rabat, trosak, marzaproc, Cena, idproduct, idinvoice) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [1.0, 0.0, 450.0, 450.0, 18, 1.0, 450.0, 0.0, 0.0, 0.0, 0.0, 450.0, null, null]
Query: InsertObjectQuery(entity.InvoiceItems[invoiceItemsUPK=null])
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
*
*



[Thumbnail for tritabele2.JPG]

Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

The column "idinvoice" has been declared with the constraint "Not Null" in DB.

And you have given the annotation



"insertable=false" which stops to insert the value into the "idinvoice" column... So, the INSERT Query is constructed with "NULL" value to be inserted into that column...

Due to the "Not Null" constraint, you are getting such error... make it as "insertable=true" or remove that[ by default insertable is true]

SCJP 6 [SCJP - Old is Gold]
Milutin Markovic
Greenhorn

Joined: Mar 18, 2010
Posts: 12
I have error in my queston. Idproduct cannot be null but I understood you and fix that. Thanks thanks thanks.

But when idproduct column is insertable=true I get error that column specified twice. Same with column idinvoice.

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column 'idproduct' specified twice
Error Code: 1110
Call: INSERT INTO invoiceItems (CenaStr, trosakproc, mpc, Nabavna, pdv, Kolicina, vpc, marza, Rabat, trosak, marzaproc, Cena, idproduct, idproduct, idinvoice, idinvoice) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [1.0, 0.0, 470.0, 470.0, 18, 1.0, 470.0, 0.0, 0.0, 0.0, 0.0, 470.0, null, null, null, null]
Query: InsertObjectQuery(entity.invoiceItems[invoiceItemsUPK=null])

Why have duplicate column? I will try to solve that tomorow. I am glad that I rid of error 1048 that troubling me two days.
 
 
subject: Saving database records problem