|
Re: [CDO/Hibernate] How to re-save changed model instance [message #820963 is a reply to message #820948] |
Wed, 14 March 2012 19:53 |
Martin Taal 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 #821485 is a reply to message #821394] |
Thu, 15 March 2012 12:24 |
Martin Taal 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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03206 seconds