Commits

noll  committed 101e662

OpenJPA-Implementation mit fehlerhafter Persistence. Libs vergessen

  • Participants
  • Parent commits f1ad599

Comments (0)

Files changed (3)

File lib/javax.persistence.jar

Binary file added.

File lib/openjpa-all-2.1.1.jar

Binary file added.

File src/strategies/OpenJPA.java

+package strategies;
+
+import hs.Product;
+import hs.ProductList;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+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();
+
+		e = fac.createEntityManager();
+		t = e.getTransaction();
+	}
+
+	public static EntityManagerFactory getWithoutConfig() {
+
+		Map<String, String> map = new HashMap<String, String>();
+
+		map.put("openjpa.ConnectionURL",
+				"jdbc:postgresql://is024sv.uni-duisburg.de/ws1011");
+		map.put("openjpa.ConnectionDriverName", "org.postgresql.Driver");
+		map.put("openjpa.ConnectionUserName", "ws1011");
+		map.put("openjpa.ConnectionPassword", "ftpw10");
+		map.put("openjpa.RuntimeUnenhancedClasses", "supported");
+		map.put("openjpa.jdbc.SynchronizeMappings", "false");
+
+		// find all classes to registrate them
+		List<Class<?>> types = new ArrayList<Class<?>>();
+		types.add(Product.class);
+
+		if (!types.isEmpty()) {
+			StringBuffer buf = new StringBuffer();
+			for (Class<?> c : types) {
+				if (buf.length() > 0)
+					buf.append(";");
+				buf.append(c.getName());
+			}
+			// <class>Product</class>
+			map.put("openjpa.MetaDataFactory", "jpa(Types=" + buf.toString()
+					+ ")");
+		}
+
+		return OpenJPAPersistence.getEntityManagerFactory(map);
+
+	}
+
+	@Override
+	public Object readObject() throws IOException, ClassNotFoundException {
+
+		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
+		return readProducts;
+	}
+
+	@Override
+	public void writeObject(Serializable obj) throws IOException {
+
+		/*
+		 * 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
+	public boolean isOpen() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public void close(Type type) throws IOException {
+		// TODO Auto-generated method stub
+
+	}
+
+	@Override
+	public void open(Type type) throws IOException {
+		// TODO Auto-generated method stub
+
+	}
+
+	@Override
+	public void restart(Type type) throws IOException {
+		// TODO Auto-generated method stub
+
+	}
+
+}