OPENJPA-931: nested embeddedId/MappedById support

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@746446 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-02-21 07:29:40 +00:00
parent cfd133447f
commit f72b68bfbb
8 changed files with 716 additions and 8 deletions

View File

@ -153,6 +153,8 @@ public abstract class EmbedValueHandler
OpenJPAStateManager embedSm = (OpenJPAStateManager) OpenJPAStateManager embedSm = (OpenJPAStateManager)
((PersistenceCapable)cval).pcGetStateManager(); ((PersistenceCapable)cval).pcGetStateManager();
idx = toDataStoreValue1(embedSm, val, store, cols, rvals, idx); 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) { } else if (cval == null) {
idx = toDataStoreValue1(null, val, store, cols, rvals, idx); idx = toDataStoreValue1(null, val, store, cols, rvals, idx);
} }

View File

@ -693,17 +693,22 @@ public class ObjectIdStateManager
return null; return null;
FieldMetaData fmd = getMetaData().getField(field); FieldMetaData fmd = getMetaData().getField(field);
Object val = null;
if (fmd.getBackingMember() instanceof Field) if (fmd.getBackingMember() instanceof Field)
return Reflection.get(_oid, (Field) fmd.getBackingMember()); val = Reflection.get(_oid, (Field) fmd.getBackingMember());
if (fmd.getBackingMember() instanceof Method) else if (fmd.getBackingMember() instanceof Method)
return Reflection.get(_oid, (Method) fmd.getBackingMember()); val = Reflection.get(_oid, (Method) fmd.getBackingMember());
else if (fmd.getDefiningMetaData().getAccessType()
if (fmd.getDefiningMetaData().getAccessType()
== ClassMetaData.ACCESS_FIELD) == ClassMetaData.ACCESS_FIELD)
return Reflection.get(_oid, Reflection.findField(_oid.getClass(), val = Reflection.get(_oid, Reflection.findField(_oid.getClass(),
fmd.getName(), true)); fmd.getName(), true));
return Reflection.get(_oid, Reflection.findGetter(_oid.getClass(), else
val = Reflection.get(_oid, Reflection.findGetter(_oid.getClass(),
fmd.getName(), true)); fmd.getName(), true));
if (fmd.getValue().getEmbeddedMetaData() != null)
return new ObjectIdStateManager(val, null, fmd);
return val;
} }
/** /**

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}