From 4e35adb6b082293c01657975e425a931dface9e5 Mon Sep 17 00:00:00 2001
From: Mic <misvy@vmware.com>
Date: Mon, 14 Jan 2013 16:55:09 +0800
Subject: [PATCH] migrated to hsql 2.2.8 and fixed a couple of JPA issues

---
 pom.xml                                       |  2 +-
 .../samples/petclinic/BaseEntity.java         |  4 +++-
 .../samples/petclinic/Vet.java                |  5 ++++-
 .../petclinic/jpa/EntityManagerClinic.java    | 20 +++++--------------
 src/main/resources/db/hsqldb/initDB.sql       | 12 +++++------
 .../samples/petclinic/jpa/JpaClinicTests.java |  7 ++++---
 6 files changed, 23 insertions(+), 27 deletions(-)

diff --git a/pom.xml b/pom.xml
index a5606ea..8530395 100644
--- a/pom.xml
+++ b/pom.xml
@@ -85,7 +85,7 @@
 		<dependency>
 			<groupId>org.hsqldb</groupId>
 			<artifactId>hsqldb</artifactId>
-			<version>1.8.0.10</version>
+			<version>2.2.8</version>
 			<scope>runtime</scope>
 		</dependency>
 		<!-- MySQL JDBC Connector -->
diff --git a/src/main/java/org/springframework/samples/petclinic/BaseEntity.java b/src/main/java/org/springframework/samples/petclinic/BaseEntity.java
index 8913ab0..69cb3b4 100644
--- a/src/main/java/org/springframework/samples/petclinic/BaseEntity.java
+++ b/src/main/java/org/springframework/samples/petclinic/BaseEntity.java
@@ -1,5 +1,7 @@
 package org.springframework.samples.petclinic;
 
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.MappedSuperclass;
 
