Commits

Micha Kops  committed 3357ade

initial release

  • Participants

Comments (0)

Files changed (13)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/>
+	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>jpa2-tutorial</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.maven.ide.eclipse.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.maven.ide.eclipse.maven2Nature</nature>
+	</natures>
+</projectDescription>

File .settings/org.eclipse.core.resources.prefs

+#Sun Oct 10 20:00:39 CEST 2010
+eclipse.preferences.version=1
+encoding/<project>=UTF-8

File .settings/org.eclipse.jdt.core.prefs

+#Sun Oct 10 12:02:38 CEST 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.source=1.6

File .settings/org.maven.ide.eclipse.prefs

+#Sat Oct 09 22:52:05 CEST 2010
+activeProfiles=
+eclipse.preferences.version=1
+fullBuildGoals=process-test-resources
+includeModules=false
+resolveWorkspaceProjects=true
+resourceFilterGoals=process-resources resources\:testResources
+skipCompilerPlugin=true
+version=1
+<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.jpa2</groupId>
+	<artifactId>jpa2-tutorial</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.apache.derby</groupId>
+			<artifactId>derbyclient</artifactId>
+			<version>10.6.1.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.derby</groupId>
+			<artifactId>derby</artifactId>
+			<version>10.6.1.0</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.8.1</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-entitymanager</artifactId>
+			<version>3.5.1-Final</version>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-simple</artifactId>
+			<version>1.5.10</version>
+		</dependency>
+	</dependencies>
+	<repositories>
+		<repository>
+			<id>maven2-repository.dev.java.net</id>
+			<name>Java.net Repository for Maven</name>
+			<url>http://download.java.net/maven/2/</url>
+			<layout>default</layout>
+		</repository>
+	</repositories>
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>

File src/main/java/com/hascode/tutorial/jpa2/Address.java

+package com.hascode.tutorial.jpa2;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class Address {
+	@Id
+	@GeneratedValue
+	private Long	id;
+	private String	street;
+	private String	city;
+	public Long getId() {
+		return id;
+	}
+	public void setId(Long id) {
+		this.id = id;
+	}
+	public String getStreet() {
+		return street;
+	}
+	public void setStreet(String street) {
+		this.street = street;
+	}
+	public String getCity() {
+		return city;
+	}
+	public void setCity(String city) {
+		this.city = city;
+	}
+}

File src/main/java/com/hascode/tutorial/jpa2/Bookmark.java

+package com.hascode.tutorial.jpa2;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class Bookmark {
+	@Id
+	@GeneratedValue
+	private Long	id;
+
+	private String	title;
+
+	private String	url;
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(String title) {
+		this.title = title;
+	}
+
+	public String getUrl() {
+		return url;
+	}
+
+	public void setUrl(String url) {
+		this.url = url;
+	}
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+}

File src/main/java/com/hascode/tutorial/jpa2/Person.java

+package com.hascode.tutorial.jpa2;
+
+import java.util.Date;
+import java.util.List;
+
+import javax.persistence.CollectionTable;
+import javax.persistence.ElementCollection;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+
+@Entity
+@NamedQueries({
+		@NamedQuery(name = "findAll", query = "SELECT p FROM Person p"),
+		@NamedQuery(name = "findByNickname", query = "SELECT p FROM Person p WHERE p.nickname=:name")})
+public class Person {
+	@Id
+	@GeneratedValue
+	private Long			id;
+
+	private String			nickname;
+
+	private String			email;
+
+	@Enumerated(EnumType.STRING)
+	private Sex				sex;
+
+	@Temporal(TemporalType.DATE)
+	private Date			birthday;
+
+	@ElementCollection()
+	@CollectionTable(name = "Hobbies")
+	private List<String>	hobbies;
+
+	@OneToOne
+	private Address			address;
+
+	@OneToMany
+	private List<Bookmark>	bookmarks;
+
+	@ManyToMany()
+	private List<Pet>		pets;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getNickname() {
+		return nickname;
+	}
+
+	public void setNickname(String nickname) {
+		this.nickname = nickname;
+	}
+
+	public String getEmail() {
+		return email;
+	}
+
+	public void setEmail(String email) {
+		this.email = email;
+	}
+
+	public Sex getSex() {
+		return sex;
+	}
+
+	public void setSex(Sex sex) {
+		this.sex = sex;
+	}
+
+	public Date getBirthday() {
+		return birthday;
+	}
+
+	public void setBirthday(Date birthday) {
+		this.birthday = birthday;
+	}
+
+	public List<String> getHobbies() {
+		return hobbies;
+	}
+
+	public void setHobbies(List<String> hobbies) {
+		this.hobbies = hobbies;
+	}
+
+	public Address getAddress() {
+		return address;
+	}
+
+	public void setAddress(Address address) {
+		this.address = address;
+	}
+
+	public List<Bookmark> getBookmarks() {
+		return bookmarks;
+	}
+
+	public void setBookmarks(List<Bookmark> bookmarks) {
+		this.bookmarks = bookmarks;
+	}
+
+	public List<Pet> getPets() {
+		return pets;
+	}
+
+	public void setPets(List<Pet> pets) {
+		this.pets = pets;
+	}
+
+}

