cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r419088 - in /incubator/cayenne/examples/trunk/generic-mapping-example: ./ lib/ src/ src/cayenne/ src/cayenne/example/ src/cayenne/example/generic/
Date Tue, 04 Jul 2006 20:34:48 GMT
Author: aadamchik
Date: Tue Jul  4 13:34:47 2006
New Revision: 419088

URL: http://svn.apache.org/viewvc?rev=419088&view=rev
Log:
checking in some examples already shown on Wiki, that are not intended for official release.

Added:
    incubator/cayenne/examples/trunk/generic-mapping-example/.classpath
    incubator/cayenne/examples/trunk/generic-mapping-example/.project
    incubator/cayenne/examples/trunk/generic-mapping-example/README.txt   (with props)
    incubator/cayenne/examples/trunk/generic-mapping-example/lib/
    incubator/cayenne/examples/trunk/generic-mapping-example/lib/cayenne-1.2RC2.jar   (with
props)
    incubator/cayenne/examples/trunk/generic-mapping-example/lib/hsqldb-1.8.0.2.jar   (with
props)
    incubator/cayenne/examples/trunk/generic-mapping-example/src/
    incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne/
    incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne.xml
    incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne/example/
    incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne/example/generic/
    incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne/example/generic/Main.java
    incubator/cayenne/examples/trunk/generic-mapping-example/src/generic-example-map.map.xml
    incubator/cayenne/examples/trunk/generic-mapping-example/src/in-memory-node.driver.xml

Added: incubator/cayenne/examples/trunk/generic-mapping-example/.classpath
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/.classpath?rev=419088&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/generic-mapping-example/.classpath (added)
+++ incubator/cayenne/examples/trunk/generic-mapping-example/.classpath Tue Jul  4 13:34:47
2006
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="lib" path="lib/hsqldb-1.8.0.2.jar"/>
+	<classpathentry kind="lib" path="lib/cayenne-1.2RC2.jar"/>
+	<classpathentry kind="output" path="build"/>
+</classpath>

Added: incubator/cayenne/examples/trunk/generic-mapping-example/.project
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/.project?rev=419088&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/generic-mapping-example/.project (added)
+++ incubator/cayenne/examples/trunk/generic-mapping-example/.project Tue Jul  4 13:34:47
2006
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>generic-mapping-example</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

Added: incubator/cayenne/examples/trunk/generic-mapping-example/README.txt
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/README.txt?rev=419088&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/generic-mapping-example/README.txt (added)
+++ incubator/cayenne/examples/trunk/generic-mapping-example/README.txt Tue Jul  4 13:34:47
2006
@@ -0,0 +1,13 @@
+Copyright 2005, Andrus Adamchik. 
+This example is distributed under the terms of ObjectStyle open source license.
+
+This example shows how to use "generic" entities (i.e. those not mapped to a 
+specific Java class). It shows how to map them and also how to extend them in 
+runtime.
+
+
+HOW TO RUN:
+
+This example is self-contained Eclipse project, including Cayenne 1.2M9 jar. 
+Just import it in Eclipse (3.1 or newer is recommended) and run "cayenne.example.generic.Main"

+class as "Java Application". As in-memory HSQLDB is used, no extra DB setup is required.
\ No newline at end of file

Propchange: incubator/cayenne/examples/trunk/generic-mapping-example/README.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/cayenne/examples/trunk/generic-mapping-example/lib/cayenne-1.2RC2.jar
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/lib/cayenne-1.2RC2.jar?rev=419088&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/cayenne/examples/trunk/generic-mapping-example/lib/cayenne-1.2RC2.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/cayenne/examples/trunk/generic-mapping-example/lib/hsqldb-1.8.0.2.jar
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/lib/hsqldb-1.8.0.2.jar?rev=419088&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/cayenne/examples/trunk/generic-mapping-example/lib/hsqldb-1.8.0.2.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne.xml?rev=419088&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne.xml (added)
+++ incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne.xml Tue Jul  4 13:34:47
2006
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<domains project-version="1.1">
+<domain name="generic-example">
+	<map name="generic-example-map" location="generic-example-map.map.xml"/>
+
+	<node name="in-memory-node"
+		 datasource="in-memory-node.driver.xml"
+		 factory="org.objectstyle.cayenne.conf.DriverDataSourceFactory">
+			<map-ref name="generic-example-map"/>
+	 </node>
+</domain>
+</domains>