@@ -12,7 +14,7 @@ import javax.persistence.MappedSuperclass;
  */
 @MappedSuperclass
 public class BaseEntity {
-	@Id
+	@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
 	protected Integer id;
 	
 
diff --git a/src/main/java/org/springframework/samples/petclinic/Vet.java b/src/main/java/org/springframework/samples/petclinic/Vet.java
index e4250ae..5ef56fe 100644
--- a/src/main/java/org/springframework/samples/petclinic/Vet.java
+++ b/src/main/java/org/springframework/samples/petclinic/Vet.java
@@ -7,6 +7,8 @@ import java.util.List;
 import java.util.Set;
 
 import javax.persistence.Entity;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
 import javax.persistence.Table;
 import javax.xml.bind.annotation.XmlElement;
@@ -25,7 +27,8 @@ import org.springframework.beans.support.PropertyComparator;
 @Entity @Table(name="vets")
 public class Vet extends Person {
 
-	@ManyToMany
+	@ManyToMany @JoinTable (name="vet_specialties",joinColumns = @JoinColumn(name = "vet_id"), 
+										inverseJoinColumns= @JoinColumn(name = "specialty_id"))
 	private Set<Specialty> specialties;
 
 
diff --git a/src/main/java/org/springframework/samples/petclinic/jpa/EntityManagerClinic.java b/src/main/java/org/springframework/samples/petclinic/jpa/EntityManagerClinic.java
index d3fe8ef..e3c82fc 100644
--- a/src/main/java/org/springframework/samples/petclinic/jpa/EntityManagerClinic.java
+++ b/src/main/java/org/springframework/samples/petclinic/jpa/EntityManagerClinic.java
@@ -24,6 +24,7 @@ import org.springframework.dao.DataAccessException;
  * @author Mike Keith
  * @author Rod Johnson
  * @author Sam Brannen
+ * @author Michael Isvy
  * @since 22.4.2006
  */
 @Repository
@@ -65,27 +66,16 @@ public class EntityManagerClinic implements Clinic {
 	}
 
 	public void storeOwner(Owner owner) {
-		// Consider returning the persistent object here, for exposing
-		// a newly assigned id using any persistence provider...
-		Owner merged = this.em.merge(owner);
-		this.em.flush();
-		owner.setId(merged.getId());
+		this.em.merge(owner);
+
 	}
 
 	public void storePet(Pet pet) {
-		// Consider returning the persistent object here, for exposing
-		// a newly assigned id using any persistence provider...
-		Pet merged = this.em.merge(pet);
-		this.em.flush();
-		pet.setId(merged.getId());
+		this.em.merge(pet);
 	}
 
 	public void storeVisit(Visit visit) {
-		// Consider returning the persistent object here, for exposing
-		// a newly assigned id using any persistence provider...
-		Visit merged = this.em.merge(visit);
-		this.em.flush();
-		visit.setId(merged.getId());
+		this.em.merge(visit);
 	}
 
 	public void deletePet(int id) throws DataAccessException {
diff --git a/src/main/resources/db/hsqldb/initDB.sql b/src/main/resources/db/hsqldb/initDB.sql
index a75bfbb..e6a8a19 100644
--- a/src/main/resources/db/hsqldb/initDB.sql
+++ b/src/main/resources/db/hsqldb/initDB.sql
@@ -1,12 +1,12 @@
 CREATE TABLE vets (
-	id INTEGER NOT NULL IDENTITY PRIMARY KEY,
+	id INTEGER IDENTITY PRIMARY KEY,
 	first_name VARCHAR(30),
 	last_name VARCHAR(30)
 );
 CREATE INDEX vets_last_name ON vets(last_name);
 
 CREATE TABLE specialties (
-	id INTEGER NOT NULL IDENTITY PRIMARY KEY,
+	id INTEGER IDENTITY PRIMARY KEY,
 	name VARCHAR(80)
 );
 CREATE INDEX specialties_name ON specialties(name);
@@ -19,13 +19,13 @@ alter table vet_specialties add constraint fk_vet_specialties_vets foreign key (
 alter table vet_specialties add constraint fk_vet_specialties_specialties foreign key (specialty_id) references specialties(id);
 
 CREATE TABLE types (
-	id INTEGER NOT NULL IDENTITY PRIMARY KEY,
+	id INTEGER IDENTITY PRIMARY KEY,
 	name VARCHAR(80)
 );
 CREATE INDEX types_name ON types(name);
 
 CREATE TABLE owners (
-	id INTEGER NOT NULL IDENTITY PRIMARY KEY,
+	id INTEGER IDENTITY PRIMARY KEY,
 	first_name VARCHAR(30),
 	last_name VARCHAR(30),
 	address VARCHAR(255),
@@ -35,7 +35,7 @@ CREATE TABLE owners (
 CREATE INDEX owners_last_name ON owners(last_name);
 
 CREATE TABLE pets (
-	id INTEGER NOT NULL IDENTITY PRIMARY KEY,
+	id INTEGER IDENTITY PRIMARY KEY,
 	name VARCHAR(30),
 	birth_date DATE,
 	type_id INTEGER NOT NULL,
@@ -46,7 +46,7 @@ alter table pets add constraint fk_pets_types foreign key (type_id) references t
 CREATE INDEX pets_name ON pets(name);
 
 CREATE TABLE visits (
-	id INTEGER NOT NULL IDENTITY PRIMARY KEY,
+	id INTEGER IDENTITY PRIMARY KEY,
 	pet_id INTEGER NOT NULL,
 	visit_date DATE,
 	description VARCHAR(255)
diff --git a/src/test/java/org/springframework/samples/petclinic/jpa/JpaClinicTests.java b/src/test/java/org/springframework/samples/petclinic/jpa/JpaClinicTests.java
index ae4acc1..d4ccd5d 100644
--- a/src/test/java/org/springframework/samples/petclinic/jpa/JpaClinicTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/jpa/JpaClinicTests.java
@@ -23,6 +23,7 @@ import org.springframework.samples.petclinic.Visit;
 import org.springframework.samples.petclinic.util.EntityUtils;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * <p>
@@ -65,7 +66,7 @@ public class JpaClinicTests {
 	}
 
 
-	@Test
+	@Test @Transactional
 	public void testGetVets() {
 		Collection<Vet> vets = this.clinic.getVets();
 		
@@ -141,7 +142,7 @@ public class JpaClinicTests {
 		assertEquals("Peter", p6.getOwner().getFirstName());
 	}
 
-	@Test
+	@Test  @Transactional
 	public void testInsertPet() {
 		Owner o6 = this.clinic.findOwner(6);
 		int found = o6.getPets().size();
@@ -168,7 +169,7 @@ public class JpaClinicTests {
 		assertEquals(old + "X", p7.getName());
 	}
 
-	@Test
+	@Test  @Transactional
 	public void testInsertVisit() {
 		Pet p7 = this.clinic.findPet(7);
 		int found = p7.getVisits().size();
-- 
GitLab