cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r571812 - in /cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial: Artist.java Main.java Painting.java Tutorial.java Tutorial1.java Tutorial2.java Tutorial3.java
Date Sat, 01 Sep 2007 16:00:28 GMT
Author: aadamchik
Date: Sat Sep  1 09:00:27 2007
New Revision: 571812

URL: http://svn.apache.org/viewvc?rev=571812&view=rev
Log:
adding relationship examples

Added:
    cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial1.java
      - copied, changed from r571760, cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial.java
    cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial2.java
    cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial3.java
Removed:
    cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial.java
Modified:
    cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Artist.java
    cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Main.java
    cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Painting.java

Modified: cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Artist.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Artist.java?rev=571812&r1=571811&r2=571812&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Artist.java (original)
+++ cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Artist.java Sat Sep  1 09:00:27
2007
@@ -2,11 +2,13 @@
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Collection;
 import java.util.Date;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
+import javax.persistence.OneToMany;
 
 @Entity
 public class Artist {
@@ -22,6 +24,9 @@
 	@Column(name = "DATE_OF_BIRTH")
 	protected Date dateOfBirth;
 
+	@OneToMany(targetEntity = Painting.class)
+	protected Collection<Painting> paintings;
+
 	public String getName() {
 		return name;
 	}
@@ -58,6 +63,14 @@
 
 			setDateOfBirth(date);
 		}
+	}
+
+	public Collection<Painting> getPaintings() {
+		return paintings;
+	}
+
+	public void setPaintings(Collection<Painting> paintings) {
+		this.paintings = paintings;
 	}
 
 }

Modified: cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Main.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Main.java?rev=571812&r1=571811&r2=571812&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Main.java (original)
+++ cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Main.java Sat Sep  1 09:00:27
2007
@@ -15,13 +15,33 @@
 
 		EntityManagerFactory emf = Persistence
 				.createEntityManagerFactory("cayenne-tutorial");
+		try {
+			runTutorials(emf);
+		} finally {
+			emf.close();
+		}
+	}
+
+	static void runTutorials(EntityManagerFactory emf) {
 		EntityManager em = emf.createEntityManager();
+		try {
+			new Tutorial1(em).execute();
+		} finally {
+			em.close();
+		}
 
+		em = emf.createEntityManager();
 		try {
-			new Tutorial(em).execute();
+			new Tutorial2(em).execute();
+		} finally {
+			em.close();
+		}
+
+		em = emf.createEntityManager();
+		try {
+			new Tutorial3(em).execute();
 		} finally {
 			em.close();
-			emf.close();
 		}
 	}
 }

Modified: cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Painting.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Painting.java?rev=571812&r1=571811&r2=571812&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Painting.java (original)
+++ cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Painting.java Sat Sep  1
09:00:27 2007
@@ -2,6 +2,7 @@
 
 import javax.persistence.Entity;
 import javax.persistence.Id;
+import javax.persistence.ManyToOne;
 
 @Entity
 public class Painting {
@@ -9,6 +10,9 @@
 	protected int id;
 
 	protected String name;
+	
+	@ManyToOne
+	protected Artist artist;
 
 	public String getName() {
 		return name;
@@ -16,5 +20,13 @@
 
 	public void setName(String name) {
 		this.name = name;
+	}
+
+	public Artist getArtist() {
+		return artist;
+	}
+
+	public void setArtist(Artist artist) {
+		this.artist = artist;
 	}
 }

Copied: cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial1.java (from
r571760, cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial1.java?p2=cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial1.java&p1=cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial.java&r1=571760&r2=571812&rev=571812&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial.java (original)
+++ cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial1.java Sat Sep 
1 09:00:27 2007
@@ -3,11 +3,11 @@
 import javax.persistence.EntityManager;
 import javax.persistence.EntityTransaction;
 
-public class Tutorial {
+public class Tutorial1 {
 
 	protected EntityManager em;
 
-	public Tutorial(EntityManager em) {
+	public Tutorial1(EntityManager em) {
 		this.em = em;
 	}
 
@@ -21,18 +21,6 @@
 			em.createQuery("delete from Gallery").executeUpdate();
 			em.createQuery("delete from Artist").executeUpdate();
 			tx.commit();
-
-			// create new persistent object
-			tx = em.getTransaction();
-			tx.begin();
-
-			Artist picasso = new Artist();
-			picasso.setName("Pablo Picasso");
-			picasso.setDateOfBirthString("18811025");
-
-			em.persist(picasso);
-			tx.commit();
-
 		} catch (Throwable th) {
 			th.printStackTrace();
 

Added: cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial2.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial2.java?rev=571812&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial2.java (added)
+++ cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial2.java Sat Sep 
1 09:00:27 2007
@@ -0,0 +1,51 @@
+package cayenne.jpa.tutorial;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+
+public class Tutorial2 {
+	protected EntityManager em;
+
+	public Tutorial2(EntityManager em) {
+		this.em = em;
+	}
+
+	public void execute() {
+		EntityTransaction tx = null;
+		try {
+			// create new persistent object
+			tx = em.getTransaction();
+			tx.begin();
+
+			Artist picasso = new Artist();
+			picasso.setName("Pablo Picasso");
+			picasso.setDateOfBirthString("18811025");
+			
+			Artist dali = new Artist();
+			dali.setName("Salvador Dali");
+			picasso.setDateOfBirthString("19040511");
+
+			em.persist(picasso);
+			tx.commit();
+
+			tx.begin();
+			Painting girl = new Painting();
+			em.persist(girl);
+			girl.setName("Girl Reading at a Table");
+			girl.setArtist(picasso);
+
+			Painting stein = new Painting();
+			em.persist(stein);
+			stein.setName("Gertrude Stein");
+			stein.setArtist(picasso);
+
+			tx.commit();
+		} catch (Throwable th) {
+			th.printStackTrace();
+
+			if (tx != null) {
+				tx.rollback();
+			}
+		}
+	}
+}

Added: cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial3.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial3.java?rev=571812&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial3.java (added)
+++ cayenne/sandbox/cayenne-jpa-tutorial/src/cayenne/jpa/tutorial/Tutorial3.java Sat Sep 
1 09:00:27 2007
@@ -0,0 +1,35 @@
+package cayenne.jpa.tutorial;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+
+public class Tutorial3 {
+
+	protected EntityManager em;
+
+	public Tutorial3(EntityManager em) {
+		this.em = em;
+	}
+
+	public void execute() {
+		EntityTransaction tx = null;
+		try {
+			// create new persistent object
+			tx = em.getTransaction();
+
+			Artist picasso = (Artist) em.createQuery(
+					"select a from Artist a where a.name like '%Picasso'")
+					.getSingleResult();
+			System.out.println("Stored paintings for " + picasso.getName()
+					+ ": " + picasso.getPaintings().size());
+
+		} catch (Throwable th) {
+			th.printStackTrace();
+
+			if (tx != null) {
+				tx.rollback();
+			}
+		}
+	}
+
+}



Mime
View raw message