Commits

Micha Kops  committed af5dc77

Entities and first query example added.

  • Participants
  • Parent commits c8b9d36

Comments (0)

Files changed (6)

 			<artifactId>slf4j-log4j12</artifactId>
 			<version>1.6.1</version>
 		</dependency>
+		<dependency>
+			<groupId>org.eclipse.persistence</groupId>
+			<artifactId>eclipselink</artifactId>
+			<version>2.5.1</version>
+		</dependency>
+		<dependency>
+			<groupId>hsqldb</groupId>
+			<artifactId>hsqldb</artifactId>
+			<version>1.8.0.10</version>
+		</dependency>
 	</dependencies>
 
 	<build>

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

+package com.hascode.app;
+
+import java.util.Calendar;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+import com.hascode.entity.Author;
+import com.hascode.entity.Book;
+import com.hascode.entity.QBook;
+import com.hascode.entity.Tag;
+import com.mysema.query.jpa.impl.JPAQuery;
+
+public class Main {
+	public void run() {
+		EntityManager em = Persistence.createEntityManagerFactory("default")
+				.createEntityManager();
+		EntityTransaction tx = em.getTransaction();
+		tx.begin();
+		Calendar cal1 = Calendar.getInstance();
+		cal1.set(2010, 0, 0);
+
+		Calendar cal2 = Calendar.getInstance();
+		cal2.set(2011, 0, 0);
+
+		Author author = new Author("Some Guy");
+		em.persist(author);
+
+		Book b1 = new Book();
+		b1.setTitle("The big book of something");
+		b1.setAuthor(author);
+		b1.setPublished(cal2.getTime());
+		Tag horrorTag = new Tag("Horror");
+		Tag dramaTag = new Tag("Drama");
+		Tag scienceTag = new Tag("Science");
+		b1.addTag(horrorTag);
+		b1.addTag(dramaTag);
+		b1.addTag(scienceTag);
+
+		Book b2 = new Book();
+		b2.setTitle("Another book");
+		b2.setAuthor(author);
+
+		em.persist(b1);
+		em.persist(b2);
+
+		JPAQuery query = new JPAQuery(em);
+		QBook book = QBook.book;
+		Book b = query
+				.from(book)
+				.where(book.title.startsWith("The"),
+						book.published.after(cal1.getTime()),
+						book.author.name.eq("Some Guy"),
+						book.tags.contains(horrorTag)).uniqueResult(book);
+		System.out.println(b.toString());
+
+	}
+
+	public static void main(final String[] args) {
+		new Main().run();
+	}
+}

File src/main/java/com/hascode/entity/Author.java

 	@OneToMany
 	private final List<Book> books = new ArrayList<>();
 
+	public Author() {
+	}
+
+	public Author(final String name) {
+		this.name = name;
+	}
+
 	public final Long getId() {
 		return id;
 	}
 		return books;
 	}
 
+	@Override
+	public String toString() {
+		StringBuilder builder = new StringBuilder();
+		builder.append("Author [id=").append(id).append(", name=").append(name)
+				.append("]");
+		return builder.toString();
+	}
+
 }

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

 import java.util.Date;
 import java.util.List;
 
+import javax.persistence.CascadeType;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
+import javax.persistence.OneToMany;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 
 	@Temporal(TemporalType.DATE)
 	private Date published;
 
+	@OneToMany(cascade = CascadeType.ALL)
 	private final List<Tag> tags = new ArrayList<>();
 
 	public final Long getId() {
 	public final List<Tag> getTags() {
 		return tags;
 	}
+
+	public void addTag(final Tag tag) {
+		this.tags.add(tag);
+	}
+
+	@Override
+	public String toString() {
+		StringBuilder builder = new StringBuilder();
+		builder.append("Book [id=").append(id).append(", title=").append(title)
+				.append(", author=").append(author).append(", published=")
+				.append(published).append(", tags=").append(tags).append("]");
+		return builder.toString();
+	}
 }

File src/main/java/com/hascode/entity/Tag.java

 package com.hascode.entity;
 
-import javax.persistence.Embeddable;
+import javax.persistence.Entity;
+import javax.persistence.Id;
 
-@Embeddable
+@Entity
 public class Tag {
+	@Id
 	private String name;
 
+	public Tag() {
+	}
+
+	public Tag(final String name) {
+		this.name = name;
+	}
+
 	public final String getName() {
 		return name;
 	}
 	public final void setName(final String name) {
 		this.name = name;
 	}
+
+	@Override
+	public String toString() {
+		StringBuilder builder = new StringBuilder();
+		builder.append("Tag [name=").append(name).append("]");
+		return builder.toString();
+	}
 }

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

+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+	version="1.0">
+	<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
+		<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>
+	<exclude-unlisted-classes>false</exclude-unlisted-classes>
+	</persistence-unit>
+</persistence>