Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [CDO/Hibernate] How to re-save changed model instance
[CDO/Hibernate] How to re-save changed model instance [message #820948] Wed, 14 March 2012 19:28 Go to next message
Markus M is currently offline Markus MFriend
Messages: 3
Registered: March 2012
Junior Member
Hi,

I am new to CDO/Hibernate and currently I follow the cdo/hibernate quickstart guide from the CDO wiki. So I have downloaded the example projects and looked at and executed the test cases. Then I made the following modification to the QuickStartTest (after the first commit, the street name is changed again and should be saved):

public void testCreatePersist() throws Exception {
        // first create an address and persist it
        final String addressName = "name " + System.currentTimeMillis(); //$NON-NLS-1$
        {
            CDOSession session = openSession();
            CDOTransaction transaction = session.openTransaction();
            // get/create a resource
            final CDOResource resource = transaction.getOrCreateResource("/res1"); //$NON-NLS-1$

            // clear any previous data
            resource.getContents().clear();

            final Company address = CompanyFactory.eINSTANCE.createCompany();
            address.setCity("test"); //$NON-NLS-1$
            address.setName(addressName);
            address.setStreet("test"); //$NON-NLS-1$
            resource.getContents().add(address);

            transaction.commit();

            // change the street name again
            session = openSession();
            transaction = session.openTransaction();
            address.setStreet("street");
            resource.getContents().add(address);
            transaction.commit();
        }

        // read back and do some tests
        {
            final CDOSession session = openSession();
            final CDOTransaction transaction = session.openTransaction();
            final CDOResource resource = transaction.getResource("/res1"); //$NON-NLS-1$
            assertEquals(true, resource.getContents().get(0) instanceof Company);
            assertEquals(1, resource.getContents().size());
            final Company address = (Company) resource.getContents().get(0);
            assertEquals(addressName, address.getName());
            transaction.commit();
        }
    }


Thus the second change of the street name is not getting persisted in the db, it is still the old "test" name in the db after the test passes. What must I do that the second change gets recognized?

Additionally, I am wondering how to handle the CDOSession lifecycle. The Testcase-methods do not close the opened session. Is the closing done automatically?

Thanks & Regards
Markus
Re: [CDO/Hibernate] How to re-save changed model instance [message #820963 is a reply to message #820948] Wed, 14 March 2012 19:53 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi,
When you open the session/transaction to change the name you also need to read the resource again from that new
transaction, or commit a transaction on the original resource which has the address object.

Yes the testcases should also close the session, that's nicer.

gr. Martin

On 03/14/2012 08:28 PM, Markus M wrote:
> Hi,
>
> I am new to CDO/Hibernate and currently I follow the cdo/hibernate quickstart guide from the CDO wiki. So I have
> downloaded the example projects and looked at and executed the test cases. Then I made the following modification to the
> QuickStartTest (after the first commit, the street name is changed again and should be saved):
>
>
> public void testCreatePersist() throws Exception {
> // first create an address and persist it
> final String addressName = "name " + System.currentTimeMillis(); //$NON-NLS-1$
> {
> CDOSession session = openSession();
> CDOTransaction transaction = session.openTransaction();
> // get/create a resource
> final CDOResource resource = transaction.getOrCreateResource("/res1"); //$NON-NLS-1$
>
> // clear any previous data
> resource.getContents().clear();
>
> final Company address = CompanyFactory.eINSTANCE.createCompany();
> address.setCity("test"); //$NON-NLS-1$
> address.setName(addressName);
> address.setStreet("test"); //$NON-NLS-1$
> resource.getContents().add(address);
>
> transaction.commit();
>
> // change the street name again
> session = openSession();
> transaction = session.openTransaction();
> address.setStreet("street");
> resource.getContents().add(address);
> transaction.commit();
> }
>
> // read back and do some tests
> {
> final CDOSession session = openSession();
> final CDOTransaction transaction = session.openTransaction();
> final CDOResource resource = transaction.getResource("/res1"); //$NON-NLS-1$
> assertEquals(true, resource.getContents().get(0) instanceof Company);
> assertEquals(1, resource.getContents().size());
> final Company address = (Company) resource.getContents().get(0);
> assertEquals(addressName, address.getName());
> transaction.commit();
> }
> }
>
>
> Thus the second change of the street name is not getting persisted in the db, it is still the old "test" name in the db
> after the test passes. What must I do that the second change gets recognized?
>
> Additionally, I am wondering how to handle the CDOSession lifecycle. The Testcase-methods do not close the opened
> session. Is the closing done automatically?
>
> Thanks & Regards
> Markus
>


--

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: [CDO/Hibernate] How to re-save changed model instance [message #820967 is a reply to message #820963] Wed, 14 March 2012 20:00 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
You are opening a second session/transaction, but referring to an object of the first transaction. I.e., Your second commit does not have any effects as you are not modifying any objects of that transaction...
Re: [CDO/Hibernate] How to re-save changed model instance [message #821394 is a reply to message #820967] Thu, 15 March 2012 09:51 Go to previous messageGo to next message
Markus M is currently offline Markus MFriend
Messages: 3
Registered: March 2012
Junior Member
Hi Martin and Erdal,

thanks for your clarifications, I get the point now. However I am still struggling how to cope with it. For example, I am unsure how to implement DAO / Persistencemanager-Classes like the following demo class:

public class PersistenceManager {

    public void saveCompany(final Company company) {
        final CDOSession session = openSession();
        final CDOTransaction transaction = session.openTransaction();
        final CDOResource resource = transaction.getOrCreateResource(RESOURCE_NAME);

        resource.getContents().add(company);

        try {
            transaction.commit();
        } catch (final CommitException e) {
            throw new RuntimeException(e);
        }

    }

    // .... other read/write methods like the above...

    // all this should move into a persistence manager base class...

    private static final String REPO_NAME = "repo1"; //$NON-NLS-1$

    private static final String RESOURCE_NAME = "res1"; //$NON-NLS-1$

    private static final String CONNECTION_ADDRESS = "localhost:2036"; //$NON-NLS-1$

    private CDOSessionConfiguration sessionConfiguration = null;

    /**
     * Opens a CDOSession, does not register an EPackage with the session. This should be done by the caller.
     */
    private CDOSession openSession() {
        if (sessionConfiguration == null) {
            initialize();
        }
        final CDOSession cdoSession = sessionConfiguration.openSession();
        cdoSession.getPackageRegistry().putEPackage(CompanyPackage.eINSTANCE);
        return cdoSession;
    }

    /**
     * Initializes the connection and creates a {@link CDOSessionConfiguration} which is stored in a member of this
     * class.
     */
    private void initialize() {
        OMPlatform.INSTANCE.setDebugging(true);
        OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
        OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);

        // Prepare container
        final IManagedContainer container = ContainerUtil.createContainer();
        Net4jUtil.prepareContainer(container); // Register Net4j factories
        TCPUtil.prepareContainer(container); // Register TCP factories
        CDONet4jUtil.prepareContainer(container); // Register CDO factories
        // LifecycleUtil.activate(container);
        container.activate();

        // Create connector
        final IConnector connector = TCPUtil.getConnector(container, CONNECTION_ADDRESS);

        // Create configuration
        sessionConfiguration = CDONet4jUtil.createSessionConfiguration();
        sessionConfiguration.setConnector(connector);
        sessionConfiguration.setRepositoryName(REPO_NAME);
    }

}


So that the saveCompany-Method still works, when the same (changed) model instance is passed in a second time, I would have to either remember the transaction of model instance and reuse it instead of creating a new one. Or the other alternative would be to get another model instance from the resource (companyb), copy the values from the model instance that has been passed in from the caller of saveCompany(...) to companyb instance and save companyb instance with getContents().add().
Is there a better way to do it?

Thanks & Regards
Markus
Re: [CDO/Hibernate] How to re-save changed model instance [message #821485 is a reply to message #821394] Thu, 15 March 2012 12:24 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Markus,
CDO is strongly based on the resource concept, the resource can be seen as a hibernate session. It fits less to a dao
pattern where you open and close a transaction for a short period of time. CDO is much stronger if you have objects in
memory longer periods of time. So assuming you are doing a rcp, a resource is linked to a visualization (a grid or edit
form for example) and lives as long as that visualization is shown to the user. So not soo much for doing short dao-like
operations.

If you are doing a non-rcp app (for example a web app) then I would consider the Texo project:
http://wiki.eclipse.org/Texo

gr. Martin

On 03/15/2012 10:51 AM, Markus M wrote:
> Hi Martin and Erdal,
>
> thanks for your clarifications, I get the point now. However I am still struggling how to cope with it. For example, I
> am unsure how to implement DAO / Persistencemanager-Classes like the following demo class:
>
>
> public class PersistenceManager {
>
> public void saveCompany(final Company company) {
> final CDOSession session = openSession();
> final CDOTransaction transaction = session.openTransaction();
> final CDOResource resource = transaction.getOrCreateResource(RESOURCE_NAME);
>
> resource.getContents().add(company);
>
> try {
> transaction.commit();
> } catch (final CommitException e) {
> throw new RuntimeException(e);
> }
>
> }
>
> // .... other read/write methods like the above...
>
> // all this should move into a persistence manager base class...
>
> private static final String REPO_NAME = "repo1"; //$NON-NLS-1$
>
> private static final String RESOURCE_NAME = "res1"; //$NON-NLS-1$
>
> private static final String CONNECTION_ADDRESS = "localhost:2036"; //$NON-NLS-1$
>
> private CDOSessionConfiguration sessionConfiguration = null;
>
> /**
> * Opens a CDOSession, does not register an EPackage with the session. This should be done by the caller.
> */
> private CDOSession openSession() {
> if (sessionConfiguration == null) {
> initialize();
> }
> final CDOSession cdoSession = sessionConfiguration.openSession();
> cdoSession.getPackageRegistry().putEPackage(CompanyPackage.eINSTANCE);
> return cdoSession;
> }
>
> /**
> * Initializes the connection and creates a {@link CDOSessionConfiguration} which is stored in a member of this
> * class.
> */
> private void initialize() {
> OMPlatform.INSTANCE.setDebugging(true);
> OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
> OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);
>
> // Prepare container
> final IManagedContainer container = ContainerUtil.createContainer();
> Net4jUtil.prepareContainer(container); // Register Net4j factories
> TCPUtil.prepareContainer(container); // Register TCP factories
> CDONet4jUtil.prepareContainer(container); // Register CDO factories
> // LifecycleUtil.activate(container);
> container.activate();
>
> // Create connector
> final IConnector connector = TCPUtil.getConnector(container, CONNECTION_ADDRESS);
>
> // Create configuration
> sessionConfiguration = CDONet4jUtil.createSessionConfiguration();
> sessionConfiguration.setConnector(connector);
> sessionConfiguration.setRepositoryName(REPO_NAME);
> }
>
> }
>
>
> So that the saveCompany-Method still works, when the same (changed) model instance is passed in a second time, I would
> have to either remember the transaction of model instance and reuse it instead of creating a new one. Or the other
> alternative would be to get another model instance from the resource (companyb), copy the values from the model instance
> that has been passed in from the caller of saveCompany(...) to companyb instance and save companyb instance with
> getContents().add(). Is there a better way to do it?
> Thanks & Regards
> Markus


--

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: [CDO/Hibernate] How to re-save changed model instance [message #821599 is a reply to message #821485] Thu, 15 March 2012 15:31 Go to previous messageGo to next message
Markus M is currently offline Markus MFriend
Messages: 3
Registered: March 2012
Junior Member
Hi Martin,

thanks for the additional info on how CDO should be used. Is there any sample project that shows, how to properly apply the resource concept in rcp applications?

Regards
Markus
Re: [CDO/Hibernate] How to re-save changed model instance [message #821614 is a reply to message #821599] Thu, 15 March 2012 15:56 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Markus,
I don't do rcp's myself... but I know that Tom Schindl has done blog posts on this topic and on databinding, maybe you
can search for that on the net...

gr. Martin

On 03/15/2012 04:31 PM, Markus M wrote:

> Hi Martin,
>
> thanks for the additional info on how CDO should be used. Is there any sample project that shows, how to properly apply
> the resource concept in rcp applications?
> Regards
> Markus


--

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
Previous Topic:Additional Classes
Next Topic:[CDO] Metamodels in CDO & exceptions
Goto Forum:
  


Current Time: Fri Apr 26 04:35:15 GMT 2024

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

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

Back to the top