Skip to main content
summaryrefslogtreecommitdiffstats
blob: e557236aaa3bfe06cfa35dcc9874b834d2c2e8f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal
 * </copyright>
 *
 * $Id: QuickStart.java,v 1.3 2006/09/13 10:39:55 mtaal Exp $
 */

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.JpoxDataStore;
import org.eclipse.emf.teneo.jpox.JpoxHelper;
import org.jpox.PMFConfiguration;

/**
 * Quick Start Tutorial
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.3 $
*/
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;
	}
}

Back to the top