Commits

synthomat  committed ae81897

OpenJPA implementiert!

  • Participants
  • Parent commits 101e662

Comments (0)

Files changed (8)

-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="src" path="src"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
-	<classpathentry kind="lib" path="lib/fpt2011.jar"/>
-	<classpathentry kind="lib" path="lib/xstream-1.4.2.jar"/>
-	<classpathentry kind="lib" path="lib/postgresql-8.4-703.jdbc4.jar"/>
-	<classpathentry kind="lib" path="lib/javax.persistence.jar"/>
-	<classpathentry kind="lib" path="lib/openjpa-all-2.1.1.jar"/>
-	<classpathentry kind="output" path="bin"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="lib" path="lib/fpt2011.jar"/>
+	<classpathentry kind="lib" path="lib/xstream-1.4.2.jar"/>
+	<classpathentry kind="lib" path="lib/postgresql-8.4-703.jdbc4.jar"/>
+	<classpathentry kind="lib" path="lib/openjpa-all-2.1.1.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

File src/META-INF/persistence.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<persistence version="1.0"
+    xmlns="http://java.sun.com/xml/ns/persistence"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
+                        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
+	<persistence-unit name="openjpa">
+		<description>JPA Application Client</description>
+		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
+		<class>hs.Product</class>
+		<properties>
+			<property name="openjpa.ConnectionURL" value="jdbc:postgresql://is024sv.uni-duisburg.de/ws1011" />
+			<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver" />
+			<property name="openjpa.ConnectionUserName" value="ws1011" />
+			<property name="openjpa.ConnectionPassword" value="ftpw10" />
+			<property name="openjpa.jdbc.SynchronizeMappings" value="false"/> 
+			<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/> 
+		</properties>
+	</persistence-unit>
+</persistence>

File src/controller/ControllerShop.java

 
 		try {
 			ProductList pl = (ProductList)strategy.readObject();
-			pl.setProductIdCounter(pl.get(pl.size()-1).getId());
-			System.out.println("Höchste ID: " + pl.getProductIdCounter());
+			//pl.setProductIdCounter(pl.get(pl.size()-1).getId());
+			//System.out.println("Höchste ID: " + pl.getProductIdCounter());
 			model.setProducts(pl);
 		} catch (IOException e1) {
 			// TODO Auto-generated catch block

File src/db/JDBCConnector.java

 
 			// ResultSet-Objekt wird verarbeitet und die Werte
 			// werden auf die Standardausgabe geschrieben
-			/*
+			
 			while (rs.next()) {
 				for (int i = 1; i <= n; i++) {
 					if (i != n)
 						System.out.println(rs.getString(i));
 				}
 			}
-			*/
+			
 			System.out.println(meta.getURL());
 			System.out.println(meta.getUserName());
 			ResultSet res = meta.getTables(null, null, null,
 		try {
 			Statement stmt = con.createStatement();
 			ResultSet rs = stmt.executeQuery("SELECT * FROM products WHERE id = " + productId);
-			
+
 			while (rs.next()) {
 				
 				// neues Product anlegen

File src/hs/HardwareShop.java

 import view.ViewShop;
 import model.ModelShop;
 
-
 public class HardwareShop {
 
 	/**

File src/hs/Product.java

 import javax.persistence.*;
 import javax.persistence.SequenceGenerator;
 
-
-
 @Entity()
 @Table(name = "products")
-
 public class Product implements fpt.com.Product{
 	
-
-	
 	/**
 	 * 
 	 */
 
 	private String name;
 	private int quantity;
-	private double price; 
+	private double price;
+
 	@Id
 	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "products_SEQ")
 	@SequenceGenerator(name = "products_SEQ", sequenceName ="products_id_seq", allocationSize = 1) 

File src/model/ModelShop.java

 	public void addProduct(Product product) {
 		// Produkt der Produkt-Liste hinzufügen
 		try {
-			product.setId(productList.getNextProductId());
+			//product.setId(productList.getNextProductId());
 			System.out.println("neue ID: " + product.getIdAsString());
 			productList.add(product);
 			this.setChanged();

File src/strategies/OpenJPA.java

 import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Persistence;
+import javax.persistence.Query;
+
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 
 public class OpenJPA implements fpt.com.SerializableStrategy {
 
-	// EntityManagerFactory fac = Persistence.createEntityManagerFactory(
-	// "openjpa", System.getProperties());
-
 	EntityManagerFactory fac = null;
 	EntityManager e;
 	EntityTransaction t;
 
 	public OpenJPA() {
-		fac = getWithoutConfig();
+		// Verbindung OHNE persistence.xml
+		// fac = getWithoutConfig();
+		
+		// Verbindung MIT persistence.xml
+		fac = getWithConfig();
+		
+		e = fac.createEntityManager();
+	}
 
-		e = fac.createEntityManager();
-		t = e.getTransaction();
+	public static EntityManagerFactory getWithConfig() {
+		EntityManagerFactory fac = Persistence.createEntityManagerFactory(
+				"openjpa", System.getProperties());
+
+		return fac;
 	}
 
 	public static EntityManagerFactory getWithoutConfig() {
 		}
 
 		return OpenJPAPersistence.getEntityManagerFactory(map);
-
 	}
 
 	@Override
 	public Object readObject() throws IOException, ClassNotFoundException {
+		t = e.getTransaction();
+		
+		ProductList readProducts = new ProductList();
 
-		ProductList readProducts = new ProductList();
-		// open(Type.READ);
-		t.begin();
-		// QBE
-		for (Object o : e.createQuery("SELECT * FROM Producer c")
-				.getResultList()) {
-			System.out.println(o);
-			readProducts = (ProductList) o;
-		}
-		t.commit(); // all ok commit
+		// Abfrage mit id < 41000
+		Query q = e.createQuery("SELECT p FROM Product p WHERE p.id < 41000 ORDER BY p.id DESC");
+
+		// Anzahl beschränken
+		q.setMaxResults(50);
+
+		// Ergebnisliste aus der Abfrage
+		List<Product> results = q.getResultList();
+
+		for (Product p : results )				
+			readProducts.add(p);
+
 		return readProducts;
 	}
 
 	@Override
 	public void writeObject(Serializable obj) throws IOException {
-
+		t = e.getTransaction();
 		/*
-		 * Durchl�uft die ProductList
+		 * Durchl�uft die ProductList
 		 */
 		Iterator<fpt.com.Product> iter = ((ProductList) obj).iterator();
+		
 		t.begin();
 		while (iter.hasNext()) {
 			Product p = (Product) iter.next();
 			e.persist(p); // schreibt das Product weg
-
 		}
 		t.commit();
 		// all ok commit
 		// all Data is saved in database now
-
 	}
 
 	@Override