openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Beer <david.m.b...@gmail.com>
Subject Re: Sequence Generator on a non ID field
Date Wed, 23 Feb 2011 23:10:07 GMT
Hi Mike

Thanks for the thoughts and sugestions, I am in the process of
rethinking how I want this work at the moment. Thanks for the help and
advice.

Thanks again

David

On Wed, 2011-02-23 at 14:21 -0600, Michael Dick wrote:
> Do you want the value stored with leading 0s, or just displayed? 
> 
> Stored will be tricky - you'll need to store it as a CHAR / VARCHAR,
> and use a String in your code. Then there would be generator changes
> needed to keep the leading 0s (I don't think we have this code in
> place right now).
> 
> Displaying is fairly easy - String.format will handle it. 
> 
> I guess the real question is whether you want to differentiate between
> 001 and 0001. If those are the same number, storing as a Long and
> fixing the display logic should work. If they're different then you'll
> need to change to use Strings.. 
> 
> Or maybe I just didn't grok what you've looking for.. 
> 
> -mike
> 
> On Wed, Feb 23, 2011 at 12:08 PM, David Beer <david.m.beer@gmail.com>
> wrote:
>         Hi Mike
>         
>         Thanks for the hint it seemed related to the order in which my
>         tests
>         were executed in my Test Suite. As soon as I moved that
>         particular test
>         forward all tests passed. Value was generated as expected. Do
>         you know
>         if there is a way to make it so that openjpa doesn't remove
>         the 00 at
>         the front of the number. For example I am trying to get a
>         generated
>         format of 0001 and to increase by one. So the next number
>         would be 0002
>         is there anyway of doing this thanks again.
>         
>         David
>         
>         
>         On Wed, 2011-02-23 at 11:41 -0600, Michael Dick wrote:
>         > Hi David,
>         >
>         > From a JPA spec perspective you're right - the @GV
>         annotation can only
>         > be used with IDs. OpenJPA lets you use it on any field [1],
>         but that
>         > support might not be perfect.
>         >
>         > The stacks look like the transaction was rolled back and
>         it's the
>         > rollback that requires an active tran. There's also an
>         exception
>         > trying to close an EM which is already closed - coming from
>         your
>         > teardown method. Could the teardown exception be hiding the
>         root cause
>         > for the rollback?
>         >
>         > -mike
>         >
>         >
>         > [1]
>         >
>         http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_meta_gen
>         >
>         > On Wed, Feb 23, 2011 at 11:32 AM, David Beer
>         <david.m.beer@gmail.com>
>         > wrote:
>         >         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.
>         >         >
>         >         > <openjpa-2.0.1-r422266:989424 nonfatal user error>
>         >         >
>         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!
>         >         > <openjpa-2.0.1-r422266:989424 fatal user 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
>         >         > > <david.m.beer@googlemail.com> 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
>         >         > >
>         >         > >
>         >         >
>         >         >
>         >
>         >
>         >
>         >
>         
>         
>         
> 



Mime
View raw message