Skip to content

Commit dbcea88

Browse files
committed
Started to include JPA into App Engine
1 parent f43a49b commit dbcea88

10 files changed

Lines changed: 106 additions & 56 deletions

File tree

‎de.vogella.gae.java.todo/src/META-INF/jdoconfig.xml‎

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
5+
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
6+
7+
<persistence-unit name="transactions-optional">
8+
<provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>
9+
<properties>
10+
<property name="datanucleus.NontransactionalRead" value="true"/>
11+
<property name="datanucleus.NontransactionalWrite" value="true"/>
12+
<property name="datanucleus.ConnectionURL" value="appengine"/>
13+
</properties>
14+
</persistence-unit>
15+
16+
</persistence>

‎de.vogella.gae.java.todo/src/de/vogella/gae/java/todo/dao/Dao.java‎

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,62 @@
44
import java.util.Calendar;
55
import java.util.List;
66

7+
import javax.persistence.EntityManager;
8+
import javax.persistence.Query;
9+
710
import org.datanucleus.sco.simple.GregorianCalendar;
811

912
import de.vogella.gae.java.todo.model.Todo;
1013

1114
public enum Dao {
12-
INSTANCE;
13-
private static long number=1;
14-
15+
INSTANCE;
16+
private static long number = 1;
17+
1518
private final List<Todo> todos = new ArrayList<Todo>();
16-
private Dao(){
19+
20+
public List<Todo> listTodos() {
21+
EntityManager em = EMFService.get().createEntityManager();
22+
// Begin a new local transaction so that we can persist a new entity
23+
em.getTransaction().begin();
24+
25+
// Read the existing entries
26+
Query q = em.createQuery("select m from Todo todo");
27+
em.getTransaction().commit();
28+
1729
// Create a test Todo
1830
// Image that I read the data from bigtables
19-
Todo todo = new Todo(1, "vogella", "Issue number 1", "Detailed Description of everything", "", GregorianCalendar.getInstance());
31+
Todo todo = new Todo(1, "vogella", "Issue number 1",
32+
"Detailed Description of everything", "",
33+
GregorianCalendar.getInstance());
2034
todos.add(todo);
2135
}
22-
public void add(String author, String summery, String description, String url, Calendar dueDate){
36+
37+
public void add(String author, String summery, String description,
38+
String url, Calendar dueDate) {
2339
synchronized (this) {
24-
number ++;
25-
todos.add(new Todo(number, author, summery, description, url, dueDate));
40+
number++;
41+
todos.add(new Todo(number, author, summery, description, url,
42+
dueDate));
2643
}
2744
}
2845

29-
30-
public List<Todo> getTodos(@SuppressWarnings("unused") String user){
31-
// For testing we will always return the same data no matter what the user is
46+
public List<Todo> getTodos(@SuppressWarnings("unused") String user) {
47+
// For testing we will always return the same data no matter what the
48+
// user is
3249
return todos;
3350
}
51+
3452
public void remove(long id) {
3553
Todo remove = null;
36-
for (Todo todo: todos){
37-
if (todo.getId() == id){
54+
for (Todo todo : todos) {
55+
if (todo.getId() == id) {
3856
remove = todo;
3957
}
4058
}
41-
if (remove!=null){
59+
if (remove != null) {
4260
todos.remove(remove);
4361
}
44-
62+
4563
}
46-
47-
}
4864

65+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package de.vogella.gae.java.todo.dao;
2+
3+
import javax.persistence.EntityManagerFactory;
4+
import javax.persistence.Persistence;
5+
6+
public class EMFService {
7+
private static final EntityManagerFactory emfInstance = Persistence
8+
.createEntityManagerFactory("transactions-optional");
9+
10+
private EMFService() {
11+
}
12+
13+
public static EntityManagerFactory get() {
14+
return emfInstance;
15+
}
16+
}

‎de.vogella.gae.java.todo/src/de/vogella/gae/java/todo/model/Todo.java‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,90 @@
33
import java.util.Calendar;
44

55
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
68
import javax.persistence.Id;
79

810
/**
911
* Model class which will store the Todo Items
1012
*
1113
* @author Lars Vogel
12-
*
14+
*
1315
*/
1416
@Entity
1517
public class Todo {
1618
@Id
17-
private final long id;
19+
@GeneratedValue(strategy = GenerationType.IDENTITY)
20+
private long id;
1821
private String author;
22+
1923
private String shortDescription;
2024
private String longDescription;
2125
private String url;
2226
private Calendar dueDate;
2327
boolean finished;
24-
25-
public Todo(long id, String author, String shortDescription, String longDescription, String url, Calendar dueDate) {
26-
this.id= id;
28+
29+
public Todo(long id, String author, String shortDescription,
30+
String longDescription, String url, Calendar dueDate) {
31+
this.id = id;
2732
this.author = author;
2833
this.shortDescription = shortDescription;
2934
this.longDescription = longDescription;
3035
this.url = url;
3136
this.dueDate = dueDate;
32-
finished = false;
37+
finished = false;
3338
}
34-
39+
3540
public long getId() {
3641
return id;
3742
}
3843

3944
public String getAuthor() {
4045
return author;
4146
}
47+
4248
public void setAuthor(String author) {
4349
this.author = author;
4450
}
51+
4552
public String getShortDescription() {
4653
return shortDescription;
4754
}
55+
4856
public void setShortDescription(String shortDescription) {
4957
this.shortDescription = shortDescription;
5058
}
59+
5160
public String getLongDescription() {
5261
return longDescription;
5362
}
63+
5464
public void setLongDescription(String longDescription) {
5565
this.longDescription = longDescription;
5666
}
67+
5768
public String getUrl() {
5869
return url;
5970
}
71+
6072
public void setUrl(String url) {
6173
this.url = url;
6274
}
75+
6376
public Calendar getDueDate() {
6477
return dueDate;
6578
}
79+
6680
public void setDueDate(Calendar dueDate) {
6781
this.dueDate = dueDate;
6882
}
83+
6984
public boolean isFinished() {
7085
return finished;
7186
}
87+
7288
public void setFinished(boolean finished) {
7389
this.finished = finished;
7490
}
75-
76-
91+
7792
}

‎de.vogella.gae.java.todo/war/WEB-INF/classes/META-INF/jdoconfig.xml‎

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
5+
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
6+
7+
<persistence-unit name="transactions-optional">
8+
<provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>
9+
<properties>
10+
<property name="datanucleus.NontransactionalRead" value="true"/>
11+
<property name="datanucleus.NontransactionalWrite" value="true"/>
12+
<property name="datanucleus.ConnectionURL" value="appengine"/>
13+
</properties>
14+
</persistence-unit>
15+
16+
</persistence>
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)