/* * Created on Dec 4, 2005 * Converted from jpox to datanucleus 8 Jan 2009 * */ package org.jpox.test; import java.util.Collection; import java.util.Properties; import javax.jdo.JDOHelper; import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManagerFactory; import javax.jdo.Query; import javax.jdo.Transaction; public class Main { public static void main(String[] args) { Properties props = new Properties(); props.setProperty("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.jdo.JDOPersistenceManagerFactory"); props.setProperty("javax.jdo.option.ConnectionDriverName", "com.mysql.jdbc.Driver"); props.setProperty("javax.jdo.option.ConnectionURL","jdbc:mysql://localhost/atest"); props.setProperty("javax.jdo.option.ConnectionUserName", ""); props.setProperty("javax.jdo.option.ConnectionPassword", ""); props.setProperty("javax.jdo.option.NontransactionalRead", "true"); props.setProperty("javax.jdo.option.NontransactionalWrite", "false"); props.setProperty("javax.jdo.option.Optimistic", "false"); props.setProperty("datanucleus.autoCreateSchema", "false"); props.setProperty("datanucleus.validateTables", "false"); props.setProperty("datanucleus.validateConstraints", "false"); PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props); // Persist the first object(s) PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); A a = new A(); a.setName("test1"); a.add((short)1); pm.makePersistent(a); tx.commit( ); } catch (Exception e) { e.printStackTrace(); System.err.println(">> Failed to persiste object " + e.getMessage()); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } // Try to add duplicate item to set pm = pmf.getPersistenceManager(); tx = pm.currentTransaction(); try { tx.begin(); A a = null; Query query = pm.newQuery(A.class, "name == n" ); query.declareParameters( "String n" ); Collection col = (Collection)query.execute( "test1" ); if (col.size() == 1) { a = (A) col.iterator().next(); } if (a != null) { // THIS NEXT LINES CAUSES THE PROBLEM a.add((short)1); } tx.commit(); } catch (Exception e) { e.printStackTrace(); System.err.println(">> Failed to add duplicate to set " + e.getMessage()); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } } }