Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!

Gajesh Tripathi

+ Follow
since Jul 06, 2006
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Gajesh Tripathi

Today, I am happy because I got solution of this problem. As I said to you, problem is with "refresh" button click hence we need to change in that code only.
Here is solution:-

I repeat old lines of code for click on "refresh" button here:-

Now, just add following lines of code in this function {This is the solution}:-


This forum is very useful for those java professionals who wants to create a master/detail entry swing entry forms including new/delete/refresh & save button using "Netbeans Master/Detail Sample Form" feature without knowing the deep knowledge of java persistence api (JPA)

Thanks to everyone who watch or participate in this forum & Special Thanks to "francesco uman".

I solve this problem but I don't know actual concept behind it i.e. why refreshing of "state" entities solve this problem? If any one know then please tell me...
I made following changes in as suggested by you:-

But it doesn't make any effect in my program... i.e. Still, Saving operation is not done after click on "Refresh" button... Same run time error is occurred.

To run this program on your computer system, you need:-
1. Netbeans IDE (Latest version/v7.3 is preferred) including JDK 7
2. MySQL server {Add database: succession which is required for this very small project that has already given)

Since this common forum hence I am giving steps to run this project after installation of Netbeans, My SQL Server & "succession" database:-
  • create new project(BindingExample) in NetBeans
  • include my provided source files{.java/.form/.xml files} to this project in "binding" package {You can also create your own Entity classes through Netbean:project node->new file->Other...->Persistence->"Entity classes from Database..."}
  • edit file: persistence.xml as per your database settings & rename persistent unit as "BindingExamplePU"

  • NOTE:
  • Text Field of StateId is setEditable(false); I am using it for display only.
  • I also put a infomration message dialog to ensure that information is saving in database properly:-

  • After removing below Comment in (entity):

    Steps which I am using:-
  • 1. Right click on file "" & then Run it.
  • 2. Click on (select) first row of masterTable {Company master}, It will display contents in Text Box & Combo Box.
  • 3. Change State using ComboBox through Mouse {from "Madhya Pradesh" to "Rajasthan"}
  • 4. Click on "Save" button & its successfully saved in database.
  • 5. Change state again: from "Madhya Pradesh" to "Punjab"
  • 6. Click on "Save" button & its again successfully saved in database.
  • {If we follow step-5 & step-6 again & again then it will save information again & again)
  • 7. Click on "Refresh" Button... It will restore information from database & we need to again select table row
  • 8. Select first row of masterTable
  • 9. Change state using comboBox(jcbStateId) from "Rajasthan" to "Madhya Pradesh" again.
  • 10. Click on "Save" button then error is occured at line: entityManager.getTransaction().commit(); & information is not saved....

  • Here is stack trace as desired {Output window text}:-
    [EL Info]: 2013-08-20 09:26:57.484--ServerSession(14120198)--EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
    [EL Info]: 2013-08-20 09:26:59.359--ServerSession(14120198)--file:/C:/Documents and Settings/prism/gt/projects/NumberSlider/build/classes/_BindingExamplePU login successful
    [EL Warning]: 2013-08-20 09:27:25.953--UnitOfWork(1200019)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '12' for key 'PRIMARY'
    Error Code: 1062
    Call: INSERT INTO statemst (StateId, StateName) VALUES (?, ?)
    bind => [2 parameters bound]
    Query: InsertObjectQuery(Madhya Pradesh)
    javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '12' for key 'PRIMARY'
    Error Code: 1062
    Call: INSERT INTO statemst (StateId, StateName) VALUES (?, ?)
    bind => [2 parameters bound]
    Query: InsertObjectQuery(Madhya Pradesh)

    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(
    at binding.FrmCompanyMst.saveButtonActionPerformed(
    at binding.FrmCompanyMst.access$100(
    at binding.FrmCompanyMst$FormListener.actionPerformed(
    at javax.swing.AbstractButton.fireActionPerformed(
    at javax.swing.AbstractButton$Handler.actionPerformed(
    at javax.swing.DefaultButtonModel.fireActionPerformed(
    at javax.swing.DefaultButtonModel.setPressed(
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(
    at java.awt.Component.processMouseEvent(
    at javax.swing.JComponent.processMouseEvent(
    at java.awt.Component.processEvent(
    at java.awt.Container.processEvent(
    at java.awt.Component.dispatchEventImpl(
    at java.awt.Container.dispatchEventImpl(
    at java.awt.Component.dispatchEvent(
    at java.awt.LightweightDispatcher.retargetMouseEvent(
    at java.awt.LightweightDispatcher.processMouseEvent(
    at java.awt.LightweightDispatcher.dispatchEvent(
    at java.awt.Container.dispatchEventImpl(
    at java.awt.Window.dispatchEventImpl(
    at java.awt.Component.dispatchEvent(
    at java.awt.EventQueue.dispatchEventImpl(
    at java.awt.EventQueue.access$200(
    at java.awt.EventQueue$
    at java.awt.EventQueue$
    at Method)
    at java.awt.EventQueue$
    at java.awt.EventQueue$
    at Method)
    at java.awt.EventQueue.dispatchEvent(
    at java.awt.EventDispatchThread.pumpOneEventForFilters(
    at java.awt.EventDispatchThread.pumpEventsForFilter(
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(
    at java.awt.EventDispatchThread.pumpEvents(
    at java.awt.EventDispatchThread.pumpEvents(
    Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '12' for key 'PRIMARY'
    Error Code: 1062
    Call: INSERT INTO statemst (StateId, StateName) VALUES (?, ?)
    bind => [2 parameters bound]
    Query: InsertObjectQuery(Madhya Pradesh)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(
    at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(
    at org.eclipse.persistence.queries.DatabaseQuery.execute(
    at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
    at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(
    at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsForClassWithChangeSet(
    at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(
    at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(
    ... 40 more
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '12' for key 'PRIMARY'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    at java.lang.reflect.Constructor.newInstance(
    at com.mysql.jdbc.Util.handleNewInstance(
    at com.mysql.jdbc.Util.getInstance(
    at com.mysql.jdbc.SQLError.createSQLException(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.sendCommand(
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
    at com.mysql.jdbc.ConnectionImpl.execSQL(
    at com.mysql.jdbc.PreparedStatement.executeInternal(
    at com.mysql.jdbc.PreparedStatement.executeUpdate(
    at com.mysql.jdbc.PreparedStatement.executeUpdate(
    at com.mysql.jdbc.PreparedStatement.executeUpdate(
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(
    ... 73 more
    Please note that I did not press "new" button because I am trying to modify the data hence entityManager.persist(c) is not called. I remove following comment as suggested by you:-
    //hence commented this line
    Its working and result is saved when we select a row and then click on 'save" button after modifying State using Combo Box. Same case is happened if we uncomment the following function code:-
    OR If we uncomment the following code given on "save" button click:-

    But only problem is that It gives error when we click on "Refresh" button and then try to save after changes...
    I hope problem is in "Refresh" button code but what is it? Here is "refresh" button code:-

    If you want to execute my this sample program on your system then please use attached mySQL_successionDB sql file(External Link for mySQL_succession.sql).
    Thanks francesco umani for interest in my problem...

    Here is external link:-
    Easy File Share Link

    Thanks again...
    Dear Developers

    I used netbeans: Master/Detail sample form to create Company master entry form in java
    I added manually a combo box with appropriate binding for foreign key: StateId in CompanyMst which is linked with StateMst

    Since this work should be done in few clicks within Netbeans, I am kinda angry at myself that I spent one and half day on it with no results... Any help?
    I would be appreciate if any one help me.

    I also post my problem with source at java-forums(please see):-
    JPA: Foregin key saving prolem (Java-Forums)

    Thanks In Advance..
    When I use this property DatabaseEditor.class then comboBox is showing but when changing value from combo then following error is showing:-

    java.lang.IllegalArgumentException: argument type mismatch

    Please check my DatabaseEditor class & tell me why this error is occurred if you know about that.

    Any ways, my main problem is solved & its my duty to submit solution here for closing this Thread.
    Modification in

    Modification in

    and its successfully working.[/code]

    Advantage of This Thread:-How to use combo box as a Property Editor? and conclusion is that we do not require to extend PropertyEditorSupport for selecting one value from multiple choice. Instead we set the multiple choice values to enumerationValues attribute of PropertyDescriptor's object.

    Thanks for viewing this Thread...
    6 years ago

    Ranganathan Kaliyur Mannar wrote:Hi Gajesh,
    Welcome to the Ranch!

    There is a class PropertyEditorSupport present in the java.beans package. You need to extend it and override the getCustomEditor() method to return your own component (also I think the supportsCustomEditor() method must also be overridden). You should start from there.

    Thank you Mr. Ranganathan Kaliyur Mannar for your reply..

    I created a class DatabaseEditor as suggested by you...

    Other source codes are attached herewith: Due to limitation of coderanch, I use .jpg extension with actual file, please remove .jpg extension to use these attachment files

    But its not working, please check my code & tell me what & where is actual problem?

    Purpose of my java bean component is : fill list using "connection", "tableName", "keyFieldName" & "valueFieldName" property & also generate condition that can be used in another sql query according to user's selection. You can also import this code into your test/sample netbean project for checking purpose.

    I register this property using following fragment of code:-
    6 years ago
    I created a Java Bean component named as "PnlForQuery" that has a property named "database":-

    /*Default value for this property is "ORACLE" but It can be: "MS-ACCESS", "SQL-SERVER", "MYSQL",....etc.*/
    String database = "ORACLE";

    How can I create a comboBox editor for this property to accept one of above string values for database so that I can use it using Beanbox provided by Netbeans or any other IDE ?

    If we need to use PropertyEditor class for this purpose then plese give me an example...

    Thanks In Advance
    6 years ago