Commits

Micha Kops committed 7cbb73d

Initial import.

Comments (0)

Files changed (7)

+.project
+.settings
+.classpath
+target
+derby.log
+test2db
+# JPA2 Projection Tutorial
+
+A small tutorial how to use constructor expressions in JPA queries to produce lightweight, detached objects.
+
+Please feel free to take a look at [my blog] for the full tutorial.
+
+----
+
+**2013 Micha Kops / hasCode.com**
+
+   [my blog]:http://www.hascode.com/
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>jpa2-projection-tutorial</artifactId>
+	<version>0.0.1</version>
+	<prerequisites>
+		<maven>3.0.4</maven>
+	</prerequisites>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<derby.version>10.9.1.0</derby.version>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.hamcrest</groupId>
+			<artifactId>hamcrest-all</artifactId>
+			<version>1.3</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+		<!-- be sure not to load an old version of junit-dep by accident -->
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit-dep</artifactId>
+			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.eclipse.persistence</groupId>
+			<artifactId>eclipselink</artifactId>
+			<version>2.4.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.derby</groupId>
+			<artifactId>derby</artifactId>
+			<version>${derby.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.derby</groupId>
+			<artifactId>derbyclient</artifactId>
+			<version>${derby.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.carrotsearch</groupId>
+			<artifactId>java-sizeof</artifactId>
+			<version>0.0.3</version>
+		</dependency>
+	</dependencies>
+
+	<repositories>
+		<repository>
+			<id>glassfish-repository</id>
+			<url>http://download.java.net/maven/glassfish</url>
+		</repository>
+		<repository>
+			<id>maven2-repository.dev.java.net</id>
+			<name>Java.net Repository for Maven</name>
+			<url>http://download.java.net/maven/2</url>
+		</repository>
+		<repository>
+			<name>jboss-public</name>
+			<id>jboss-public</id>
+			<url>https://repository.jboss.org/nexus/content/repositories/</url>
+		</repository>
+		<repository>
+			<id>EclipseLink Build Repo</id>
+			<url>http://maven.eclipse.org/nexus/content/groups/build/</url>
+		</repository>
+	</repositories>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>2.4</version>
+				<configuration>
+					<source>1.7</source>
+					<target>1.7</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>

src/main/java/com/hascode/tutorial/app/Main.java

+package com.hascode.tutorial.app;
+
+import static com.carrotsearch.sizeof.RamUsageEstimator.sizeOf;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+import com.hascode.tutorial.dto.SimpleBook;
+import com.hascode.tutorial.entity.Book;
+
+public class Main {
+	private EntityManagerFactory emf;
+	private EntityManager em;
+	private EntityTransaction tx;
+
+	public static void main(final String[] args) {
+		new Main().run();
+	}
+
+	private void run() {
+		emf = Persistence.createEntityManagerFactory("hascode-manual");
+		em = emf.createEntityManager();
+		tx = em.getTransaction();
+		tx.begin();
+		for (int i = 0; i < 100; i++) {
+			Book book = new Book();
+			book.setTitle("title" + i);
+			book.setAuthor("author" + i);
+			book.setDescription("description" + i);
+			book.setExcerpt("excerpt" + i);
+			book.setIsbn("isbn" + i);
+			book.setSomeOtherProp("prop" + i);
+			em.persist(book);
+		}
+		long amount = em.createNamedQuery("Book.countAll", Long.class)
+				.getSingleResult();
+		System.out.println(amount + " books in the store..");
+		tx.commit();
+
+		List<Book> books = em.createNamedQuery("Book.findAll", Book.class)
+				.getResultList(); // list of attached entities .. expensive ..
+		System.out.println(books.size() + " Book entities' guessed size is: "
+				+ sizeOf(books));
+		List<SimpleBook> bookDtos = em.createNamedQuery(
+				"Book.findAll.toSimpleBook", SimpleBook.class).getResultList();
+		System.out.println(bookDtos.size()
+				+ " Book entities' guessed size is: " + sizeOf(bookDtos));
+		em.close();
+		emf.close();
+	}
+}

src/main/java/com/hascode/tutorial/dto/SimpleBook.java

+package com.hascode.tutorial.dto;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "book")
+public class SimpleBook {
+	private final Long id;
+	private final String title;
+	private final String author;
+
+	public SimpleBook(final Long id, final String title, final String author) {
+		this.id = id;
+		this.title = title;
+		this.author = author;
+	}
+
+	public final Long getId() {
+		return id;
+	}
+
+	public final String getTitle() {
+		return title;
+	}
+
+	public final String getAuthor() {
+		return author;
+	}
+
+}

src/main/java/com/hascode/tutorial/entity/Book.java

+package com.hascode.tutorial.entity;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@Entity
+@NamedQueries({
+		@NamedQuery(name = "Book.findAll", query = "SELECT b FROM Book b"),
+		@NamedQuery(name = "Book.countAll", query = "SELECT COUNT(b) FROM Book b"),
+		@NamedQuery(name = "Book.findAll.toSimpleBook", query = "SELECT NEW com.hascode.tutorial.dto.SimpleBook(b.id, b.title, b.author) FROM Book b") })
+@XmlRootElement
+public class Book {
+	@Id
+	@GeneratedValue
+	private Long id;
+	private String title;
+	private String author;
+	private String isbn;
+	private String description;
+	private String excerpt;
+	private String someOtherProp;
+
+	public final Long getId() {
+		return id;
+	}
+
+	public final void setId(final Long id) {
+		this.id = id;
+	}
+
+	public final String getTitle() {
+		return title;
+	}
+
+	public final void setTitle(final String title) {
+		this.title = title;
+	}
+
+	public final String getAuthor() {
+		return author;
+	}
+
+	public final void setAuthor(final String author) {
+		this.author = author;
+	}
+
+	public final String getIsbn() {
+		return isbn;
+	}
+
+	public final void setIsbn(final String isbn) {
+		this.isbn = isbn;
+	}
+
+	public final String getDescription() {
+		return description;
+	}
+
+	public final void setDescription(final String description) {
+		this.description = description;
+	}
+
+	public final String getExcerpt() {
+		return excerpt;
+	}
+
+	public final void setExcerpt(final String excerpt) {
+		this.excerpt = excerpt;
+	}
+
+	public final String getSomeOtherProp() {
+		return someOtherProp;
+	}
+
+	public final void setSomeOtherProp(final String someOtherProp) {
+		this.someOtherProp = someOtherProp;
+	}
+}

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

+<?xml version="1.0" encoding="UTF-8"?>
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+	version="1.0">
+	<persistence-unit name="hascode-manual"
+		transaction-type="RESOURCE_LOCAL">
+		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+		<class>com.hascode.tutorial.entity.Book</class>
+		<properties>
+			<property name="eclipselink.target-database" value="DERBY" />
+			<property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
+			<property name="eclipselink.jdbc.url" value="jdbc:derby:test2db;create=true" />
+			<property name="eclipselink.jdbc.user" value="APPT" />
+			<property name="eclipselink.jdbc.password" value="APPT" />
+			<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
+			<property name="eclipselink.logging.level" value="FINE" />
+		</properties>
+	</persistence-unit>
+</persistence>