You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by fa...@apache.org on 2009/02/21 08:29:40 UTC
svn commit: r746446 - in /openjpa/trunk:
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/
openjpa-kernel/src/main/java/org/apache/openjpa/kernel/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/
Author: faywang
Date: Sat Feb 21 07:29:40 2009
New Revision: 746446
URL: http://svn.apache.org/viewvc?rev=746446&view=rev
Log:
OPENJPA-931: nested embeddedId/MappedById support
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId1.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ObjectIdStateManager.java
Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java?rev=746446&r1=746445&r2=746446&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java Sat Feb 21 07:29:40 2009
@@ -153,6 +153,8 @@
OpenJPAStateManager embedSm = (OpenJPAStateManager)
((PersistenceCapable)cval).pcGetStateManager();
idx = toDataStoreValue1(embedSm, val, store, cols, rvals, idx);
+ } else if (cval instanceof ObjectIdStateManager) {
+ idx = toDataStoreValue1((ObjectIdStateManager)cval, val, store, cols, rvals, idx);
} else if (cval == null) {
idx = toDataStoreValue1(null, val, store, cols, rvals, idx);
}
Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ObjectIdStateManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ObjectIdStateManager.java?rev=746446&r1=746445&r2=746446&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ObjectIdStateManager.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ObjectIdStateManager.java Sat Feb 21 07:29:40 2009
@@ -693,17 +693,22 @@
return null;
FieldMetaData fmd = getMetaData().getField(field);
- if (fmd.getBackingMember() instanceof Field)
- return Reflection.get(_oid, (Field) fmd.getBackingMember());
- if (fmd.getBackingMember() instanceof Method)
- return Reflection.get(_oid, (Method) fmd.getBackingMember());
-
- if (fmd.getDefiningMetaData().getAccessType()
+ Object val = null;
+ if (fmd.getBackingMember() instanceof Field)
+ val = Reflection.get(_oid, (Field) fmd.getBackingMember());
+ else if (fmd.getBackingMember() instanceof Method)
+ val = Reflection.get(_oid, (Method) fmd.getBackingMember());
+ else if (fmd.getDefiningMetaData().getAccessType()
== ClassMetaData.ACCESS_FIELD)
- return Reflection.get(_oid, Reflection.findField(_oid.getClass(),
+ val = Reflection.get(_oid, Reflection.findField(_oid.getClass(),
fmd.getName(), true));
- return Reflection.get(_oid, Reflection.findGetter(_oid.getClass(),
+ else
+ val = Reflection.get(_oid, Reflection.findGetter(_oid.getClass(),
fmd.getName(), true));
+
+ if (fmd.getValue().getEmbeddedMetaData() != null)
+ return new ObjectIdStateManager(val, null, fmd);
+ return val;
}
/**
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java?rev=746446&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java Sat Feb 21 07:29:40 2009
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+import javax.persistence.ManyToOne;
+import javax.persistence.MappedById;
+import javax.persistence.OneToMany;
+
+import org.apache.openjpa.persistence.jdbc.VersionColumn;
+
+/**
+ * Entity used to test compound primary keys using entity as relationship to
+ * more than one level.
+ *
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+@VersionColumn
+public class Book1 implements Serializable {
+ @EmbeddedId
+ private BookId1 bid;
+
+ @OneToMany(cascade = CascadeType.ALL, mappedBy = "book")
+ private Set<Page1> pages = new HashSet<Page1>();
+
+ @MappedById("library")
+ @Column(nullable = false)
+ @ManyToOne
+ private Library1 library;
+
+ private String author;
+
+ public BookId1 getBid() {
+ return bid;
+ }
+
+ public void setBid(BookId1 bid) {
+ this.bid = bid;
+ }
+
+ public Library1 getLibrary() {
+ return library;
+ }
+
+ public void setLibrary(Library1 library) {
+ this.library = library;
+ }
+
+ public Page1 getPage(PageId1 pid) {
+ for (Page1 p: pages) {
+ if (p.getPid().equals(pid)) {
+ return p;
+ }
+ }
+ return null;
+ }
+
+ public void addPage(Page1 p) {
+ p.setBook(this);
+ pages.add(p);
+ }
+
+ public String getAuthor() {
+ return author;
+ }
+
+ public void setAuthor(String author) {
+ this.author = author;
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof Book1)) {
+ return false;
+ }
+
+ Book1 other = (Book1)o;
+
+ if (!getBid().equals(other.getBid())) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public int hashCode() {
+ return getBid().hashCode();
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId1.java?rev=746446&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/BookId1.java Sat Feb 21 07:29:40 2009
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+
+import javax.persistence.Embeddable;
+
+
+/**
+ * Entity identity used to test compound primary keys using entity as
+ * relationship to more than one level.
+ *
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+@Embeddable
+public final class BookId1 implements Serializable {
+ private String name;
+ private String library;
+
+ public BookId1() {}
+
+ public BookId1(String name, String library) {
+ this.name = name;
+ this.library = library;
+ }
+
+
+ public boolean equals(Object o) {
+ if (!(o instanceof BookId1)) {
+ return false;
+ }
+
+ BookId1 other = (BookId1)o;
+
+ if (!(getName().equals(other.getName()))) {
+ return false;
+ }
+
+ if (!getLibrary().equals(other.getLibrary())) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public int hashCode() {
+ return safeHash(getName()) * safeHash(getLibrary());
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getLibrary() {
+ return library;
+ }
+
+ public void setLibrary(String library) {
+ this.library = library;
+ }
+
+ private int safeHash(Object o) {
+ return (o == null) ? 31 : o.hashCode();
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java?rev=746446&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java Sat Feb 21 07:29:40 2009
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+import java.util.HashSet;
+import java.util.Set;
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+import org.apache.openjpa.persistence.jdbc.VersionColumn;
+
+/**
+ * Entity used to test compound primary keys using entity as relationship to
+ * more than one level.
+ *
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+@VersionColumn
+public class Library1 implements Serializable {
+ @Id
+ @Column(name="LIBRARY_NAME", nullable = false)
+ private String name;
+
+ @OneToMany(cascade = CascadeType.ALL, mappedBy = "library")
+ private Set<Book1> books = new HashSet<Book1>();
+
+ private String location;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Set<Book1> getBooks() {
+ return books;
+ }
+
+ public Book1 getBook(BookId1 bid) {
+ for (Book1 b: books) {
+ if (b.getBid().equals(bid)) {
+ return b;
+ }
+ }
+
+ return null;
+ }
+
+ public void addBook(Book1 book) {
+ book.setLibrary(this);
+ books.add(book);
+ }
+
+ public String getLocation() {
+ return location;
+ }
+
+ public void setLocation(String location) {
+ this.location = location;
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof Library1)) {
+ return false;
+ }
+
+ Library1 other = (Library1)o;
+
+ if (!getName().equals(other.getName())) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public int hashCode() {
+ return getName().hashCode();
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java?rev=746446&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java Sat Feb 21 07:29:40 2009
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.ManyToOne;
+import javax.persistence.MappedById;
+
+import org.apache.openjpa.persistence.jdbc.VersionColumn;
+
+/**
+ * Entity used to test compound primary keys using entity as relationship to
+ * more than one level.
+ *
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+@Entity
+
+@VersionColumn
+public class Page1 implements Serializable {
+ @EmbeddedId
+ private PageId1 pid;
+
+ @MappedById("book")
+ @Column(nullable = false)
+ @ManyToOne
+ @JoinColumns({
+ @JoinColumn(name="BOOK_LIBRARY_LIBRARY_NAME", referencedColumnName="LIBRARY_LIBRARY_NAME"),
+ @JoinColumn(name="BOOK_BOOK_NAME", referencedColumnName="BOOK_NAME")
+ })
+ private Book1 book;
+
+ public PageId1 getPid() {
+ return pid;
+ }
+
+ public void setPid(PageId1 pid) {
+ this.pid = pid;
+ }
+
+ public Book1 getBook() {
+ return book;
+ }
+
+ public void setBook(Book1 book) {
+ this.book = book;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java?rev=746446&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java Sat Feb 21 07:29:40 2009
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import java.io.Serializable;
+
+import javax.persistence.Embeddable;
+import javax.persistence.Embedded;
+
+
+/**
+ * Entity identity used to test compound primary keys using entity as
+ * relationship to more than one level.
+ *
+ * Test case and domain classes were originally part of the reported issue
+ * <A href="https://issues.apache.org/jira/browse/OPENJPA-207">OPENJPA-207</A>
+ *
+ * @author Jeffrey Blattman
+ * @author Pinaki Poddar
+ *
+ */
+
+@Embeddable
+public final class PageId1 implements Serializable {
+ private int number;
+ @Embedded
+ private BookId1 book;
+
+ public PageId1() {}
+
+ public PageId1(int number, BookId1 book) {
+ this.number = number;
+ this.book = book;
+ }
+
+
+ public int getNumber() {
+ return number;
+ }
+
+ public void setNumber(int number) {
+ this.number = number;
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof PageId1)) {
+ return false;
+ }
+
+ PageId1 other = (PageId1)o;
+
+ if (!(getNumber() == other.getNumber())) {
+ return false;
+ }
+
+ if (!getBook().equals(other.getBook())) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public int hashCode() {
+ return number * (book != null ? getBook().hashCode() : 31);
+ }
+
+
+ public BookId1 getBook() {
+ return book;
+ }
+
+ public void setBook(BookId1 book) {
+ this.book = book;
+ }
+}
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java?rev=746446&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java Sat Feb 21 07:29:40 2009
@@ -0,0 +1,220 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+
+/**
+ * This is a variation of TestMultipleLevelDerivedIdentity Using
+ * MappedById annotations.
+ * @author Fay Wang
+ *
+ */
+@SuppressWarnings("unchecked")
+public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase {
+ private static String LIBRARY_NAME = "LIB";
+ private static String BOOK_NAME = "foo";
+ private static int NUM_PAGES = 3;
+ public void setUp() throws Exception {
+ super.setUp(CLEAR_TABLES, Library1.class, Book1.class, Page1.class,
+ BookId1.class, PageId1.class,
+ "openjpa.RuntimeUnenhancedClasses", "unsupported");
+ create();
+ }
+
+ public void testPersist() {
+ create();
+ }
+
+ public void testQueryRootLevel() {
+ EntityManager em = emf.createEntityManager();
+ List<Library1> list = em.createQuery("SELECT p FROM Library1 p")
+ .getResultList();
+ assertFalse(list.isEmpty());
+ Library1 lib = (Library1) list.get(0);
+ BookId1 bid = new BookId1(BOOK_NAME, lib.getName());
+ Book1 b = lib.getBook(bid);
+ assertNotNull(b);
+
+ Page1 p = b.getPage(new PageId1(1, bid));
+ assertNotNull(p);
+ }
+
+ public void testQueryIntermediateLevel() {
+ EntityManager em = emf.createEntityManager();
+ List<Book1> list = em.createQuery("SELECT p FROM Book1 p")
+ .getResultList();
+ assertFalse(list.isEmpty());
+ Book1 book = list.get(0);
+ Library1 lib = book.getLibrary();
+ for (int i=1; i<=NUM_PAGES; i++) {
+ PageId1 pid = new PageId1(i, book.getBid());
+ Page1 page = book.getPage(pid);
+ assertNotNull(page);
+ assertEquals(book, page.getBook());
+ assertEquals(lib, page.getBook().getLibrary());
+ assertEquals(page, page.getBook().getPage(
+ new PageId1(pid.getNumber(), book.getBid())));
+ }
+ }
+
+ public void testQueryLeafLevel() {
+ EntityManager em = emf.createEntityManager();
+ List<Page1> list = em.createQuery("SELECT p FROM Page1 p")
+ .getResultList();
+ assertFalse(list.isEmpty());
+ Book1 book = list.get(0).getBook();
+ Library1 lib = book.getLibrary();
+ for (Page1 page : list) {
+ assertEquals(book, page.getBook());
+ assertEquals(lib, page.getBook().getLibrary());
+ assertEquals(page, page.getBook().
+ getPage(page.getPid()));
+ }
+ }
+
+ public void testFindRootNode() {
+ EntityManager em = emf.createEntityManager();
+ Library1 lib = em.find(Library1.class, LIBRARY_NAME);
+ assertNotNull(lib);
+ BookId1 bid = new BookId1(BOOK_NAME, lib.getName());
+ Book1 b = lib.getBook(bid);
+ assertNotNull(b);
+ PageId1 pid = new PageId1(1, bid);
+ assertNotNull(b.getPage(pid));
+ }
+
+ public void testFindIntermediateNode() {
+ EntityManager em = emf.createEntityManager();
+
+ BookId1 bookId = new BookId1();
+ bookId.setLibrary(LIBRARY_NAME);
+ bookId.setName(BOOK_NAME);
+ Book1 book = em.find(Book1.class, bookId);
+ assertNotNull(book);
+ }
+
+ public void testFindLeafNode() {
+ EntityManager em = emf.createEntityManager();
+
+ BookId1 bookId = new BookId1();
+ bookId.setLibrary(LIBRARY_NAME);
+ bookId.setName(BOOK_NAME);
+ PageId1 pageId = new PageId1();
+ pageId.setBook(bookId);
+ pageId.setNumber(2);
+ Page1 page = em.find(Page1.class, pageId);
+ assertNotNull(page);
+ }
+
+ public void testUpdate() {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ BookId1 bookId = new BookId1();
+ bookId.setLibrary(LIBRARY_NAME);
+ bookId.setName(BOOK_NAME);
+ Book1 book = em.find(Book1.class, bookId);
+ assertNotNull(book);
+ book.setAuthor("modifiy Author");
+ em.getTransaction().commit();
+ }
+
+ public void testDeleteRoot() {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ Library1 lib = em.find(Library1.class, LIBRARY_NAME);
+ em.remove(lib);
+ em.getTransaction().commit();
+
+ assertEquals(0, count(Library1.class));
+ assertEquals(0, count(Book1.class));
+ assertEquals(0, count(Page1.class));
+ }
+
+ public void testDeleteLeafObtainedByQuery() {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ Page1 page = (Page1)em.createQuery("SELECT p FROM Page1 p WHERE p.pid.number=2")
+ .getSingleResult();
+ assertNotNull(page);
+ em.remove(page);
+ em.getTransaction().commit();
+
+ assertEquals(1, count(Library1.class));
+ assertEquals(1, count(Book1.class));
+ assertEquals(NUM_PAGES-1, count(Page1.class));
+ }
+
+ public void testDeleteLeafObtainedByFind() {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ BookId1 bookId = new BookId1();
+ bookId.setLibrary(LIBRARY_NAME);
+ bookId.setName(BOOK_NAME);
+ PageId1 pageId = new PageId1();
+ pageId.setBook(bookId);
+ pageId.setNumber(2);
+ Page1 page = em.find(Page1.class, pageId);
+ assertNotNull(page);
+ em.remove(page);
+ em.getTransaction().commit();
+
+ assertEquals(1, count(Library1.class));
+ assertEquals(1, count(Book1.class));
+ assertEquals(NUM_PAGES-1, count(Page1.class));
+ }
+
+
+ /**
+ * Create a Library with a Book and three Pages.
+ */
+ public void create() {
+ if (count(Library1.class) > 0)
+ return;
+
+ EntityManager em = null;
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+
+ Library1 lib = new Library1();
+ lib.setName(LIBRARY_NAME);
+
+ Book1 book = new Book1();
+ BookId1 bid = new BookId1();
+ bid.setName(BOOK_NAME);
+ bid.setLibrary(lib.getName());
+ book.setBid(bid);
+ lib.addBook(book);
+ for (int i = 1; i <= NUM_PAGES; i++) {
+ Page1 page = new Page1();
+ PageId1 pid = new PageId1(i, bid);
+ page.setPid(pid);
+ book.addPage(page);
+ }
+ em.persist(lib);
+ em.getTransaction().commit();
+
+ em.clear();
+ }
+}