Return-Path: Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: (qmail 38830 invoked from network); 23 Feb 2011 17:37:16 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 23 Feb 2011 17:37:16 -0000 Received: (qmail 54947 invoked by uid 500); 23 Feb 2011 17:37:16 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 54700 invoked by uid 500); 23 Feb 2011 17:37:14 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 54692 invoked by uid 99); 23 Feb 2011 17:37:13 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Feb 2011 17:37:13 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of david.m.beer@gmail.com designates 74.125.82.174 as permitted sender) Received: from [74.125.82.174] (HELO mail-wy0-f174.google.com) (74.125.82.174) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Feb 2011 17:37:05 +0000 Received: by wyg36 with SMTP id 36so1227655wyg.33 for ; Wed, 23 Feb 2011 09:36:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:subject:from:reply-to:to:cc:in-reply-to :references:content-type:date:message-id:mime-version:x-mailer :content-transfer-encoding; bh=Yl22vR+mFJxvxrHFulR63E48GRIQzTOZe0kHZ1cbGwo=; b=PUBzL4zCm4KsJ04C790wDFKSvMLySnsvspfp81G1QFmAe3oEPymzMvq3wzqb+fPuHC FnhmMHRwhxjyszvWiiM5oFbJH1GpAJ/yON1ahLb+NHtjDloQy/EcgtBSEvxlG815JNxE x7Ct5a41ozV2YEGstQpyPKb2hV7lg1icU+2Kc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:from:reply-to:to:cc:in-reply-to:references:content-type :date:message-id:mime-version:x-mailer:content-transfer-encoding; b=jEHnjPqcxHD5JwReyUtSjernEXgLAXEwEfMOJ4C/x3HClVFrAR371p9O6Ammiod616 gpTIdgrS4lUPdY3eI6mUBYZGJ+um3FmOgycpjdAjSVetwVV+1WlONrDR8Lw6vOzTmRff lmPK5ynGl4upU1zN4sCnP1KZADOKNUG8O5jXk= Received: by 10.217.4.66 with SMTP id t44mr419493wes.32.1298482362171; Wed, 23 Feb 2011 09:32:42 -0800 (PST) Received: from [192.168.1.4] (client-82-31-7-249.midd.adsl.virginmedia.com [82.31.7.249]) by mx.google.com with ESMTPS id n1sm3273614weq.7.2011.02.23.09.32.38 (version=SSLv3 cipher=OTHER); Wed, 23 Feb 2011 09:32:39 -0800 (PST) Subject: Re: Sequence Generator on a non ID field From: David Beer Reply-To: david.m.beer@gmail.com To: Michael Dick Cc: users@openjpa.apache.org In-Reply-To: <1298476562.3454.20.camel@vulcan> References: <1298471441.3454.15.camel@vulcan> <1298476562.3454.20.camel@vulcan> Content-Type: text/plain; charset="UTF-8" Date: Wed, 23 Feb 2011 17:32:37 +0000 Message-ID: <1298482357.3454.22.camel@vulcan> Mime-Version: 1.0 X-Mailer: Evolution 2.32.1 (2.32.1-1.fc14) Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org Hi >From a bit more research it seem @GeneratedValue can only be used with @Id. If this is the case what is the best way to insert auto generated data based on a sequence and the previous number. David On Wed, 2011-02-23 at 15:56 +0000, David Beer wrote: > Hi Mike > > This is the full stack trace. > > > org.apache.openjpa.persistence.InvalidStateException: Can only perform > operation while a transaction is active. > at > org.apache.openjpa.kernel.BrokerImpl.assertTransactionOperation(BrokerImpl.java:4595) > at org.apache.openjpa.kernel.BrokerImpl.rollback(BrokerImpl.java:1496) > at > org.apache.openjpa.kernel.DelegatingBroker.rollback(DelegatingBroker.java:933) > at > org.apache.openjpa.persistence.EntityManagerImpl.rollback(EntityManagerImpl.java:589) > at > com.copperarrow.crm.basemodel.jpa.UsersTest.testCommitUser(UsersTest.java:67) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) > at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) > at java.lang.reflect.Method.invoke(Method.java:597) > at org.junit.runners.model.FrameworkMethod > $1.runReflectiveCall(FrameworkMethod.java:44) > at > org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) > at > org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) > at > org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) > at > org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) > at > org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) > at > org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) > at > org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) > at > org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) > at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) > at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) > at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) > at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) > at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) > at > org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) > at > org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) > at org.junit.runners.ParentRunner.run(ParentRunner.java:236) > at org.junit.runners.Suite.runChild(Suite.java:128) > at org.junit.runners.Suite.runChild(Suite.java:24) > at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) > at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) > at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) > at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) > at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) > at > org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) > at > org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) > at org.junit.runners.ParentRunner.run(ParentRunner.java:236) > at > org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35) > at > org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115) > at > org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) > at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) > at java.lang.reflect.Method.invoke(Method.java:597) > at org.apache.maven.surefire.booter.ProviderFactory > $ClassLoaderProxy.invoke(ProviderFactory.java:103) > at $Proxy0.invoke(Unknown Source) > at > org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150) > at > org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91) > at > org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69) > > com.copperarrow.crm.basemodel.jpa.PersistSuite Time elapsed: 0.091 sec > <<< ERROR! > > org.apache.openjpa.persistence.InvalidStateException: The context has > been closed. The stack trace at which the context was closed is > available if Runtime=TRACE logging is enabled. > at > org.apache.openjpa.kernel.BrokerImpl.assertOpen(BrokerImpl.java:4573) > at > org.apache.openjpa.kernel.BrokerImpl.beginOperation(BrokerImpl.java:1895) > at org.apache.openjpa.kernel.BrokerImpl.close(BrokerImpl.java:4271) > at > org.apache.openjpa.kernel.DelegatingBroker.close(DelegatingBroker.java:1346) > at > org.apache.openjpa.persistence.EntityManagerImpl.close(EntityManagerImpl.java:1282) > at > com.copperarrow.crm.basemodel.jpa.DatabaseConnection.shutdown(DatabaseConnection.java:72) > at > com.copperarrow.crm.basemodel.jpa.PersistSuite.tearDown(PersistSuite.java:38) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) > at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) > at java.lang.reflect.Method.invoke(Method.java:597) > at org.junit.runners.model.FrameworkMethod > $1.runReflectiveCall(FrameworkMethod.java:44) > at > org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) > at > org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) > at > org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37) > at org.junit.runners.ParentRunner.run(ParentRunner.java:236) > at > org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35) > at > org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115) > at > org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) > at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) > at java.lang.reflect.Method.invoke(Method.java:597) > at org.apache.maven.surefire.booter.ProviderFactory > $ClassLoaderProxy.invoke(ProviderFactory.java:103) > at $Proxy0.invoke(Unknown Source) > at > org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150) > at > org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91) > at > org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69) > > The full class code is as follows: > > @Entity > @Table(name = "t_USERS") > public class Users implements Serializable { > > /** Serialisation ID. **/ > private static final long serialVersionUID = 1L; > /** Entity id. **/ > @Id > @GeneratedValue(strategy = GenerationType.IDENTITY) > private Long id; > /** User ID. This will be a user ID starting from 0001. **/ > @GeneratedValue(strategy = GenerationType.TABLE, generator = > "UserGen") > @TableGenerator(name = "UserGen", table = "USER_GEN", pkColumnName = > "PK", > valueColumnName = "USERID") > private Long userID; > /** User Name associated with the user used to login with. **/ > private String userName; > /** hash of the users password. **/ > private String passHash; > /** Role Category is the role of the user e.g. Admin, sales, etc. > **/ > @Enumerated(EnumType.STRING) > private RoleCategory roleCategory; > /** ci is the contact information associated with the user. **/ > @Embedded > private ContactInformation ci; > > /** > * @return id > */ > public Long getId() { > return id; > } > > /** > * @param nId the id to set > */ > public void setId(final Long nId) { > this.id = nId; > } > > /** > * @return the serialVersionUID > */ > public static long getSerialVersionUID() { > return serialVersionUID; > } > > /** > * @return the userID > */ > public Long getUserID() { > return userID; > } > > /** > * @param userID the userID to set > */ > public void setUserID(Long userID) { > this.userID = userID; > } > > /** > * @return the userName > */ > public String getUserName() { > return userName; > } > > /** > * @param userName the userName to set > */ > public void setUserName(String userName) { > this.userName = userName; > } > > /** > * @return the passHash > */ > public String getPassHash() { > return passHash; > } > > /** > * @param passHash the passHash to set > */ > public void setPassHash(String passHash) { > this.passHash = passHash; > } > > /** > * @return the roleCategory > */ > public RoleCategory getRoleCategory() { > return roleCategory; > } > > /** > * @param roleCategory the roleCategory to set > */ > public void setRoleCategory(RoleCategory roleCategory) { > this.roleCategory = roleCategory; > } > > /** > * @return the ci > */ > public ContactInformation getCi() { > return ci; > } > > /** > * @param ci the ci to set > */ > public void setCi(ContactInformation ci) { > this.ci = ci; > } > > @Override > public int hashCode() { > int hash = 0; > hash += (id != null ? id.hashCode() : 0); > return hash; > } > > @Override > public boolean equals(final Object object) { > // TODO: Warning - this method won't work in the case the id > fields > // are not set > if (!(object instanceof Users)) { > return false; > } > Users other = (Users) object; > if ((this.id == null && other.id != null) > || (this.id != null && !this.id.equals(other.id))) { > return false; > } > return true; > } > > @Override > public String toString() { > return "com.copperarrow.crm.basemodel.jpa.Users[ id=" + id + > " ]"; > } > > If you need anything else just ask. > > Thanks > > David > > On Wed, 2011-02-23 at 09:17 -0600, Michael Dick wrote: > > Hi David, > > > > Could you post the stack for the exception you're getting? > > > > GeneratedValues should be obtained by your non-jta-data-source (or > > openjpa.ConnectionFactory2) - and that shouldn't require a > > transaction. > > > > -mike > > > > On Wed, Feb 23, 2011 at 8:30 AM, David Beer > > wrote: > > Hi all > > > > I am trying to get a value for a field set when commited to > > the db, to > > be auto generated. This field is not an ID for the table. I > > seem to get > > the error message "InvalidStateException: Can only perform > > operation > > while a transaction is active." > > > > My class looks like the following: > > > > @GeneratedValue(strategy = GenerationType.TABLE, generator = > > "UserGen") > > @TableGenerator(name = "UserGen", table = "USER_GEN", > > pkColumnName = > > "PK", valueColumnName = "USERID") > > private Long userID; > > > > Any Thoughts. > > > > Thanks > > > > David > > > > > >