Commits

Micha Kops committed c082e43

JPA persistence example added.

  • Participants
  • Parent commits 4301b94

Comments (0)

Files changed (5)

File src/main/java/com/hascode/tutorial/jee/Book.java

+package com.hascode.tutorial.jee;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+@Entity
+public class Book {
+	@Id
+	@GeneratedValue
+	private Long id;
+
+	@NotNull
+	@Size(min = 3, max = 40)
+	private String title;
+
+	@NotNull
+	private String author;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(final Long id) {
+		this.id = id;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(final String title) {
+		this.title = title;
+	}
+
+	public String getAuthor() {
+		return author;
+	}
+
+	public void setAuthor(final String author) {
+		this.author = author;
+	}
+}

File src/main/java/com/hascode/tutorial/jee/BookEJB.java

+package com.hascode.tutorial.jee;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ejb.Stateless;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+
+@Stateless
+public class BookEJB {
+	@PersistenceContext(unitName = "defaultPersistenceUnit")
+	private EntityManager em;
+
+	public Book saveBook(final Book book) {
+		em.persist(book);
+		return book;
+	}
+
+	public List<Book> findAllBooks() {
+		final Query query = em
+				.createQuery("SELECT b FROM Book b ORDER BY b.title DESC");
+		List<Book> entries = query.getResultList();
+		if (entries == null) {
+			entries = new ArrayList<Book>();
+		}
+		return entries;
+	}
+
+	public void deleteBook(Book book) {
+		book = em.merge(book);
+		em.remove(book);
+	}
+}

File src/main/resources/META-INF/persistence.xml

 		transaction-type="JTA">
 		<provider>oracle.toplink.essentials.PersistenceProvider</provider>
 		<jta-data-source>jdbc/__default</jta-data-source>
-		<class>com.hascode.tutorial.jee.User</class>
+		<class>com.hascode.tutorial.jee.Book</class>
 		<properties>
 			<property name="eclipselink.ddl-generation" value="create-tables" />
 		</properties>

File src/test/java/com/hascode/tutorial/jee/BookServiceIT.java

+package com.hascode.tutorial.jee;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import javax.inject.Inject;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(Arquillian.class)
+public class BookServiceIT {
+	@Inject
+	private BookEJB bookEJB;
+
+	@Deployment
+	public static JavaArchive createArchiveAndDeploy() {
+		return ShrinkWrap.create(JavaArchive.class)
+				.addClasses(BookEJB.class, Book.class)
+				.addAsResource("META-INF/persistence.xml")
+				.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+	}
+
+	@Test
+	public void testFetchBooks() {
+		assertTrue(bookEJB.findAllBooks().isEmpty());
+		Book b1 = new Book();
+		b1.setAuthor("tim");
+		b1.setTitle("A fascinating book");
+		Book b2 = new Book();
+		b2.setAuthor("tom");
+		b2.setTitle("I R Coder");
+		Book b3 = new Book();
+		b3.setAuthor("maria");
+		b3.setTitle("Some book");
+		Book b4 = new Book();
+		b4.setAuthor("tim");
+		b4.setTitle("Another fascinating book");
+		bookEJB.saveBook(b1);
+		bookEJB.saveBook(b2);
+		bookEJB.saveBook(b3);
+		bookEJB.saveBook(b4);
+		assertEquals(4, bookEJB.findAllBooks().size());
+		bookEJB.deleteBook(b4);
+		assertEquals(3, bookEJB.findAllBooks().size());
+	}
+}

File src/test/resources/META-INF/persistence.xml

+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
+<persistence-unit name="defaultPersistenceUnit" transaction-type="JTA">
+    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
+    <class>com.hascode.tutorial.jee.Book</class>
+    <properties>
+        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
+        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
+        <property name="javax.persistence.jdbc.user" value="sa"/>
+        <property name="javax.persistence.jdbc.password" value=""/>
+        <property name="eclipselink.ddl-generation" value="create-tables" />
+    </properties>
+</persistence-unit>
+</persistence>