Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Teneo-hibernate:child object is in an other resource
Teneo-hibernate:child object is in an other resource [message #498103] Sat, 14 November 2009 14:10 Go to next message
Allon Moritz is currently offline Allon MoritzFriend
Messages: 38
Registered: July 2009
Member
hi
first I'm pretty new in the world of teneo and hibernate but I was searching now for quite some time for a solution in the web. here is my problem:
I have an emf object of type Product loaded in resource A. this object has an attribute PriceCategory.
An other resource B is loading all PriceCategories which I have in my database.
the thing is when I call product.getPriceCategory() it creates a new instance of a PriceCategory object. now I have two objects of PriceCategory in my memory, one attached to resource B and the other to a Product object. but I want to have just one PriceCategory object......
I'm sure I miss something because this problem is sooner or later facing every developer using teneo and hibernate. everything else is working as expected Smile

thanks for any help
Re: Teneo-hibernate:child object is in an other resource [message #498105 is a reply to message #498103] Sat, 14 November 2009 14:25 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5339
Registered: July 2009
Senior Member
Hi Allon,
To get the two resources working together like this you have to let them share a session:
http://www.elver.org/hibernate/hibernateresources.html#Shari ng+one+Session+between+Resources%2C+One+transaction+when+sav ing+multiple+resources
(note url can be broken in separate lines as it is pretty long).

btw, you don't need to use resources to read/save objects from the database, you can also use plain hibernate or jpa.

gr. Martin

Allon Moritz wrote:
> hi
> first I'm pretty new in the world of teneo and hibernate but I was
> searching now for quite some time for a solution in the web. here is my
> problem:
> I have an emf object of type Product loaded in resource A. this object
> has an attribute PriceCategory.
> An other resource B is loading all PriceCategories which I have in my
> database.
> the thing is when I call product.getPriceCategory() it creates a new
> instance of a PriceCategory object. now I have two objects of
> PriceCategory in my memory, one attached to resource B and the other to
> a Product object. but I want to have just one PriceCategory object......
> I'm sure I miss something because this problem is sooner or later facing
> every developer using teneo and hibernate. everything else is working as
> expected :)
> thanks for any help


--

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@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: Teneo-hibernate:child object is in an other resource [message #498113 is a reply to message #498105] Sat, 14 November 2009 15:55 Go to previous messageGo to next message
Allon Moritz is currently offline Allon MoritzFriend
Messages: 38
Registered: July 2009
Member
I read this article but I'm not sure if i do understand it right.

my problem is that an object pricecategory is created two times. from the object product and as child in the resource which loads all pricecategories.....the problem comes when I edit in my RCP app a pricecategory, which is a child in the resource which loads all pricecategories. the pricecategory object in the product object is not updated because it is another instance (f..... this is complicated to describe)

Quote:

btw, you don't need to use resources to read/save objects from the database, you can also use plain hibernate or jpa.


I'm using emf and teneo to store eObjects so I need resources.....
Re: Teneo-hibernate:child object is in an other resource [message #498114 is a reply to message #498113] Sat, 14 November 2009 16:10 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5339
Registered: July 2009
Senior Member
Hi Allon,
As a default a resource will have its own Hibernate session. This means that if you load things in one resource and then
load another resource that Hibernate/Teneo will not relate objects in the two resources. To get one resource to use data
from another resource then you need to let them share a Hibernate session.

Here is an example of loading and updating EObjects with just direct Hibernate:
http://www.elver.org/hibernate/tutorialone/tutorial1_3.html
(the same is possible using a JPA EntityManager).

gr. Martin

Allon Moritz wrote:
> I read this article but I'm not sure if i do understand it right.
>
> my problem is that an object pricecategory is created two times. from
> the object product and as child in the resource which loads all
> pricecategories.....the problem comes when I edit in my RCP app a
> pricecategory, which is a child in the resource which loads all
> pricecategories. the pricecategory object in the product object is not
> updated because it is another instance (f..... this is complicated to
> describe)
>
> Quote:
>> btw, you don't need to use resources to read/save objects from the
>> database, you can also use plain hibernate or jpa.
>
> I'm using emf and teneo to store eObjects so I need resources.....


--

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@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: Teneo-hibernate:child object is in an other resource [message #498116 is a reply to message #498114] Sat, 14 November 2009 16:40 Go to previous messageGo to next message
Allon Moritz is currently offline Allon MoritzFriend
Messages: 38
Registered: July 2009
Member
I think some code will bring some ligth into my problem....

I have a static method creating a session controller:
public static synchronized SessionController getSessionController() {
		if (sessionController == null) {
			try {
				NetworkServerControl server = new NetworkServerControl();
				server.start(null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			Properties props = new Properties();
			props.setProperty(Environment.DRIVER,"org.apache.derby.jdbc.ClientDriver");
			props.setProperty(Environment.URL,"jdbc:derby://localhost:1527/ShopDB;create=true");
			props.setProperty(Environment.DIALECT,org.hibernate.dialect.DerbyDialect.class.getName());
			props.setProperty(Environment.SHOW_SQL, "true");
			HbDataStore hbds = (HbDataStore) HbHelper.INSTANCE
					.createRegisterDataStore("ShopDB");
			hbds.setProperties(props);
			hbds.setEPackages(new EPackage[] { ShopPackage.eINSTANCE });
			hbds.initialize();

			sessionController = new SessionController();
			sessionController.setHbDataStore(hbds);
			SessionController.registerSessionController("ShopDB",
					sessionController);
		}
		return sessionController;
	}


the code to create a resource:
private static Resource createResource(String type) {
getSessionController().getSessionWrapper()
				.beginTransaction();
		String uriStr = "hibernate://?" + HibernateResource.DS_NAME_PARAM
				+ "=ShopDB&query1=FROM " + type;
		URI uri = URI.createURI(uriStr);
		final Resource res = new ResourceSetImpl().getResource(uri, true);
		getSessionController().getSessionWrapper()
				.commitTransaction();
		return res;
}


and here is where the problem comes (assumption: there is one product with a link to the only pricecategory in the table pricecategory):
PRICE_CATEGORIES_RESOURCE = createResource("PriceCategory");
PriceCategory  cat0 = (PriceCategory)PRICE_CATEGORIES_RESOURCE.getContents().get(0);
PRODUCTS_RESOURCE = createResource("Product ");
Product prod = (Product)PRODUCTS_RESOURCE.getContents().get(0);
PriceCategory cat1 = prod.getPriceCategory();


now cat1 and cat0 are not the same object........I understand that they are not the same but how can I connect prod with cat0???

really thank you for your patience Very Happy
Re: Teneo-hibernate:child object is in an other resource [message #498169 is a reply to message #498116] Sun, 15 November 2009 14:19 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5339
Registered: July 2009
Senior Member
Hi Allon,
You have to add the session controller name to the uri otherwise the resources can't find it.

See the usage of the HibernateResource.SESSION_CONTROLLER_PARAM in this description:
http://www.elver.org/hibernate/hibernateresources.html#Shari ng+one+Session+between+Resources%2C+One+transaction+when+sav ing+multiple+resources

gr. Martin

Allon Moritz wrote:
> I think some code will bring some ligth into my problem....
>
> I have a static method creating a session controller:
>
> public static synchronized SessionController getSessionController() {
> if (sessionController == null) {
> try {
> NetworkServerControl server = new NetworkServerControl();
> server.start(null);
> } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> Properties props = new Properties();
>
> props.setProperty(Environment.DRIVER,"org.apache.derby.jdbc.ClientDriver ");
>
> props.setProperty(Environment.URL,"jdbc:derby://localhost:1527/ShopDB;create=true");
>
>
> props.setProperty(Environment.DIALECT,org.hibernate.dialect. DerbyDialect.class.getName());
>
> props.setProperty(Environment.SHOW_SQL, "true");
> HbDataStore hbds = (HbDataStore) HbHelper.INSTANCE
> .createRegisterDataStore("ShopDB");
> hbds.setProperties(props);
> hbds.setEPackages(new EPackage[] { ShopPackage.eINSTANCE });
> hbds.initialize();
>
> sessionController = new SessionController();
> sessionController.setHbDataStore(hbds);
> SessionController.registerSessionController("ShopDB",
> sessionController);
> }
> return sessionController;
> }
>
>
> the code to create a resource:
>
> private static Resource createResource(String type) {
> getSessionController().getSessionWrapper()
> .beginTransaction();
> String uriStr = "hibernate://?" + HibernateResource.DS_NAME_PARAM
> + "=ShopDB&query1=FROM " + type;
> URI uri = URI.createURI(uriStr);
> final Resource res = new ResourceSetImpl().getResource(uri, true);
> getSessionController().getSessionWrapper()
> .commitTransaction();
> return res;
> }
>
>
> and here is where the problem comes (assumption: there is one product
> with a link to the only pricecategory in the table pricecategory):
>
> PRICE_CATEGORIES_RESOURCE = createResource("PriceCategory");
> PriceCategory cat0 =
> (PriceCategory)PRICE_CATEGORIES_RESOURCE.getContents().get(0 );
> PRODUCTS_RESOURCE = createResource("Product ");
> Product prod = (Product)PRODUCTS_RESOURCE.getContents().get(0);
> PriceCategory cat1 = prod.getPriceCategory();
>
>
> now cat1 and cat0 are not the same object........I understand that they
> are not the same but how can I connect prod with cat0???
>
> really thank you for your patience :d


--

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@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: Teneo-hibernate:child object is in an other resource [message #498182 is a reply to message #498169] Sun, 15 November 2009 16:28 Go to previous message
Allon Moritz is currently offline Allon MoritzFriend
Messages: 38
Registered: July 2009
Member
this did it!!!!! thanks a lot....... Laughing Laughing Laughing Laughing

I knew I missed something...great product by the way

[Updated on: Sun, 15 November 2009 16:29]

Report message to a moderator

Previous Topic:generating an ecore file/model from an existing POJO web project
Next Topic:Usage of generated editor - DSL - relationship
Goto Forum:
  


Current Time: Fri Nov 28 14:48:05 GMT 2014

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

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