cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r419083 - in /incubator/cayenne/examples/trunk/cross-db-example: ./ lib/ src/ src/test/ src/test/db1/ src/test/db1/auto/ src/test/db2/ src/test/db2/auto/
Date Tue, 04 Jul 2006 20:28:20 GMT
Author: aadamchik
Date: Tue Jul  4 13:28:18 2006
New Revision: 419083

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

Added:
    incubator/cayenne/examples/trunk/cross-db-example/.classpath
    incubator/cayenne/examples/trunk/cross-db-example/.project
    incubator/cayenne/examples/trunk/cross-db-example/README.txt   (with props)
    incubator/cayenne/examples/trunk/cross-db-example/lib/
    incubator/cayenne/examples/trunk/cross-db-example/lib/cayenne-1.2RC2.jar   (with props)
    incubator/cayenne/examples/trunk/cross-db-example/lib/hsqldb-1.8.0.2.jar   (with props)
    incubator/cayenne/examples/trunk/cross-db-example/src/
    incubator/cayenne/examples/trunk/cross-db-example/src/cayenne.xml
    incubator/cayenne/examples/trunk/cross-db-example/src/db1.driver.xml
    incubator/cayenne/examples/trunk/cross-db-example/src/db2.driver.xml
    incubator/cayenne/examples/trunk/cross-db-example/src/map1.map.xml
    incubator/cayenne/examples/trunk/cross-db-example/src/map2.map.xml
    incubator/cayenne/examples/trunk/cross-db-example/src/test/
    incubator/cayenne/examples/trunk/cross-db-example/src/test/Main.java
    incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/
    incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/Department.java
    incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/auto/
    incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/auto/_Department.java
    incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/
    incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/Person.java
    incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/auto/
    incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/auto/_Person.java