Added: incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne/example/generic/Main.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne/example/generic/Main.java?rev=419088&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne/example/generic/Main.java
(added)
+++ incubator/cayenne/examples/trunk/generic-mapping-example/src/cayenne/example/generic/Main.java
Tue Jul  4 13:34:47 2006
@@ -0,0 +1,143 @@
+package cayenne.example.generic;
+
+import java.sql.Types;
+import java.util.Iterator;
+import java.util.List;
+
+import org.objectstyle.cayenne.CayenneRuntimeException;
+import org.objectstyle.cayenne.DataObject;
+import org.objectstyle.cayenne.access.DataContext;
+import org.objectstyle.cayenne.access.DataNode;
+import org.objectstyle.cayenne.access.DbGenerator;
+import org.objectstyle.cayenne.exp.Expression;
+import org.objectstyle.cayenne.exp.ExpressionFactory;
+import org.objectstyle.cayenne.map.DataMap;
+import org.objectstyle.cayenne.map.DbAttribute;
+import org.objectstyle.cayenne.map.DbEntity;
+import org.objectstyle.cayenne.map.ObjAttribute;
+import org.objectstyle.cayenne.map.ObjEntity;
+import org.objectstyle.cayenne.query.SelectQuery;
+
+/**
+ * An example launcher. Uses in-memory HSQLDB for all database operations.
+ * 
+ * @author Andrus Adamchik
+ */
+public class Main {
+
+    DataContext context;
+    DataMap orMapping;
+
+    public static void main(String[] args) {
+
+        Main example = new Main();
+
+        // dynamically change mapping to add more attributes
+        example.extendMapping();
+
+        // create HSQLDB schema
+
+        // in a real application a smarter approach to schema update may be needed, that
+        // only loads new changes instead of rebuilding the full DB.
+        example.generateDBSchema();
+
+        // create/save/query some objects
+        example.workWithGenericObjects();
+    }
+
+    Main() {
+        context = DataContext.createDataContext();
+        orMapping = context.getEntityResolver().getDataMap("generic-example-map");
+    }
+
+    /**
+     * Dynamically add two new attributes to the "message" entity - "topic" and
+     * "category".
+     */
+    void extendMapping() {
+        DbEntity messageTable = orMapping.getDbEntity("message");
+
+        DbAttribute topicColumn = new DbAttribute("topic", Types.VARCHAR, messageTable);
+        topicColumn.setMaxLength(200);
+        messageTable.addAttribute(topicColumn);
+
+        DbAttribute categoryColumn = new DbAttribute(
+                "category",
+                Types.VARCHAR,
+                messageTable);
+        categoryColumn.setMaxLength(200);
+        messageTable.addAttribute(categoryColumn);
+
+        ObjEntity messageEntity = orMapping.getObjEntity("Message");
+
+        ObjAttribute topicProperty = new ObjAttribute(
+                "topic",
+                String.class.getName(),
+                messageEntity);
+        topicProperty.setDbAttribute(topicColumn);
+        messageEntity.addAttribute(topicProperty);
+
+        ObjAttribute categoryProperty = new ObjAttribute("category", String.class
+                .getName(), messageEntity);
+        categoryProperty.setDbAttribute(categoryColumn);
+        messageEntity.addAttribute(categoryProperty);
+    }
+
+    /**
+     * Creates a DataMap on the fly, generating DB schema from it.
+     */
+    void generateDBSchema() {
+        DataNode db = context.getParentDataDomain().lookupDataNode(orMapping);
+        DbGenerator generator = new DbGenerator(db.getAdapter(), orMapping);
+
+        try {
+            generator.runGenerator(db.getDataSource());
+        }
+        catch (Exception e) {
+            throw new CayenneRuntimeException("Error generating schema", e);
+        }
+    }
+
+    void workWithGenericObjects() {
+
+        // 1. create and save a new author and a few messages....
+        DataObject author = context.createAndRegisterNewObject("Person");
+        author.writeProperty("firstName", "Joe");
+        author.writeProperty("lastName", "Doe");
+
+        DataObject firstPost = context.createAndRegisterNewObject("Message");
+        firstPost.writeProperty("subject", "First POST!!");
+        firstPost.writeProperty("text", "... read the subject ....");
+
+        // just like with "normal" DataObjects, this call will set reverse relationship as
+        // well
+        firstPost.writeProperty("author", author);
+
+        // now set post attributes that where generated dynamically on startup
+        firstPost.writeProperty("topic", "off-topic");
+        firstPost.writeProperty("category", "technical discussion");
+
+        DataObject anotherPost = context.createAndRegisterNewObject("Message");
+        anotherPost.writeProperty("subject", "Post On Topic");
+        anotherPost.writeProperty("text", "bla-bla-bla");
+        anotherPost.writeProperty("author", author);
+
+        context.commitChanges();
+
+        // 2. Query database. Note that entity name (not Class) must be used as query
+        // root.
+
+        Expression e = ExpressionFactory.likeIgnoreCaseExp("subject", "%first%");
+        SelectQuery q = new SelectQuery("Message", e);
+        List spam = context.performQuery(q);
+
+        Iterator it = spam.iterator();
+        while (it.hasNext()) {
+            DataObject spamMessage = (DataObject) it.next();
+            System.out.println("*** Got fp spam: "
+                    + spamMessage.readProperty("subject")
+                    + " by "
+                    + spamMessage.readNestedProperty("author.lastName"));
+        }
+    }
+}

Added: incubator/cayenne/examples/trunk/generic-mapping-example/src/generic-example-map.map.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/src/generic-example-map.map.xml?rev=419088&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/generic-mapping-example/src/generic-example-map.map.xml
(added)
+++ incubator/cayenne/examples/trunk/generic-mapping-example/src/generic-example-map.map.xml
Tue Jul  4 13:34:47 2006
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map project-version="1.1">
+	<db-entity name="message">
+		<db-attribute name="author_id" type="INTEGER"/>
+		<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+		<db-attribute name="subject" type="VARCHAR" length="200"/>
+		<db-attribute name="text" type="VARCHAR" length="1000"/>
+	</db-entity>
+	<db-entity name="person">
+		<db-attribute name="first_name" type="VARCHAR" length="200"/>
+		<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+		<db-attribute name="last_name" type="VARCHAR" length="200"/>
+	</db-entity>
+	<obj-entity name="Message" dbEntityName="message">
+		<obj-attribute name="subject" type="java.lang.String" db-attribute-path="subject"/>
+		<obj-attribute name="text" type="java.lang.String" db-attribute-path="text"/>
+	</obj-entity>
+	<obj-entity name="Person" dbEntityName="person">
+		<obj-attribute name="firstName" type="java.lang.String" db-attribute-path="first_name"/>
+		<obj-attribute name="lastName" type="java.lang.String" db-attribute-path="last_name"/>
+	</obj-entity>
+	<db-relationship name="author" source="message" target="person" toMany="false">
+		<db-attribute-pair source="author_id" target="id"/>
+	</db-relationship>
+	<db-relationship name="posts" source="person" target="message" toMany="true">
+		<db-attribute-pair source="id" target="author_id"/>
+	</db-relationship>
+	<obj-relationship name="author" source="Message" target="Person" deleteRule="Nullify"
db-relationship-path="author"/>
+	<obj-relationship name="posts" source="Person" target="Message" deleteRule="Cascade"
db-relationship-path="posts"/>
+</data-map>

Added: incubator/cayenne/examples/trunk/generic-mapping-example/src/in-memory-node.driver.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/generic-mapping-example/src/in-memory-node.driver.xml?rev=419088&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/generic-mapping-example/src/in-memory-node.driver.xml
(added)
+++ incubator/cayenne/examples/trunk/generic-mapping-example/src/in-memory-node.driver.xml
Tue Jul  4 13:34:47 2006
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<driver project-version="1.1" class="org.hsqldb.jdbcDriver">
+	<url value="jdbc:hsqldb:mem:generic-example"/>
+	<connectionPool min="1" max="1" />
+	<login userName="sa"/>
+</driver>



Mime
View raw message