primefaces in-cell editing on a table with compound primary key - primefaces

I'm using primefaces 3.2 in-cell editing component to update an oracle DB table having compound primary key. The user should be able to edit also the primary key values but in this way if I use
em.merge(entity);
I got the following error
Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [voce] of class [entity.competenze_distaccati.CompetenzeDistaccatiPK] is mapped to a primary key column in the database. Updates are not allowed.
...which sounds reasonable to me. The question is: How can I give the user the possibility to change also the primary key values?
rowEdit event listener is called with the changed value so I don't know which was the original record to perform a delete-insert.

Allowing modification of Primary Key is not Correct Way of handling the data. Instead, allow for deletion and add the record again with required values. This way, you will be following the standards and also, your problem gets solved.

Related

Cayenne "resets" primary key value?

I am using Cayenne to add records to a MySQL database, and I am seeing some strange behavior.
When I run my application, I create a DataContext, perform a series of adds, then close the application. This works out well, because I am using an integer for a primary key, and when I add a record to the database, the key automatically increments. For some reason, it starts at 200 for the first record, then goes to 201 for the second record, etc.
If, however, I stop the application, then run it again, the primary key starts at 200 again! This, of course, causes an exception to be thrown because a new record ends up having a duplicate primary key. It is looking like when I create a new object using the DataContext's newObject() after starting my application, Cayenne does not "remember" how far the primary key was incremented when the application was previously run.
Does anyone know what is causing this reset of the primary key values, and (more importantly) how to stop it from happening??? Or have I found a bug in the current version of Cayenne? I am using Version 3.0.2.
Someone please advise...
The last used PK for a given table is stored in a special table called AUTO_PK_SUPPORT. Please check the contents of this table between the restarts of your app. Also check you application Cayenne logs for reads and writes to AUTO_PK_SUPPORT. This should give you an idea of what's happening.
Aside from that you might switch to auto-increment PK (see "Primary Key Provided by Database" section here). MySQL supports auto-incremented PK columns and if you have an option of altering the schema, this IMO is the cleanest PK generation strategy out of all available. (And it doesn't require AUTO_PK_SUPPORT).

Exception when deleting an object

When I try to delete an object, I get a 'System.InvalidOperationException' with the following additional information:
The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.
How can I get more information? This message is like "something's wrong, but we are not going to reveal what"
The object/row you've tried to delete is probably a foreign key - the foreign key can't be null in the related table. Try deleting the other object/row first.
This issue would also happen if you tried to do this in SQL, you'd get:
The DELETE statement conflicted with the REFERENCE constraint 'FK_foo_bar'. The conflict occured in database...

violation of primary key constraint .Cannot insert duplicate key in object using ADO

we are working on a users apllication using Access2003(VBA) as software language and SQL Server 2005 as database.
We are using ADO method and we encounter a problem.
when users create new record in a ADO Screen and they want to save the record after implementing it they receive this error :
error -2147217873 violation of primary key constraint 'PK_ '.Cannot insert duplicate key in object 'Pk_...'
Any help will be appreciated
Thanks in advance
The problem occures since you can't have two primary keys with the same value.
If you are using Ints as primary key, remember to put auto-increment on it.
If you are using GUID as primary key, you might forget to set the guid to sometheing else than the default empty guid, and there by trying to insert and empty guid twice.
Are you trying to insert a new record with the primary key field having a value that is already in the database. The primary key field must always contain unique values.
Check witch columnt is your PrimaryKey. If you are trying to insert value that already exist then you are getting that error.
you should create your PK value either from your code or on the SQL side. On the SQL side, when creating your database, you have to indicate that default value for "myPrimaryKey" field is uniqueIdentifier, while from code, you could have something like
myRecordset.fields("myPrimaryKey") = stGuidGen()
(check here for the stGuidGen function)
There are some pros and cons to each method. By implementing the SQL method, you do not have to care about generating PKs anymore. By doing it through code, you can store the newly generated value without having to requery the database, and reuse it immediatly in your code, and this can be very useful.
get the property of your primary key column and set the identity property to YES

Insertion conflict LINQ-to-SQL

I get this error when insert a new album:
{System.Data.SqlClient.SqlException:
INSERT statement conflicted with COLUMN FOREIGN KEY
constraint 'FK_ChannelAlbum_Group'.
The conflict occurred in database 'Stamper', table 'Channel', column 'ID'.
I don't know what is going on because sometime I insert the new album to the context the error will occur but Album table has no Channel ID columns only Album_Channel table. This insert does not influence the album_channel and channel table at all. Why is there a conflict.
I realized that after I have created the new album after that i try inserting a album_channel data I have an error so I stop debugger and try to fix the problem. Once i have work that out, I go an insert a new album once more but it gives me this error always. So I close my visual studio and reopen it in order to work.....
I am not sure is it a good way of having singleton style of creating the context e.g.
I have write a context as class and this context only created once, to prevent it to create too many times.
Hard to answer without a diagram. It looks like you have a foreign key in the table you're inserting which is not a valid primary key in the table it is referencing. If you don't believe you have one, then you could try checking your relationships and possibly regenerating your L2S entities. Maybe even try to insert into that table manually and see if you get the same error.
Context wrapped in a singleton is a bad idea for web applications as it isn't thread safe. You could look into the 'unit of work' pattern or try caching the context instance.

Why is it necessary to assign atleast one column in a table (in MySQL) as PRIMARY KEY for the table to accept UPDATE and INSERT statements via JDBC?

Also; can I add a negative INT inside a PRIMARY KEY column inside a table via JDBC?
There should be no reason why you can't have a negative number in a primary key field, unless you are using an unsigned integer as its data type.
When you don't have a primary key defined (or a unique index for that matter), the database server has no way of knowing that the rows are unique. Being able to tell one row from another is pretty fundamental when it comes to databases, and I think maybe the designers of MySQL probably are forcing this on you -- I don't know, since I don't use MySQL all that much... It's a problem that I've never ran into, to be honest, because pretty much every table that I ever create has a PK!
Why is it necessary to assign atleast one column in a table (in MySQL) as PRIMARY KEY for the table to accept UPDATE and INSERT statements via JDBC?
This is not a JDBC limitation. This is a database limitation. Consult DB-specific docs for answers.
Also; can I add a negative INT inside a PRIMARY KEY column inside a table via JDBC?
This is not a JDBC limitation. This is a database limitation. Consult DB-specific docs for answers.
To make things clear: JDBC is here just a simple tool to give you the ability to execute the SQL language using the Java language. If you get exceptions of java.sql package, then the problem more lies in the faulty SQL syntax used, or a limitation in the database in question, or in rare cases a bug in the DB-supplied JDBC driver, but it certainly isn't caused by the JDBC API in general as you seem to think.