Micha Kops avatar Micha Kops committed 4362930

Serialization examples added.

Comments (0)

Files changed (3)

+		<dependency>
+			<groupId>com.thoughtworks.xstream</groupId>
+			<artifactId>xstream</artifactId>
+			<version>1.4.4</version>
+		</dependency>


 import static com.carrotsearch.sizeof.RamUsageEstimator.sizeOf;
 import java.util.List;
+import java.util.Vector;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Persistence;
+import javax.xml.bind.JAXBException;
 import com.hascode.tutorial.dto.SimpleBook;
 import com.hascode.tutorial.entity.Book;
+import com.thoughtworks.xstream.XStream;
 public class Main {
 	private EntityManagerFactory emf;
 	private EntityManager em;
 	private EntityTransaction tx;
-	public static void main(final String[] args) {
+	public static void main(final String[] args) throws JAXBException {
 		new Main().run();
-	private void run() {
+	private void run() throws JAXBException {
 		emf = Persistence.createEntityManagerFactory("hascode-manual");
 		em = emf.createEntityManager();
 		tx = em.getTransaction();
 		List<SimpleBook> bookDtos = em.createNamedQuery(
 				"Book.findAll.toSimpleBook", SimpleBook.class).getResultList();
-				+ " Book entities' guessed size is: " + sizeOf(bookDtos));
+				+ " SimpleBook entities' guessed size is: " + sizeOf(bookDtos));
+		printAsJson(books);
+		printAsJson(bookDtos);
+	private <T> void printAsJson(final List<T> list) throws JAXBException {
+		XStream xs = new XStream();
+		xs.alias("books", Vector.class);
+		xs.alias("book", Book.class);
+		xs.alias("book", SimpleBook.class);
+		System.out.println("serialized: " + xs.toXML(list));
+	}


 @XmlRootElement(name = "book")
 public class SimpleBook {
-	private final Long id;
-	private final String title;
-	private final String author;
+	private Long id;
+	private String title;
+	private String author;
+	public SimpleBook() {
+	}
 	public SimpleBook(final Long id, final String title, final String author) {
 		this.id = id;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.