1
package log2;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import log2.model.Company;
import log2.model.Department;
import log2.model.Employee;
import log2.model.Phone;
public class Populate {
public static void main(String[] args) {
try {
InputStream fis_prop = new FileInputStream("versant.properties");
Properties prop = new Properties();
prop.load(fis_prop);
fis_prop.close();
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(prop);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
Employee hiep = new Employee("Hiep", new Phone("0668196134"),"Resp. projets");
Employee harry = new Employee("Harry", new Phone("0668196134"),"Resp. ressource humain");
Employee ron = new Employee("Ron", new Phone("0668196134"),"Resp. communication");
Company inria = new Company("INRIA");
Department oasis = new Department("OASIS");
Department orion = new Department("ORION");
oasis.setCompany(inria);
orion.setCompany(inria);
inria.getDepartments().add(oasis);
inria.getDepartments().add(orion);
oasis.getEmployees().add(hiep);
oasis.getEmployees().add(harry);
orion.getEmployees().add(ron);
pm.makePersistent(inria);
pm.currentTransaction().commit();
pm.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package log2;
import log2.model.Contact;
import javax.jdo.*;
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
public class InsertData {
2
public static void main(String[] args) {
try {
// load .jdogenie project file as properties to connect
Properties p = loadProperties();
// run against local or remote JDO Genie server
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);
InsertData insertData = new InsertData(pmf.getPersistenceManager());
try {
insertData.go();
} finally {
insertData.pm.close();
}
System.exit(0);
} catch (Exception e) {
e.printStackTrace(System.out);
System.exit(1);
}
}
private static Properties loadProperties() throws IOException {
Properties p = new Properties();
InputStream in = null;
try {
in = InsertData.class.getResourceAsStream("/versant.properties");
if (in == null) throw new IOException("versant.properties not on classpath");
p.load(in);
} finally {
if (in != null) in.close();
}
return p;
}
private PersistenceManager pm;
public InsertData(PersistenceManager pm) {
this.pm = pm;
}
public void go() throws Exception {
createContacts();
showContacts();
lookupContact("D");
lookupContact("Ca");
lookupContact("Te");
}
/**
* Create the product catalog.
*/
private void createContacts() {
pm.currentTransaction().begin();
Contact david = new Contact("David", "Tinker", "555 4321", "555 1234", "david@jdogenie.com");
Contact jaco = new Contact("Jaco", "Uys", "555 9876", "555 6789", "jaco@jdogenie.com");
Contact carl = new Contact("Carl", "Cronje", "555 4567", "555 7654", "carl@jdogenie.com");
Contact dirk = new Contact("Dirk", "le Roux", "555 2345", "555 5432", "dirk@jdogenie.com");
pm.makePersistent(david);
pm.makePersistent(jaco);
pm.makePersistent(carl);
pm.makePersistent(dirk);
pm.currentTransaction().commit();
}
/**
* List all items in the catalog in description order.
*/
private void showContacts() {
Query q = pm.newQuery(Contact.class);
q.setOrdering("name ascending");
Collection ans = null;
try {
3
ans = (Collection)q.execute();
System.out.println("Contacts, ordered by name:");
for (Iterator i = ans.iterator(); i.hasNext(); ) {
Contact contact = (Contact)i.next();
System.out.println(contact.getName() + " " + contact.getSurname() +
" (" + contact.getEmail()+")");
}
System.out.println("---");
} finally {
if (ans != null) q.close(ans);
}
}
/**
* Find the item with code.
*/
private void lookupContact(String nameStart) {
Query q = pm.newQuery(Contact.class);
q.declareParameters("String ns");
q.setFilter("name.startsWith(ns)");
Collection ans = null;
try {
ans = (Collection)q.execute(nameStart);
System.out.println("Lookup Contact with name starting with:'"+nameStart+"'");
Iterator i = ans.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
System.out.println("---");
} finally {
if (ans != null) q.close(ans);
}
}
}
package log2;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import log2.model.Employee;
import log2.model.Person;
public class TestQuery {
public static void main(String[] args) {
try {
InputStream fis_prop = new FileInputStream("versant.properties");
Properties prop = new Properties();
prop.load(fis_prop);
fis_prop.close();
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(prop);
PersistenceManager pm = pmf.getPersistenceManager();
4
System.out.println("Les personne dont le nom commence par 'H'");
Collection resu = new ArrayList();
Query query = pm.newQuery(Employee.class, "name.startsWith(prefix)");
query.declareParameters("String prefix");
resu = (Collection)query.execute("H");
Iterator i = resu.iterator();
while (i.hasNext()) {
Person p = (Person) (i.next());
System.out.println(p.getName());
}
System.out.println("Tout employee");
pm.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<jdo>
<package name="log2.model">
<class name="Person" />
<class name="Phone" />
<class name="Company">
<field name="departments">
<collection element-type="Department">
<extension vendor-name="versant" key="inverse" value="company" />
</collection>
</field>
</class>
<class name="Employee" persistence-capable-superclass="Person" />
<class name="Department">
<field name="employees">
<collection element-type="Employee">
<extension vendor-name="versant" key="inverse" value="{auto}" />
</collection>
</field>
</class>
<class name="Contact" />
</package>
<package name="log2">
<class name="InsertData" />
</package>
</jdo>
1 / 4 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !