Select aliases not working in OpenJPA 1.2.1
Hi, I am using the following OpenJPA version in an Eclipse RCP app: OpenJPA 1.2.1 version id: openjpa-1.2.1-r2180:4612 I receive errors when using aliases in a select statement: Example: select s.name, count(distinct g) as c from Systems s, IN(s.groups) g group by s.name order by c Error msg: Encountered as at character 34, but expected: [,, FROM].by c When I drop the alias and order by the count, as in the following example, select s.name, count(distinct g) from Systems s, IN(s.groups) g group by s.name order by count(g) the query is executed but the result is not ordered properly. Is this due to the use of distinct in the query, but not the ORDER? According to the query syntax, distinct cannot be used with ORDER. Is the use of aliases in the select as I describe here part of JPA 2.0 and therefore not supported in my OpenJPA 1.2.1? If so, what any suggestions on how to order on count of a distinct attribute? Thanks, Chris
Re: Cannot close EntityManager
If an exception occurs in remove(), the transaction is still open when the finally block is performed. Check whether the trans is active and roll it back if so, as in the following: } finally { if (em != null) { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } } -chris Cil-Gamir hannes.visa...@r mb.co.za To users@openjpa.apache.org 04/09/2010 09:23 cc AM Subject Cannot close EntityManager Please respond to us...@openjpa.apa che.org Hi I have this piece of code EntityManager em = emf.getEntityManager(); try { em.getTransaction().begin(); em.remove(kycRequestTempDocumentsToDelete); em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } I cannot seem to make that em.close call, it tells me that the transaction is still active. I use an em-per-request and that's the end of that request, so I would really like to close it. The application runs on tomcat and will likely not be restarted in months, is it ok to leave the em open, this method gets called thousands of times a day. Any advice on how to close it ? -- View this message in context: http://n2.nabble.com/Cannot-close-EntityManager-tp4875727p4875727.html Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: empty collections
Hi, Solved. Problem was that both sides of the bi-directional relationship were not being assigned before persisting, just one side : user.getGroups().add(group); The following fixed the problem: user.getGroups().add(group); group.getUsers().add(user); Interesting that by restarting the app, the relationships could be read, just not after persisting. Thanks, chris Hi, I have a bidirectional many-to-many relationship between two entities. Each entity references a collection of the other entity. I import data, then retrieve entities with a query. However, when I refer to an entity's collection, its size is always 0 (eg, group.getUsers().size() is 0). I look into the db and the join table is filled correctly. I restart the app, and then the same entity correctly returns the collection. That is, group.getUsers ().size() is now 0 and I can access the collection's elements. I am wondering is this is a known pattern and there is an a-ha out there. Otherwise OpenJPA is working great! Thanks, chris
empty collections
Hi, I have a bidirectional many-to-many relationship between two entities. Each entity references a collection of the other entity. I import data, then retrieve entities with a query. However, when I refer to an entity's collection, its size is always 0 (eg, group.getUsers().size () is 0). I look into the db and the join table is filled correctly. I restart the app, and then the same entity correctly returns the collection. That is, group.getUsers().size() is now 0 and I can access the collection's elements. I am wondering is this is a known pattern and there is an a-ha out there. Otherwise OpenJPA is working great! Thanks, chris
How to specify LRS in orm.xml?
Hi The OpenJPA describes how to indicate that a field should use OpenJPA's large result set proxies using the @LRS Java annotation. Can I specify LRS using orm.xml instead? Thanks,chris
Re: How to specify LRS in orm.xml?
Hi Pinaki, Thanks for the quick response. Why not define a separate OpenJPA XML schema? Then declare its namespace in orm.xml and one can use JPA and custom elements in the same document. The original JPA schema is untouched. -chris Hi, Any OpenJPA specific annotations are, unfortunately, not supported in XML descriptor. Because, JPA spec does not define a XML schema with a simple name,value mechanism for vendor extensions. OpenJPA can merge source code annotations with orm.xml -- so, one has to leave OpenJPA-specific mapping annotations on source code itself. Christopher Giblin wrote: Hi The OpenJPA describes how to indicate that a field should use OpenJPA's large result set proxies using the @LRS Java annotation. Can I specify LRS using orm.xml instead? Thanks,chris
noob: NoSuchElementException when using FetchBatchSize
Hi, I have OpenJPA 1.2.1, Derby 10.3 and deal with large result sets. When I configure openjpa.FetchBatchSize to any number, 0, 1000, whatever, I get the following exception: java.util.NoSuchElementException: The result list has been closed. at org.apache.openjpa.lib.rop.AbstractResultList.assertOpen( AbstractResultList.java:89) at org.apache.openjpa.lib.rop.WindowResultList.size( WindowResultList.java:84) at org.apache.openjpa.kernel.DelegatingResultList.size( DelegatingResultList.java:136) at com.ibm..SomeClass.test(SomeClass.java:30) Here are the persistence.xml properties: properties property name=openjpa.RuntimeUnenhancedClasses value=supported/ property name=openjpa.ConnectionDriverName value= org.apache.derby.jdbc.ClientDriver/ property name=openjpa.ConnectionUserName value=abc/ property name=openjpa.ConnectionPassword value=123/ property name=openjpa.ConnectionRetainMode value=always/ property name=openjpa.jdbc.DBDictionaryvalue= batchLimit=200/ property name=openjpa.FetchBatchSize value=0/ property name=openjpa.jdbc.ResultSetType value=scroll-sensitive/ property name=openjpa.jdbc.LRSSize value=last/ property name=openjpa.Log value=SQL=TRACE/ /properties I have tried various combinations of the properties, but always get the exception once FetchBatchSize is used. How can I avoid this exception? Thanks, chris
Re: influence connection usage?
Hi Mike, Thank you very, very much. That was exactly what I needed. Works perfectly. The OpenJPA documentation is excellent but substantial - I missed that. Thanks, chris Hi Christopher, Take a look at the openjpa.ConnectionRetainMode property [1] [2]. [1] http://openjpa.apache.org/builds/latest/docs/manual/ manual.html#openjpa.ConnectionRetainMode [2] http://openjpa.apache.org/builds/latest/docs/manual/ manual.html#ref_guide_dbsetup_retain It sounds like you'd benefit from setting it to 'always' or 'transaction'. Regards -mike On Tue, Sep 29, 2009 at 8:08 AM, Christopher Giblin c...@zurich.ibm.comwrote: Hi, Is there any way to influence OpenJPA's connection usage when not using connection pooling? I plan to move to connection pooling in the future but cannot quite at the moment. My OpenJPA is configured to use Derby ClientDataSource. When iterating over objects with lazy-loaded attributes, the query is naturally issued to fill the proxy as each object is accessed. However, I never get through the whole array because eventually the OS has too many TCP connections ( 2000) closing (in state TIME_WAIT) and can no load open any more connections. It seems each access opens and closes an DB connection and hence a TCP connection. I get partially around this by changing queries and using eager loading. But I cannot do this in all cases. I am wondering, is it possible to get OpenJPA to reuse a connection, rather than opening and closing so frequently? Thanks,chris
noob: understanding dynamic fetch modes
Hi, I have a class, A, with a one-to-many relationship to another class, B. Thus A refers to a collection of B. Normally I want to load the collection lazy and therefore set fetch to LAZY in my orm.xml. In one case, however, I want to load A eagerly. I thought the following would do the trick : Query query = em.createQuery(select m from ...); OpenJPAQuery jpaQ = OpenJPAPersistence.cast(query); JDBCFetchPlan fetchPlan = (JDBCFetchPlan) jpaQ.getFetchPlan(); fetchPlan.setEagerFetchMode(FetchMode.PARALLEL); ListIdentity results = query.getResultList(); but in the debugger, I see the collection is loaded lazily. I also tried using the property instead : query.setHint(openjpa.FetchPlan.EagerFetchMode,parallel); But this resulted in a IllegalArugmentException. What am I missing? Thanks,chris
relationship EntityManager and JDBC Connection?
Hi, Does an EntityManager instance correspond one-to-one to a JDBC connection? When the EM is closed, is the JDBC connection closed? That is, by opening and closing EMs, is one also opening and closing JDBC connections? I am using OpenJPA with Derby. There is no connection pooling: property name=openjpa.ConnectionDriverName value= org.apache.derby.jdbc.ClientDriver/ Sorry if I overlooked in the excellent documentation. Thanks,chris
Re: relationship EntityManager and JDBC Connection?
... more to the background of my problem : I create an EM instance and loop, calling persist() on perhaps 2,000 objects in batches of 50. I run netstat -an | grep 1527 and observe thousands of TCP sessions being open and closed. Eventually, there are almost 4,000 TCP sockets in TIME_WAIT state. Eventually java.net.BindException: Address already in use: connect is thrown which, from what read, is an OS limit on TCP connections being exhausted. Is there a way I can control the use of db connections and TCP sockets? Is the only way out a connection pool? Thanks,chris Does an EntityManager instance correspond one-to-one to a JDBC connection? When the EM is closed, is the JDBC connection closed? That is, by opening and closing EMs, is one also opening and closing JDBC connections? I am using OpenJPA with Derby. There is no connection pooling: property name=openjpa.ConnectionDriverName value= org.apache.derby.jdbc.ClientDriver/ Sorry if I overlooked in the excellent documentation.
Re: metadata api?
Thanks Jeremy, However, pk.getName() returns null as does fk[i].getName(). In the debugger, I see the table clsMapping.getTable() is the expected table. Also, pk is instantiated and fk[] has the right number of foreign keys. They just don't return their respective names, which is what I need. How do I retrieve the name of these keys? Or is this a bug? Thanks,chris Hi Chris, OpenJPA provides APIs that allow you to interrogate class mappings. Using a direct approach (OpenJPA resolves class mapping and metadata as part of creating an emf/em), this code gives you access to the primary and foreign keys of an entity via the schema information stored in the mapping. import org.apache.openjpa.jdbc.meta.ClassMapping; import org.apache.openjpa.jdbc.schema.Column; import org.apache.openjpa.jdbc.schema.ForeignKey; import org.apache.openjpa.jdbc.schema.PrimaryKey; import org.apache.openjpa.persistence.JPAFacadeHelper; ... EntityManager em = emf.createEntityManager(); ClassMapping clsMapping = (ClassMapping)JPAFacadeHelper.getMetaData(em, SomeEntity.class); ForeignKey[] fks = clsMapping.getTable().getForeignKeys(); PrimaryKey pk = clsMapping.getTable().getPrimaryKey(); hth, -Jeremy On Wed, Sep 9, 2009 at 3:31 AM, Christopher Giblin c...@zurich.ibm.comwrote: Hi, I defined object-relational mappings in orm.xml. Works fine. Is an API available which allows querying of the orm definition for a given object? I am programming with reflection and would like to know, given an object, which of its attributes are primary or foreign keys. Thanks, chris
Re: metadata api?
Hi, If I go over the column, I get closer:: pk.getColumns()[0].getName() Wondering how I would get the attribute name instead of column name? In following example, how would I get groupId instead of group_id ? attributes id name=groupId column name=group_id/ generated-value strategy=IDENTITY/ /id ... -chris Thanks Jeremy, However, pk.getName() returns null as does fk[i].getName(). In the debugger, I see the table clsMapping.getTable() is the expected table. Also, pk is instantiated and fk[] has the right number of foreign keys. They just don't return their respective names, which is what I need. How do I retrieve the name of these keys? Or is this a bug? Thanks,chris Hi Chris, OpenJPA provides APIs that allow you to interrogate class mappings. Using a direct approach (OpenJPA resolves class mapping and metadata as part of creating an emf/em), this code gives you access to the primary and foreign keys of an entity via the schema information stored in the mapping. import org.apache.openjpa.jdbc.meta.ClassMapping; import org.apache.openjpa.jdbc.schema.Column; import org.apache.openjpa.jdbc.schema.ForeignKey; import org.apache.openjpa.jdbc.schema.PrimaryKey; import org.apache.openjpa.persistence.JPAFacadeHelper; ... EntityManager em = emf.createEntityManager(); ClassMapping clsMapping = (ClassMapping)JPAFacadeHelper.getMetaData(em, SomeEntity.class); ForeignKey[] fks = clsMapping.getTable().getForeignKeys(); PrimaryKey pk = clsMapping.getTable().getPrimaryKey(); hth, -Jeremy On Wed, Sep 9, 2009 at 3:31 AM, Christopher Giblin c...@zurich.ibm.comwrote: Hi, I defined object-relational mappings in orm.xml. Works fine. Is an API available which allows querying of the orm definition for a given object? I am programming with reflection and would like to know, given an object, which of its attributes are primary or foreign keys. Thanks, chris
metadata api?
Hi, I defined object-relational mappings in orm.xml. Works fine. Is an API available which allows querying of the orm definition for a given object? I am programming with reflection and would like to know, given an object, which of its attributes are primary or foreign keys. Thanks, chris
Newbie: sharing embedded Derby connection
Hi, I have just completed the first steps with OpenJPA running in my Eclipse RCP app. I use Derby with the EmbeddedDriver. So far so good. I have another set of plugins, Eclipse Data Tools Project (DTP), which provide some DB management tools. However, both OpenJPA and DTP want to own the Derby instance. When one has it, the other can not connect which is actually to be expected. When I close the EntityManagerFactory, it frees the db. But of course, I want to create that factory just once. I realize this is also a Derby question and equally a DTP question (I will ask there, too). Has anyone had to reconcile this before such that both could the embedded Derby? Thanks, chris
newbie: null Entity Manager Factory in Eclipse
Hi, When I run my test as a standalone Java program, things work. When I launch the same code from within a plugin in an Eclipse RCP app, Persistence.createEntityManagerFactory returns null. I have called Persistence.createEntityManagerFactory both ways, with and without persistence.xml. Get null both ways. I have googled and have done everything recommended in those posts, including initializing the factory with properties and making sure the jdbc driver is on the class path. Still get null. I feel I am now going round in circles. Any hints? Thanks, chris
Re: newbie: null Entity Manager Factory in Eclipse
Hi, Thanks for the interesting responses! In the end, I solved it by placing the openjpa jars in a subfolder of the plugin calling Persistence.createEntityManagerFactory. I then configured the plugin to add the jars to its classpath (manifest-runtime-claaspath- add). Also, persistence.xml must go into the plugin's manifest directory, not src/manifest as all the JPA tutorials show. I agree, returning null is not helpful. I would vote for an exception with an explanation. Thanks, chris Re: newbie: null Entity Manager Factory in Eclipse It sounds like your META-INF/persistence.xml file isn't found on your classpath when running your RCP application. I really wish that createEntityManagerFactory would do something other than return null if a persistence unit isn't found. I'd suggest taking a close look at the classpath for your RCP application. Hopefully this helps! -Rick On Mon, Jul 20, 2009 at 8:50 AM, Christopher Giblin c...@zurich.ibm.comwrote: Hi, When I run my test as a standalone Java program, things work. When I launch the same code from within a plugin in an Eclipse RCP app, Persistence.createEntityManagerFactory returns null. I have called Persistence.createEntityManagerFactory both ways, with and without persistence.xml. Get null both ways. I have googled and have done everything recommended in those posts, including initializing the factory with properties and making sure the jdbc driver is on the class path. Still get null. I feel I am now going round in circles. Any hints? Thanks, chris