File src/main/java/com/hascode/tutorial/jpa2/Pet.java

+package com.hascode.tutorial.jpa2;
+
+import java.util.List;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+
+@Entity
+public class Pet {
+	@Id
+	@GeneratedValue
+	private Long			id;
+
+	private String			name;
+
+	@ManyToMany(mappedBy = "pets")
+	private List<Person>	owner;
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public List<Person> getOwner() {
+		return owner;
+	}
+
+	public void setOwner(List<Person> owner) {
+		this.owner = owner;
+	}
+}

File src/main/java/com/hascode/tutorial/jpa2/Sex.java

+package com.hascode.tutorial.jpa2;
+
+public enum Sex {
+	MALE, FEMALE
+}

File src/test/java/com/hascode/tutorial/jpa2/PersonTest.java

+package com.hascode.tutorial.jpa2;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+import javax.persistence.Query;
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class PersonTest {
+	private static EntityManagerFactory	emf;
+	private static EntityManager		em;
+	private static EntityTransaction	tx;
+
+	@Before
+	public void initEntityManager() throws Exception {
+		emf = Persistence.createEntityManagerFactory("hascode-manual");
+		em = emf.createEntityManager();
+		tx = em.getTransaction();
+	}
+
+	@After
+	public void closeEntityManager() throws SQLException {
+		em.close();
+		emf.close();
+	}
+
+	@Test
+	public void testPersist() {
+		tx.begin();
+		Person person = new Person();
+		person.setNickname("mickey");
+		person.setEmail("mickey@testhost.moby");
+		em.persist(person);
+		tx.commit();
+
+		// look if the entity was persisted and got an id assigned
+		assertNotNull(person.getId());
+
+		// lookup entity
+		Long id = person.getId();
+		Person userFromDatabase = em.find(Person.class, id);
+		assertNotNull(userFromDatabase);
+		assertEquals("mickey", userFromDatabase.getNickname());
+		assertEquals("mickey@testhost.moby", userFromDatabase.getEmail());
+	}
+
+	@Test
+	public void testEnum() {
+		tx.begin();
+		Person person = new Person();
+		person.setNickname("mickey");
+		person.setSex(Sex.FEMALE);
+		em.persist(person);
+		tx.commit();
+
+		Long id = person.getId();
+		Person userFromDatabase = em.find(Person.class, id);
+
+		assertEquals(Sex.FEMALE, userFromDatabase.getSex());
+	}
+
+	@Test
+	public void testDate() {
+		tx.begin();
+		final Date date = new Date();
+		Person person = new Person();
+		person.setNickname("mickey");
+		person.setBirthday(date);
+		em.persist(person);
+		tx.commit();
+
+		Long id = person.getId();
+		Person userFromDatabase = em.find(Person.class, id);
+
+		assertEquals(date, userFromDatabase.getBirthday());
+	}
+
+	@Test
+	public void testHobbies() {
+		tx.begin();
+		Person person = new Person();
+		person.setNickname("mickey");
+		final List<String> hobbies = new ArrayList<String>();
+		hobbies.add("Coding");
+		hobbies.add("Not sleeping");
+		hobbies.add("drinking coffee");
+		hobbies.add("living the cliché");
+		person.setHobbies(hobbies);
+		em.persist(person);
+		tx.commit();
+
+		Long id = person.getId();
+		Person userFromDatabase = em.find(Person.class, id);
+
+		assertEquals(4, userFromDatabase.getHobbies().size());
+	}
+
+	@Test
+	public void testAddress() {
+		tx.begin();
+
+		Address address = new Address();
+		address.setCity("Paris");
+		address.setStreet("Rue de St. Denis");
+		em.persist(address);
+
+		Person person = new Person();
+		person.setNickname("mickey");
+		person.setAddress(address);
+		em.persist(person);
+		tx.commit();
+
+		Long id = person.getId();
+		Person userFromDatabase = em.find(Person.class, id);
+
+		assertEquals("Paris", userFromDatabase.getAddress().getCity());
+	}
+
+	@Test
+	public void testBookmarks() {
+		tx.begin();
+
+		Bookmark b1 = new Bookmark();
+		b1.setTitle("A website");
+		b1.setUrl("http://www.hascode.com");
+
+		Bookmark b2 = new Bookmark();
+		b2.setTitle("Another website");
+		b2.setUrl("http://www.hascode.com/tag/jpa2");
+		em.persist(b1);
+		em.persist(b2);
+
+		List<Bookmark> bookmarks = new ArrayList<Bookmark>();
+		bookmarks.add(b1);
+		bookmarks.add(b2);
+
+		Person person = new Person();
+		person.setNickname("mickey");
+		person.setBookmarks(bookmarks);
+		em.persist(person);
+		tx.commit();
+
+		Long id = person.getId();
+		Person userFromDatabase = em.find(Person.class, id);
+
+		assertEquals("A website", userFromDatabase.getBookmarks().get(0)
+				.getTitle());
+	}
+
+	@Test
+	public void testPet() {
+		tx.begin();
+
+		Pet pet1 = new Pet();
+		pet1.setName("Nanny the bunny");
+		Pet pet2 = new Pet();
+		pet2.setName("Doggie Dog");
+
+		List<Pet> pets = new ArrayList<Pet>();
+		pets.add(pet1);
+		pets.add(pet2);
+
+		Person p1 = new Person();
+		p1.setNickname("Mickey");
+		p1.setPets(pets);
+
+		Person p2 = new Person();
+		p2.setNickname("Minny");
+		p2.setPets(pets);
+
+		em.persist(pet1);
+		em.persist(pet2);
+		em.persist(p1);
+		em.persist(p2);
+		tx.commit();
+
+		assertNotNull(pet1.getId());
+
+		Person userFromDB = em.find(Person.class, p1.getId());
+		assertEquals(2, userFromDB.getPets().size());
+	}
+
+	@Test
+	public void testJPQL() {
+		tx.begin();
+
+		Bookmark b1 = new Bookmark();
+		b1.setTitle("Snoring for experts");
+		b1.setUrl("http://www.hascode.com");
+
+		em.persist(b1);
+
+		List<Bookmark> bookmarks = new ArrayList<Bookmark>();
+		bookmarks.add(b1);
+
+		Person hal = new Person();
+		hal.setNickname("HAL9000");
+		hal.setBookmarks(bookmarks);
+		em.persist(hal);
+		em.flush();
+		tx.commit();
+
+		// query with named parameters (i prefer this one)
+		Query query = em
+				.createQuery("SELECT p FROM Person p WHERE p.nickname=:name");
+		query.setParameter("name", "HAL9000");
+		Person p1 = (Person) query.getResultList().get(0);
+		assertEquals(hal.getId(), p1.getId());
+
+		// same with positional parameters
+		Query query2 = em
+				.createQuery("SELECT p FROM Person p WHERE p.nickname=?1");
+		query2.setParameter(1, "HAL9000");
+		Person p2 = (Person) query2.getResultList().get(0);
+		assertEquals(hal.getId(), p2.getId());
+
+		// an example using joins
+		Query query3 = em
+				.createQuery("SELECT p FROM Person p LEFT JOIN FETCH p.bookmarks b WHERE b.title=:title");
+		query3.setParameter("title", "Snoring for experts");
+		Person p3 = (Person) query3.getResultList().get(0);
+		assertEquals(hal.getId(), p3.getId());
+	}
+
+	@Test
+	public void testNamedQueries() {
+		tx.begin();
+		Person donald = new Person();
+		donald.setNickname("Donald");
+
+		Person ronald = new Person();
+		ronald.setNickname("Ronald");
+
+		em.persist(donald);
+		em.persist(ronald);
+		tx.commit();
+
+		List<Person> persons = em.createNamedQuery("findAll").getResultList();
+		assertEquals(2, persons.size());
+
+		Query q = em.createNamedQuery("findByNickname");
+		q.setParameter("name", "Ronald");
+		Person isItRonald = (Person) q.getResultList().get(0);
+		assertEquals(ronald.getId(), isItRonald.getId());
+	}
+
+	@Test
+	public void testCriteriaApi() {
+		tx.begin();
+		Person donald = new Person();
+		donald.setNickname("Donald");
+
+		Person ronald = new Person();
+		ronald.setNickname("Ronald");
+
+		em.persist(donald);
+		em.persist(ronald);
+		tx.commit();
+
+		CriteriaBuilder cb = em.getCriteriaBuilder();
+		CriteriaQuery<Person> cq = cb.createQuery(Person.class);
+		Root<Person> pr = cq.from(Person.class);
+		cq.where(cb.equal(pr.get("nickname"), "Ronald"));
+		TypedQuery<Person> typedQuery = em.createQuery(cq);
+		List<Person> persons = typedQuery.getResultList();
+		assertEquals(ronald.getId(), persons.get(0).getId());
+
+	}
+}

File src/test/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.hibernate.ejb.HibernatePersistence</provider>
+		<class>com.hascode.tutorial.jpa2.Person</class>
+		<class>com.hascode.tutorial.jpa2.Address</class>
+		<class>com.hascode.tutorial.jpa2.Bookmark</class>
+		<class>com.hascode.tutorial.jpa2.Pet</class>
+		<properties>
+			<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
+			<property name="javax.persistence.jdbc.url" value="jdbc:derby:hascode_testdb;create=true" />
+			<property name="hibernate.show_sql" value="true" />
+			<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
+			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
+		</properties>
+	</persistence-unit>
+</persistence>