diff options
Diffstat (limited to 'examples/org.eclipse.emf.teneo.tutorial/src/jpoxtutorial/QuickStart.java')
-rw-r--r-- | examples/org.eclipse.emf.teneo.tutorial/src/jpoxtutorial/QuickStart.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/examples/org.eclipse.emf.teneo.tutorial/src/jpoxtutorial/QuickStart.java b/examples/org.eclipse.emf.teneo.tutorial/src/jpoxtutorial/QuickStart.java new file mode 100644 index 000000000..33f18c6f3 --- /dev/null +++ b/examples/org.eclipse.emf.teneo.tutorial/src/jpoxtutorial/QuickStart.java @@ -0,0 +1,101 @@ +/* + * Copyright 2004-2006, Elver.org (http://www.elver.org). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jpoxtutorial; + +import java.util.Properties; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.example.library.Book; +import org.eclipse.example.library.BookCategory; +import org.eclipse.example.library.Library; +import org.eclipse.example.library.LibraryFactory; +import org.eclipse.example.library.LibraryPackage; +import org.eclipse.example.library.Writer; +import org.eclipse.emf.teneo.jpox.emf.JpoxDataStore; +import org.eclipse.emf.teneo.jpox.emf.JpoxHelper; +import org.jpox.PMFConfiguration; + +/** + * Quick Start Tutorial + * + * @author <a href="mailto:mtaal@elver.org">Martin Taal</a> + * @version $Revision: 1.1 $ +*/ +public class QuickStart { + + /** The main method */ + public static void main(String[] args) { + // the name of the database, this database should exist but does not need to contain tables + String dbName = "library"; + doQuickStart(dbName); //ignore return + } + + /** Methodn which can be called by others */ + public static JpoxDataStore doQuickStart(String dbName) { + // Now create and register a JpoxDataStore + String pmfName = "MyPMF"; // the name of the JpoxDataStore + + // db connection info, replace with your own database connection information + Properties properties = new Properties(); + properties.setProperty(PMFConfiguration.JDO_DATASTORE_DRIVERNAME_PROPERTY, "com.mysql.jdbc.Driver"); + properties.setProperty(PMFConfiguration.JDO_DATASTORE_URL_PROPERTY, "jdbc:mysql://127.0.0.1:3306/mylibrary"); + properties.setProperty(PMFConfiguration.JDO_DATASTORE_USERNAME_PROPERTY, "root"); + properties.setProperty(PMFConfiguration.JDO_DATASTORE_PASSWORD_PROPERTY, "root"); + + // create the data store + JpoxDataStore jpoxDataStore = JpoxHelper.INSTANCE.createRegisterDataStore(pmfName); + jpoxDataStore.setProperties(properties); + jpoxDataStore.setEPackages(new EPackage[]{LibraryPackage.eINSTANCE}); + jpoxDataStore.initialize(); + + // Now create a persistence manager and a transaction + PersistenceManager pm = jpoxDataStore.getPMF().getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + // Start a transaction, create a library and make it persistent + tx.begin(); + Library lib = LibraryFactory.eINSTANCE.createLibrary(); + lib.setName("My Library"); + pm.makePersistent(lib); + + // create a writer + Writer writer = LibraryFactory.eINSTANCE.createWriter(); + writer.setName("JRR Tolkien"); + + // and one of his books + Book book = LibraryFactory.eINSTANCE.createBook(); + book.setAuthor(writer); + book.setPages(305); + book.setTitle("The Hobbit"); + book.setCategory(BookCategory.SCIENCE_FICTION_LITERAL); + + // add the writer/book to the library. The writer and book are automatically + // made persistent because they are added to the library which is already + // made persistent + lib.getWriters().add(writer); + lib.getBooks().add(book); + + // at commit the objects will be present in the database + tx.commit(); + // and close of, this should actually be done in a finally block + pm.close(); + + return jpoxDataStore; + } +}
\ No newline at end of file |