Skip to content

Commit 3220913

Browse files
shaimaa-hshalabyzhendrikse
authored andcommitted
From JDO to XML and Back Again 2/3 (eugenp#2681)
* From JDO to XML and Back Again 2/3 * remove comments * remove myapp class * divided into methods * remove main methods * remove classes
1 parent d343c59 commit 3220913

7 files changed

Lines changed: 342 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.jdo.xml;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import javax.jdo.annotations.Element;
9+
import javax.jdo.annotations.PersistenceCapable;
10+
import javax.jdo.annotations.PrimaryKey;
11+
import javax.xml.bind.annotation.XmlAttribute;
12+
import javax.xml.bind.annotation.XmlElement;
13+
import javax.xml.bind.annotation.XmlElementWrapper;
14+
15+
@PersistenceCapable(
16+
schema="/myproduct/people",
17+
table="person"
18+
)
19+
public class AnnotadedPerson {
20+
@XmlAttribute
21+
private long personNum;
22+
23+
@PrimaryKey
24+
private String firstName;
25+
private String lastName;
26+
27+
@XmlElementWrapper(name="phone-numbers")
28+
@XmlElement(name="phone-number")
29+
@Element(types=String.class)
30+
private List phoneNumbers = new ArrayList();
31+
32+
33+
public AnnotadedPerson(long personNum, String firstName, String lastName) {
34+
super();
35+
this.personNum = personNum;
36+
this.firstName = firstName;
37+
this.lastName = lastName;
38+
}
39+
40+
public long getPersonNum() {
41+
return personNum;
42+
}
43+
44+
public void setPersonNum(long personNum) {
45+
this.personNum = personNum;
46+
}
47+
48+
public String getFirstName() {
49+
return firstName;
50+
}
51+
52+
public void setFirstName(String firstName) {
53+
this.firstName = firstName;
54+
}
55+
56+
public String getLastName() {
57+
return lastName;
58+
}
59+
60+
public void setLastName(String lastName) {
61+
this.lastName = lastName;
62+
}
63+
64+
public List getPhoneNumbers() {
65+
return phoneNumbers;
66+
}
67+
68+
public void setPhoneNumbers(List phoneNumbers) {
69+
this.phoneNumbers = phoneNumbers;
70+
}
71+
72+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.jdo.xml;
2+
3+
import java.util.List;
4+
5+
import javax.jdo.JDOHelper;
6+
import javax.jdo.PersistenceManager;
7+
import javax.jdo.PersistenceManagerFactory;
8+
import javax.jdo.Query;
9+
import javax.jdo.Transaction;
10+
11+
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
12+
import org.datanucleus.metadata.PersistenceUnitMetaData;
13+
14+
public class MyApp {
15+
16+
private static PersistenceUnitMetaData pumd;
17+
private static PersistenceManagerFactory pmf;
18+
private static PersistenceManager pm;
19+
20+
public static void main( String[] args ) {
21+
22+
//persist product object using dynamic persistence unit
23+
defineDynamicPersistentUnit();
24+
Product product = new Product("id1","Sony Discman", "A standard discman from Sony", 49.99);
25+
persistObject(product);
26+
closePersistenceManager();
27+
28+
//persist AnnotatedPerson object using named pmf
29+
defineNamedPersistenceManagerFactory("XmlDatastore");
30+
AnnotadedPerson annotatedPerson = new AnnotadedPerson(654320,"annotated","person");
31+
annotatedPerson.getPhoneNumbers().add("999999999");
32+
annotatedPerson.getPhoneNumbers().add("000000000");
33+
persistObject(annotatedPerson);
34+
queryAnnotatedPersonsInXML();
35+
closePersistenceManager();
36+
37+
//persist Person object using PMF created by properties file
38+
definePersistenceManagerFactoryUsingPropertiesFile("META-INF\\datanucleus.properties");
39+
Person person = new Person(654321,"bealdung","author");
40+
person.getPhoneNumbers().add("123456789");
41+
person.getPhoneNumbers().add("987654321");
42+
persistObject(person);
43+
queryPersonsInXML();
44+
closePersistenceManager();
45+
}
46+
47+
public static void defineDynamicPersistentUnit(){
48+
49+
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
50+
pumd.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myfile_dynamicPMF.xml");
51+
pumd.addProperty("datanucleus.schema.autoCreateAll", "true");
52+
pumd.addProperty("datanucleus.xml.indentSize", "4");
53+
54+
pmf = new JDOPersistenceManagerFactory(pumd, null);
55+
pm = pmf.getPersistenceManager();
56+
}
57+
58+
public static void defineNamedPersistenceManagerFactory(String pmfName){
59+
60+
pmf = JDOHelper.getPersistenceManagerFactory("XmlDatastore");
61+
pm = pmf.getPersistenceManager();
62+
}
63+
64+
public static void definePersistenceManagerFactoryUsingPropertiesFile(String filePath){
65+
66+
pmf = JDOHelper.getPersistenceManagerFactory(filePath);
67+
pm = pmf.getPersistenceManager();
68+
}
69+
70+
public static void closePersistenceManager(){
71+
72+
if(pm!=null && !pm.isClosed()){
73+
pm.close();
74+
}
75+
}
76+
77+
public static void persistObject(Object obj){
78+
79+
Transaction tx = pm.currentTransaction();
80+
81+
try {
82+
tx.begin();
83+
pm.makePersistent(obj);
84+
tx.commit();
85+
} finally {
86+
if (tx.isActive()) {
87+
tx.rollback();
88+
}
89+
}
90+
}
91+
92+
public static void queryPersonsInXML(){
93+
94+
Query<Person> query = pm.newQuery(Person.class);
95+
List<Person> result = query.executeList();
96+
System.out.println("name: "+result.get(0).getFirstName());
97+
}
98+
99+
public static void queryAnnotatedPersonsInXML(){
100+
101+
Query<AnnotadedPerson> query = pm.newQuery(AnnotadedPerson.class);
102+
List<AnnotadedPerson> result = query.executeList();
103+
System.out.println("name: "+result.get(0).getFirstName());
104+
}
105+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.jdo.xml;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import javax.jdo.annotations.Element;
9+
import javax.jdo.annotations.PersistenceCapable;
10+
import javax.jdo.annotations.PrimaryKey;
11+
import javax.xml.bind.annotation.XmlAttribute;
12+
import javax.xml.bind.annotation.XmlElement;
13+
import javax.xml.bind.annotation.XmlElementWrapper;
14+
15+
16+
@PersistenceCapable
17+
public class Person {
18+
private long personNum;
19+
20+
@PrimaryKey
21+
private String firstName;
22+
private String lastName;
23+
24+
private List phoneNumbers = new ArrayList();
25+
26+
public Person(long personNum, String firstName, String lastName) {
27+
super();
28+
this.personNum = personNum;
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
}
32+
33+
public long getPersonNum() {
34+
return personNum;
35+
}
36+
37+
public void setPersonNum(long personNum) {
38+
this.personNum = personNum;
39+
}
40+
41+
public String getFirstName() {
42+
return firstName;
43+
}
44+
45+
public void setFirstName(String firstName) {
46+
this.firstName = firstName;
47+
}
48+
49+
public String getLastName() {
50+
return lastName;
51+
}
52+
53+
public void setLastName(String lastName) {
54+
this.lastName = lastName;
55+
}
56+
57+
public List getPhoneNumbers() {
58+
return phoneNumbers;
59+
}
60+
61+
public void setPhoneNumbers(List phoneNumbers) {
62+
this.phoneNumbers = phoneNumbers;
63+
}
64+
65+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.jdo.xml;
2+
3+
import javax.jdo.annotations.IdGeneratorStrategy;
4+
import javax.jdo.annotations.PersistenceAware;
5+
import javax.jdo.annotations.PersistenceCapable;
6+
import javax.jdo.annotations.Persistent;
7+
import javax.jdo.annotations.PrimaryKey;
8+
9+
@PersistenceCapable
10+
public class Product {
11+
12+
@PrimaryKey
13+
String id;
14+
String name;
15+
String description;
16+
double price;
17+
18+
public Product(){
19+
20+
}
21+
22+
public Product(String id,String name,String description,double price){
23+
this.id = id;
24+
this.name=name;
25+
this.description = description;
26+
this.price = price;
27+
}
28+
29+
30+
public String getId() {
31+
return id;
32+
}
33+
34+
public void setId(String id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
public String getDescription() {
45+
return description;
46+
}
47+
public void setDescription(String description) {
48+
this.description = description;
49+
}
50+
public double getPrice() {
51+
return price;
52+
}
53+
public void setPrice(double price) {
54+
this.price = price;
55+
}
56+
57+
58+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
javax.jdo.PersistenceManagerFactoryClass=org.datanucleus.api.jdo.JDOPersistenceManagerFactory
2+
javax.jdo.option.ConnectionURL= xml:file:myfile-ds.xml
3+
datanucleus.xml.indentSize=6
4+
datanucleus.schema.autoCreateAll=true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<jdoconfig xmlns="http://xmlns.jcp.org/xml/ns/jdo/jdoconfig"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/jdo/jdoconfig
5+
http://xmlns.jcp.org/xml/ns/jdo/jdoconfig_3_2.xsd"
6+
version="3.2">
7+
8+
<!-- Datastore Txn PMF -->
9+
<persistence-manager-factory name="XmlDatastore">
10+
<property name="javax.jdo.PersistenceManagerFactoryClass"
11+
value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" />
12+
<property name="javax.jdo.option.ConnectionURL" value="xml:file:namedPMF-ds.xml" />
13+
<property name="datanucleus.xml.indentSize" value="6" />
14+
<property name="datanucleus.schema.autoCreateAll"
15+
value="true" />
16+
</persistence-manager-factory>
17+
</jdoconfig>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<jdo xmlns="http://xmlns.jcp.org/xml/ns/jdo/jdo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/jdo/jdo http://xmlns.jcp.org/xml/ns/jdo/jdo_3_1.xsd"
3+
version="3.1">
4+
<package name="com.baeldung.jdo.xml">
5+
<class name="Person" detachable="true" schema="/myproduct/people"
6+
table="person">
7+
<field name="personNum">
8+
<extension vendor-name="datanucleus" key="XmlAttribute"
9+
value="true" />
10+
</field>
11+
<field name="firstName" primary-key="true" /> <!-- PK since JAXB requires String -->
12+
<field name="lastName" />
13+
<field name="phoneNumbers">
14+
<collection element-type="java.lang.String" />
15+
<element column="phoneNumber" />
16+
</field>
17+
</class>
18+
</package>
19+
20+
21+
</jdo>

0 commit comments

Comments
 (0)