Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Texo] Working with Text entities
[Texo] Working with Text entities [message #1111934] Wed, 18 September 2013 23:31 Go to next message
Miles Parker is currently offline Miles ParkerFriend
Messages: 1341
Registered: July 2009
Senior Member
Broad topic, but basically I'm having trouble figuring out how to interact directly with Texo objects. In EMF this is straightforward (hah, I can say that after working w/ EMF for eight years!), but it doesn't seem to obvious with Texo. I've read the wiki pages on dealing w/ DAO and Object store, but my EE foo is a little rusty to say the least. All I want is this basic recipe:

Get a set of objects (in EMF I'd start w/ the top-level resource content, but this doesn't seem appropriate for Texo), make some changes to them and then persist those changes. I can do all but the last part. Smile

I've tried the following plus a bunch of other stuff:

		final EntityManager entityManager = EntityManagerProvider.getInstance()
				.createEntityManager();
		entityManager.getTransaction().begin();
		final BaseDao<Product> productDao = DaoRegistry.getInstance().getDaoForEntity(Product.class);
		for (Product product : productDao.getAll()) {
			product.setSKU(12345);
			entityManager.persist(product);
		}
		entityManager.getTransaction().commit();
		EntityManagerProvider.getInstance().releaseEntityManager(entityManager);


But it just chokes on the call to persist, and if I don't do that, it appears that the model is never actually updated.

Any hints here would be greatly appreciated. Smile

-Miles
Re: [Texo] Working with Text entities [message #1112200 is a reply to message #1111934] Thu, 19 September 2013 08:38 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Miles,
The DAO and your code should use the same entitymanager, then they are in the same transaction. Explicitly calling
persist is only needed for new objects. There is a way to handle this through the EntityManagerProvider so that all the
daos in the same thread use the same entitymanager, somewhere in initialization code you need to do this:

EntityManagerProvider.getInstance().setUseCurrentEntityManagerPattern(true);

With this then your code becomes something like this:

// use getEntitymanager not createEntityManager
> final EntityManager entityManager = EntityManagerProvider.getInstance()
> .getEntityManager();
> entityManager.getTransaction().begin();
> final BaseDao<Product> productDao = DaoRegistry.getInstance().getDaoForEntity(Product.class);
> for (Product product : productDao.getAll()) {
> product.setSKU(12345);
> }
> entityManager.getTransaction().commit();
> EntityManagerProvider.getInstance().clearCurrentEntityManager();

With the webapps I use I always use the session-in-view pattern so one transaction/entitymanager by request and
commit/rollback and remove the entitymanager from the thread at the end of the request. This can all be done in a
request filter. Then in your application code you don't need to worry about transactions or commit.
Texo has an implementation of this in the CurrentEntityManagerRequestFilter class.

gr. Martin

On 09/19/2013 01:31 AM, Miles Parker wrote:
> Broad topic, but basically I'm having trouble figuring out how to interact directly with Texo objects. In EMF this is
> straightforward (hah, I can say that after working w/ EMF for eight years!), but it doesn't seem to obvious with Texo.
> I've read the wiki pages on dealing w/ DAO and Object store, but my EE foo is a little rusty to say the least. All I
> want is this basic recipe:
>
> Get a set of objects (in EMF I'd start w/ the top-level resource content, but this doesn't seem appropriate for Texo),
> make some changes to them and then persist those changes. I can do all but the last part. :)
>
> I've tried the following plus a bunch of other stuff:
>
>
> final EntityManager entityManager = EntityManagerProvider.getInstance()
> .createEntityManager();
> entityManager.getTransaction().begin();
> final BaseDao<Product> productDao = DaoRegistry.getInstance().getDaoForEntity(Product.class);
> for (Product product : productDao.getAll()) {
> product.setSKU(12345);
> entityManager.persist(product);
> }
> entityManager.getTransaction().commit();
> EntityManagerProvider.getInstance().releaseEntityManager(entityManager);
>
>
> But it just chokes on the call to persist, and if I don't do that, it appears that the model is never actually updated.
>
> Any hints here would be greatly appreciated. :)
>
> -Miles


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Texo] Working with Text entities [message #1112535 is a reply to message #1112200] Thu, 19 September 2013 21:10 Go to previous messageGo to next message
Miles Parker is currently offline Miles ParkerFriend
Messages: 1341
Registered: July 2009
Senior Member
Still no luck finding the secret recipe!

Here's what I've tried:

		final EntityManager entityManager = EntityManagerProvider.getInstance()
				.createEntityManager();
		entityManager.getTransaction().begin();
		final BaseDao<Product> productDao = DaoRegistry.getInstance().getDaoForEntity(Product.class);
		for (Product product : productDao.getAll()) {
			product.setSeverity3(100);
		}
		entityManager.getTransaction().commit();
		EntityManagerProvider.getInstance().clearCurrentEntityManager();


I think that's exactly what you've suggested below. One thing I noted was that the request filter was already set up because I copied the example project:

	<filter>
		<filter-name>emFilter</filter-name>
		<filter-class>org.eclipse.emf.texo.server.store.CurrentEntityManagerRequestFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>emFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


Thinking that might have been interfering somehow, I tried commenting it out. I also tried with the filter and then with and without the transaction calls. I've verified that the objects actually have their values changed, it just never gets written to the db.

I've tried both:

EntityManagerProvider.getInstance().releaseEntityManager(entityManager);

and

EntityManagerProvider.getInstance().clearCurrentEntityManager();


I'm calling all of this from my ModelBrowserServlet in response to a doGet.

Martin Taal wrote on Thu, 19 September 2013 04:38
Hi Miles,
The DAO and your code should use the same entitymanager, then they are in the same transaction. Explicitly calling
persist is only needed for new objects. There is a way to handle this through the EntityManagerProvider so that all the
daos in the same thread use the same entitymanager, somewhere in initialization code you need to do this:

EntityManagerProvider.getInstance().setUseCurrentEntityManagerPattern(true);

With this then your code becomes something like this:

// use getEntitymanager not createEntityManager
> final EntityManager entityManager = EntityManagerProvider.getInstance()
> .getEntityManager();
> entityManager.getTransaction().begin();
> final BaseDao<Product> productDao = DaoRegistry.getInstance().getDaoForEntity(Product.class);
> for (Product product : productDao.getAll()) {
> product.setSKU(12345);
> }
> entityManager.getTransaction().commit();
> EntityManagerProvider.getInstance().clearCurrentEntityManager();

With the webapps I use I always use the session-in-view pattern so one transaction/entitymanager by request and
commit/rollback and remove the entitymanager from the thread at the end of the request. This can all be done in a
request filter. Then in your application code you don't need to worry about transactions or commit.
Texo has an implementation of this in the CurrentEntityManagerRequestFilter class.

gr. Martin
Re: [Texo] Working with Text entities [message #1112772 is a reply to message #1112535] Fri, 20 September 2013 06:26 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Miles,
I think you missed this part of my code snippet:
// use getEntitymanager not createEntityManager
> final EntityManager entityManager = EntityManagerProvider.getInstance()
> .getEntityManager();

If there is a request filter then your application code should not do release/clear entitymanager (at least not before
committing the transaction explicitly). But in any case with the request filter in place the application code is much
simplere, something like this:

final BaseDao<Product> productDao = DaoRegistry.getInstance().getDaoForEntity(Product.class);
for (Product product : productDao.getAll()) {
product.setSKU(12345);
}

(the commit happens automatically at the end of the request)
(so then you don't need to work with the entitymanager if you only want to update, for deletion/insertion you need to
get the current entitymanager by doing EntityManagerProvider.getInstance().getEntityManager();

gr. Martin

On 09/19/2013 11:10 PM, Miles Parker wrote:
> // use getEntitymanager not createEntityManager
> > final EntityManager entityManager = EntityManagerProvider.getInstance()
> > .getEntityManager();


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Texo] Working with Text entities [message #1113283 is a reply to message #1112772] Fri, 20 September 2013 22:58 Go to previous message
Miles Parker is currently offline Miles ParkerFriend
Messages: 1341
Registered: July 2009
Senior Member
You're right. I was creating the entity manager rather than getting it. But still, no joy. So I'm wondering now if perhaps it has something to do w/ the Dao stuff. Perhaps there is something that I should have generated or annotated that I haven't?

Anyway not being certain, I decided to kick it old school style Very Happy based on the generate test data code and just did:

		final EntityManager entityManager = EntityManagerProvider.getInstance()
				.getEntityManager();
		entityManager.getTransaction().begin();
		List resultList = entityManager.createQuery("Select x from mypackage_Product As x").getResultList();
		for (Object object : resultList) {
			Product product = (Product) object;
			product.setSKU(12345);
		}

		entityManager.getTransaction().commit();
		EntityManagerProvider.getInstance().releaseEntityManager(entityManager);


That worked! Smile

Minor nit, but it still didn't work at first because of a an oddity with JPA and the Texo generation. I kept getting "Error compiling the query [Select x from mypackage_product As x]. Unknown entity type [mypackage_product].". Almost gave up, but then decided to look at the generated class. There we have:

@Entity(name = "mypackage_Product")
public class Product extends Identifiable {


Note the case! Now, in some sense, this makes sense since by convention packages are lower case and classes are upper case. But, most SQL dbs are case insensitive, and in fact Postgres sets all ids to pure lower-case. though I tried both "mypackage_product" and "Product" it wouldn't have occurred to me to try "mypackage_Product" or even that it mattered. (Why does JPA care when noone else does!) I'm not sure what if anything you could do in Texo to reduce the chances of this biting anyone else, but hopefully someone in the same situation will find this forum message. Smile

[Updated on: Fri, 20 September 2013 22:59]

Report message to a moderator

Previous Topic:[TENEO] Exception in HibernateDatastore
Next Topic:Visible or Invisible Properties When model changed
Goto Forum:
  


Current Time: Fri Mar 29 02:05:49 GMT 2024

Powered by FUDForum. Page generated in 0.05536 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top