Added: incubator/cayenne/examples/trunk/cross-db-example/.classpath
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/.classpath?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/.classpath (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/.classpath Tue Jul  4 13:28:18 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="bin"/>
+</classpath>

Added: incubator/cayenne/examples/trunk/cross-db-example/.project
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/.project?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/.project (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/.project Tue Jul  4 13:28:18 2006
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>cross-db-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/cross-db-example/README.txt
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/README.txt?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/README.txt (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/README.txt Tue Jul  4 13:28:18 2006
@@ -0,0 +1,17 @@
+Copyright 2005, Andrus Adamchik. 
+This example is distributed under the terms of ObjectStyle open source license.
+
+This example shows how Cayenne can be used with multiple
+databases at once, even with a relationship between tables across DBs.
+
+HOW TO RUN:
+
+This example is self-contained Eclipse project, including Cayenne 1.2RC2 jar. 
+Just import it in Eclipse (3.1 or newer is recommended) and run "test.Main" 
+class as "Java Application". As in-memory HSQLDB is used, no extra DB setup is required.
+
+MAPPING DETAILS:
+
+You can open and inspect mapping in CayenneModeler. XML files are located
+under "cross-db-example/src". Each database schema has its own DataMap, with
+relationship spanning across two DataMaps.
\ No newline at end of file

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

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

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

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

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

Added: incubator/cayenne/examples/trunk/cross-db-example/src/cayenne.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/cayenne.xml?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/cayenne.xml (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/cayenne.xml Tue Jul  4 13:28:18
2006
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<domains project-version="1.1">
+<domain name="cross-db">
+	<map name="map1" location="map1.map.xml"/>
+	<map name="map2" location="map2.map.xml"/>
+
+	<node name="db1"
+		 datasource="db1.driver.xml"
+		 factory="org.objectstyle.cayenne.conf.DriverDataSourceFactory">
+			<map-ref name="map1"/>
+	 </node>
+	<node name="db2"
+		 datasource="db2.driver.xml"
+		 factory="org.objectstyle.cayenne.conf.DriverDataSourceFactory">
+			<map-ref name="map2"/>
+	 </node>
+</domain>
+</domains>

Added: incubator/cayenne/examples/trunk/cross-db-example/src/db1.driver.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/db1.driver.xml?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/db1.driver.xml (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/db1.driver.xml Tue Jul  4 13:28:18
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:db1"/>
+	<connectionPool min="1" max="1" />
+	<login userName="sa"/>
+</driver>

Added: incubator/cayenne/examples/trunk/cross-db-example/src/db2.driver.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/db2.driver.xml?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/db2.driver.xml (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/db2.driver.xml Tue Jul  4 13:28:18
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:db2"/>
+	<connectionPool min="1" max="1" />
+	<login userName="sa"/>
+</driver>

Added: incubator/cayenne/examples/trunk/cross-db-example/src/map1.map.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/map1.map.xml?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/map1.map.xml (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/map1.map.xml Tue Jul  4 13:28:18
2006
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map project-version="1.1">
+	<property name="defaultPackage" value="test.db1"/>
+	<db-entity name="department">
+		<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+		<db-attribute name="name" type="VARCHAR" length="200"/>
+	</db-entity>
+	<obj-entity name="Department" className="test.db1.Department" dbEntityName="department">
+		<obj-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
+	</obj-entity>
+	<db-relationship name="people" source="department" target="person" toMany="true">
+		<db-attribute-pair source="id" target="department_id"/>
+	</db-relationship>
+	<obj-relationship name="people" source="Department" target="Person" deleteRule="Nullify"
db-relationship-path="people"/>
+</data-map>

Added: incubator/cayenne/examples/trunk/cross-db-example/src/map2.map.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/map2.map.xml?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/map2.map.xml (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/map2.map.xml Tue Jul  4 13:28:18
2006
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map project-version="1.1">
+	<property name="defaultPackage" value="test.db2"/>
+	<db-entity name="person">
+		<db-attribute name="department_id" type="INTEGER"/>
+		<db-attribute name="full_name" type="VARCHAR" length="200"/>
+		<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+	</db-entity>
+	<obj-entity name="Person" className="test.db2.Person" lock-type="optimistic" dbEntityName="person">
+		<obj-attribute name="fullName" type="java.lang.String" db-attribute-path="full_name"/>
+	</obj-entity>
+	<db-relationship name="department" source="person" target="department" toMany="false">
+		<db-attribute-pair source="department_id" target="id"/>
+	</db-relationship>
+	<obj-relationship name="department" source="Person" target="Department" deleteRule="Nullify"
db-relationship-path="department"/>
+</data-map>

Added: incubator/cayenne/examples/trunk/cross-db-example/src/test/Main.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/test/Main.java?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/test/Main.java (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/test/Main.java Tue Jul  4 13:28:18
2006
@@ -0,0 +1,107 @@
+package test;
+
+import java.util.Iterator;
+
+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.query.SelectQuery;
+
+import test.db1.Department;
+import test.db2.Person;
+
+public class Main {
+
+    DataContext context;
+
+    public static void main(String[] args) {
+        Main m = new Main();
+        try {
+            m.initSchema("map1");
+            m.initSchema("map2");
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            System.exit(1);
+        }
+
+        m.createDepartmentWithPeople();
+
+        m.invalidateAll();
+        m.readToManyRelationship();
+
+        m.invalidateAll();
+        m.readToOneRelationship();
+    }
+
+    Main() {
+        this.context = DataContext.createDataContext();
+    }
+
+    /**
+     * Helper method that initializes schema on startup. As we are using in-memory HSQLDB,
+     * we have to do it every time.
+     */
+    void initSchema(String mapName) throws Exception {
+        DataMap map = context.getEntityResolver().getDataMap(mapName);
+        DataNode node = context.getParentDataDomain().lookupDataNode(map);
+
+        // last domain parameter is needed to avoid bogus cross-db FK generation
+        DbGenerator g = new DbGenerator(node.getAdapter(), map, null, context
+                .getParentDataDomain());
+
+        g.runGenerator(node.getDataSource());
+    }
+
+    /**
+     * Helper method that invalidates all cached objects to make sure the next operation
+     * starts fresh from the database.
+     */
+    void invalidateAll() {
+        context.invalidateObjects(context.getGraphManager().registeredNodes());
+    }
+
+    void createDepartmentWithPeople() {
+        Department itDepartment = (Department) context
+                .createAndRegisterNewObject(Department.class);
+        itDepartment.setName("IT Department");
+
+        Person joe = (Person) context.createAndRegisterNewObject(Person.class);
+        joe.setFullName("Joe Doe");
+        joe.setDepartment(itDepartment);
+
+        // we can insert objects from different DBs in the same commit.
+        context.commitChanges();
+    }
+
+    void readToManyRelationship() {
+        Expression qualifier = ExpressionFactory.matchExp(
+                Department.NAME_PROPERTY,
+                "IT Department");
+        SelectQuery q = new SelectQuery(Department.class, qualifier);
+        Department itDepartment = (Department) context.performQuery(q).get(0);
+
+        // reading people will result in an internal query from a different DB:
+        Iterator it = itDepartment.getPeople().iterator();
+        while (it.hasNext()) {
+            Person p = (Person) it.next();
+            System.out.println(p.getFullName() + " works in " + itDepartment.getName());
+        }
+    }
+
+    void readToOneRelationship() {
+        Expression qualifier = ExpressionFactory.matchExp(
+                Person.FULL_NAME_PROPERTY,
+                "Joe Doe");
+        SelectQuery q = new SelectQuery(Person.class, qualifier);
+        Person joe = (Person) context.performQuery(q).get(0);
+
+        // reading department data will result in an internal query from a different DB:
+        System.out.println(joe.getFullName()
+                + " works in "
+                + joe.getDepartment().getName());
+    }
+}

Added: incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/Department.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/Department.java?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/Department.java (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/Department.java Tue Jul
 4 13:28:18 2006
@@ -0,0 +1,10 @@
+package test.db1;
+
+import test.db1.auto._Department;
+
+public class Department extends _Department {
+
+}
+
+
+

Added: incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/auto/_Department.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/auto/_Department.java?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/auto/_Department.java (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/test/db1/auto/_Department.java Tue
Jul  4 13:28:18 2006
@@ -0,0 +1,36 @@
+package test.db1.auto;
+
+import java.util.List;
+
+/** Class _Department was generated by Cayenne.
+  * It is probably a good idea to avoid changing this class manually, 
+  * since it may be overwritten next time code is regenerated. 
+  * If you need to make any customizations, please use subclass. 
+  */
+public class _Department extends org.objectstyle.cayenne.CayenneDataObject {
+
+    public static final String NAME_PROPERTY = "name";
+    public static final String PEOPLE_PROPERTY = "people";
+
+    public static final String ID_PK_COLUMN = "id";
+
+    public void setName(String name) {
+        writeProperty("name", name);
+    }
+    public String getName() {
+        return (String)readProperty("name");
+    }
+    
+    
+    public void addToPeople(test.db2.Person obj) {
+        addToManyTarget("people", obj, true);
+    }
+    public void removeFromPeople(test.db2.Person obj) {
+        removeToManyTarget("people", obj, true);
+    }
+    public List getPeople() {
+        return (List)readProperty("people");
+    }
+    
+    
+}

Added: incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/Person.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/Person.java?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/Person.java (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/Person.java Tue Jul  4
13:28:18 2006
@@ -0,0 +1,10 @@
+package test.db2;
+
+import test.db2.auto._Person;
+
+public class Person extends _Person {
+
+}
+
+
+

Added: incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/auto/_Person.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/auto/_Person.java?rev=419083&view=auto
==============================================================================
--- incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/auto/_Person.java (added)
+++ incubator/cayenne/examples/trunk/cross-db-example/src/test/db2/auto/_Person.java Tue Jul
 4 13:28:18 2006
@@ -0,0 +1,32 @@
+package test.db2.auto;
+
+/** Class _Person was generated by Cayenne.
+  * It is probably a good idea to avoid changing this class manually, 
+  * since it may be overwritten next time code is regenerated. 
+  * If you need to make any customizations, please use subclass. 
+  */
+public class _Person extends org.objectstyle.cayenne.CayenneDataObject {
+
+    public static final String FULL_NAME_PROPERTY = "fullName";
+    public static final String DEPARTMENT_PROPERTY = "department";
+
+    public static final String ID_PK_COLUMN = "id";
+
+    public void setFullName(String fullName) {
+        writeProperty("fullName", fullName);
+    }
+    public String getFullName() {
+        return (String)readProperty("fullName");
+    }
+    
+    
+    public void setDepartment(test.db1.Department department) {
+        setToOneTarget("department", department, true);
+    }
+
+    public test.db1.Department getDepartment() {
+        return (test.db1.Department)readProperty("department");
+    } 
+    
+    
+}



Mime
View raw message