Return-Path: Delivered-To: apmail-openjpa-commits-archive@www.apache.org Received: (qmail 95254 invoked from network); 9 Mar 2009 17:09:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 9 Mar 2009 17:09:00 -0000 Received: (qmail 9057 invoked by uid 500); 9 Mar 2009 17:09:00 -0000 Delivered-To: apmail-openjpa-commits-archive@openjpa.apache.org Received: (qmail 9040 invoked by uid 500); 9 Mar 2009 17:09:00 -0000 Mailing-List: contact commits-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openjpa.apache.org Delivered-To: mailing list commits@openjpa.apache.org Received: (qmail 9031 invoked by uid 99); 9 Mar 2009 17:08:59 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 Mar 2009 10:08:59 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 Mar 2009 17:08:58 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 0DC5F2388975; Mon, 9 Mar 2009 17:08:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r751766 [2/2] - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persis... Date: Mon, 09 Mar 2009 17:08:36 -0000 To: commits@openjpa.apache.org From: fancy@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090309170838.0DC5F2388975@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/FileName.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/FileName.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/FileName.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/FileName.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,69 @@ +/* + * 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.jdbc.maps.spec_10_1_27_ex8; + +import javax.persistence.Embeddable; + +@Embeddable +public class FileName { + + String fName; + String lName; + + public FileName() {} + + public FileName(String fName, String lName) { + this.fName = fName; + this.lName = lName; + } + + public String getFName() { + return fName; + } + + public void setFName(String fName) { + this.fName = fName; + } + + public String getLName() { + return lName; + } + + public void setLName(String lName) { + this.lName = lName; + } + + public boolean equals(Object o) { + if (!(o instanceof FileName)) return false; + FileName other = (FileName) o; + if (fName.equals(other.fName) && + lName.equals(other.lName)) + return true; + return false; + } + + public int hashCode() { + int ret = 0; + if (lName != null) + ret += lName.hashCode(); + if (fName != null) + ret = 31 * ret + fName.hashCode(); + return ret; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/TestSpec10_1_27_Ex8.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/TestSpec10_1_27_Ex8.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/TestSpec10_1_27_Ex8.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/TestSpec10_1_27_Ex8.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,261 @@ +/* + * 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.jdbc.maps.spec_10_1_27_ex8; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import junit.framework.Assert; + +import org.apache.openjpa.lib.jdbc.AbstractJDBCListener; +import org.apache.openjpa.lib.jdbc.JDBCEvent; +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + + +public class TestSpec10_1_27_Ex8 extends SingleEMFTestCase { + public int numCompany = 2; + public int numDivisionsPerCo = 2; + public List namedQueries = new ArrayList(); + + public int compId = 1; + public int divId = 1; + public int vpId = 1; + + public int newDivId = 100; + public int newVpId = 100; + + protected List sql = new ArrayList(); + protected int sqlCount; + + public void setUp() { + super.setUp(CLEAR_TABLES, + Company.class, + FileName.class, + VicePresident.class, + "openjpa.jdbc.JDBCListeners", + new JDBCListener[] { this.new Listener() }); + createObj(emf); + } + + public void testQueryObj() throws Exception { + queryObj(emf); + } + + public void testQueryQualifiedId() throws Exception { + EntityManager em = emf.createEntityManager(); + + String query = "select KEY(e), e from Company c, " + + " in (c.orgs) e order by c.id"; + List rs = em.createQuery(query).getResultList(); + FileName d = (FileName) ((Object[]) rs.get(0))[0]; + VicePresident v = (VicePresident) ((Object[]) rs.get(0))[1]; + + em.clear(); + String query4 = "select ENTRY(e) from Company c, " + + " in (c.orgs) e order by c.id"; + List rs4 = em.createQuery(query4).getResultList(); + Map.Entry me = (Map.Entry) rs4.get(0); + + assertTrue(d.equals(me.getKey())); + assertEquals(v.getId(), ((VicePresident) me.getValue()).getId()); + + em.clear(); + query = "select KEY(e), e from Company c " + + " left join c.orgs e order by c.id"; + rs = em.createQuery(query).getResultList(); + d = (FileName) ((Object[]) rs.get(0))[0]; + v = (VicePresident) ((Object[]) rs.get(0))[1]; + + em.clear(); + query4 = "select ENTRY(e) from Company c " + + " left join c.orgs e order by c.id"; + rs4 = em.createQuery(query4).getResultList(); + me = (Map.Entry) rs4.get(0); + + assertTrue(d.equals(me.getKey())); + assertEquals(v.getId(), ((VicePresident) me.getValue()).getId()); + + em.close(); + } + + public List getSql() { + return sql; + } + + public int getSqlCount() { + return sqlCount; + } + + public void createObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numCompany; i++) + createCompany(em, compId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createCompany(EntityManager em, int id) { + Company c = new Company(); + c.setId(id); + for (int i = 0; i < numDivisionsPerCo; i++) { + VicePresident vp = createVicePresident(em, vpId++); + FileName fileName = new FileName("f" + vp.getId(), "l" + vp.getId()); + c.addToOrganization(vp, fileName); + vp.setCompany(c); + em.persist(vp); + } + em.persist(c); + } + + public VicePresident createVicePresident(EntityManager em, int id) { + VicePresident vp = new VicePresident(); + vp.setId(id); + vp.setName("vp" + id); + return vp; + } + + public void findObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + Company c = em.find(Company.class, 1); + assertCompany(c); + + VicePresident vp = em.find(VicePresident.class, 1); + assertVicePresident(vp); + + updateObj(em, c); + em.close(); + + em = emf.createEntityManager(); + c = em.find(Company.class, 1); + assertCompany(c); + deleteObj(em, c); + em.close(); + } + + public void updateObj(EntityManager em, Company c) { + EntityTransaction tran = em.getTransaction(); + + // remove an element + tran.begin(); + Map orgs = c.getOrganization(); + Set keys = orgs.keySet(); + for (Object key : keys) { + FileName name = (FileName) key; + VicePresident vp = c.getOrganization(name); + vp.setCompany(null); + c.removeFromOrganization(name); + em.persist(vp); + break; + } + em.persist(c); + em.flush(); + tran.commit(); + + // add an element + tran.begin(); + VicePresident vp = createVicePresident(em, newVpId++); + FileName fileName = new FileName("f" + vp.getId(), "l" + vp.getId()); + c.addToOrganization(vp, fileName); + vp.setCompany(c); + em.persist(vp); + em.persist(c); + em.flush(); + tran.commit(); + + // modify an element + tran.begin(); + vp = c.getOrganization(fileName); + vp.setName("NewVp" + vp.getId()); + em.persist(c); + em.persist(vp); + em.flush(); + tran.commit(); + } + + public void deleteObj(EntityManager em, Company c) { + EntityTransaction tran = em.getTransaction(); + tran.begin(); + em.remove(c); + tran.commit(); + } + + public void assertCompany(Company c) { + int id = c.getId(); + Map organization = c.getOrganization(); + Assert.assertEquals(2,organization.size()); + } + + public void assertVicePresident(VicePresident vp) { + int id = vp.getId(); + String name = vp.getName(); + } + + public void queryObj(EntityManagerFactory emf) { + queryCompany(emf); + queryVicePresident(emf); + } + + public void queryCompany(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select c from Company c"); + List cs = q.getResultList(); + for (Company c : cs){ + assertCompany(c); + } + tran.commit(); + em.close(); + } + + public void queryVicePresident(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select vp from VicePresident vp"); + List vps = q.getResultList(); + for (VicePresident vp : vps){ + assertVicePresident(vp); + } + tran.commit(); + em.close(); + } + + public class Listener extends AbstractJDBCListener { + @Override + public void beforeExecuteStatement(JDBCEvent event) { + if (event.getSQL() != null && sql != null) { + sql.add(event.getSQL()); + sqlCount++; + } + } + } +} + Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/VicePresident.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/VicePresident.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/VicePresident.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/VicePresident.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,57 @@ +/* + * 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.jdbc.maps.spec_10_1_27_ex8; + +import javax.persistence.*; + +@Entity +@Table(name="S27x8VP") +public class VicePresident { + @Id + int id; + + String name; + + @ManyToOne + Company co; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Company getCompany() { + return co; + } + + public void setCompany(Company co) { + this.co = co; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Company.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Company.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Company.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Company.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,56 @@ +/* + * 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.jdbc.maps.spec_10_1_29_ex1; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="T29x1Comp") +public class Company { + @Id + int id; + + @OneToMany //unidirectional + @JoinTable(name="COMPANY_ORGANIZATION", + joinColumns=@JoinColumn(name="COMPANY"), + inverseJoinColumns=@JoinColumn(name="VICEPRESIDENT") + ) + @MapKeyJoinColumn(name="DIVISION") + Map organization = + new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getOrganization() { + return organization; + } + + public void addToOrganization(Division division, VicePresident vp) { + organization.put(division, vp); + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Division.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Division.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Division.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Division.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,63 @@ +/* + * 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.jdbc.maps.spec_10_1_29_ex1; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="T29x1Div") +public class Division { + @Id + int id; + + String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean equals(Object o) { + Division d = (Division) o; + if (d.name.equals(name) && + d.getId() == id) + return true; + return false; + } + + public int hashCode() { + int ret = 0; + ret = ret * 31 + name.hashCode(); + ret = ret *31 + id; + return ret; + } +} \ No newline at end of file Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/TestSpec10_1_29_Ex1.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/TestSpec10_1_29_Ex1.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/TestSpec10_1_29_Ex1.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/TestSpec10_1_29_Ex1.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,230 @@ +/* + * 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.jdbc.maps.spec_10_1_29_ex1; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import junit.framework.Assert; + +import org.apache.openjpa.lib.jdbc.AbstractJDBCListener; +import org.apache.openjpa.lib.jdbc.JDBCEvent; +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestSpec10_1_29_Ex1 extends SingleEMFTestCase { + public int numCompany = 2; + public int numDivisionsPerCo = 2; + public List namedQueries = new ArrayList(); + + public int compId = 1; + public int divId = 1; + public int vpId = 1; + public void setUp() { + super.setUp(CLEAR_TABLES, + Company.class, + Division.class, + VicePresident.class, + "openjpa.jdbc.JDBCListeners", + new JDBCListener[] { this.new Listener() }); + createObj(emf); + } + + public void testQueryObj() throws Exception { + queryObj(emf); + } + + public void testQueryQualifiedId() throws Exception { + EntityManager em = emf.createEntityManager(); + + String query = "select KEY(e), e from Company c, " + + " in (c.organization) e order by c.id"; + List rs = em.createQuery(query).getResultList(); + Division d = (Division) ((Object[]) rs.get(0))[0]; + VicePresident v = (VicePresident) ((Object[]) rs.get(0))[1]; + + em.clear(); + String query4 = "select ENTRY(e) from Company c, " + + " in (c.organization) e order by c.id"; + List rs4 = em.createQuery(query4).getResultList(); + Map.Entry me = (Map.Entry) rs4.get(0); + + assertTrue(d.equals(me.getKey())); + assertEquals(v.getId(), ((VicePresident) me.getValue()).getId()); + + em.clear(); + query = "select KEY(e), e from Company c " + + " left join c.organization e order by c.id"; + rs = em.createQuery(query).getResultList(); + d = (Division) ((Object[]) rs.get(0))[0]; + v = (VicePresident) ((Object[]) rs.get(0))[1]; + + em.clear(); + query4 = "select ENTRY(e) from Company c " + + " left join c.organization e order by c.id"; + rs4 = em.createQuery(query4).getResultList(); + me = (Map.Entry) rs4.get(0); + + assertTrue(d.equals(me.getKey())); + assertEquals(v.getId(), ((VicePresident) me.getValue()).getId()); + + em.close(); + } + + protected List sql = new ArrayList(); + protected int sqlCount; + + public List getSql() { + return sql; + } + + public int getSqlCount() { + return sqlCount; + } + + public void createObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numCompany; i++) + createCompany(em, compId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createCompany(EntityManager em, int id) { + Company c = new Company(); + c.setId(id); + for (int i = 0; i < numDivisionsPerCo; i++) { + Division d = createDivision(em, divId++); + VicePresident vp = createVicePresident(em, vpId++); + c.addToOrganization(d, vp); + em.persist(d); + em.persist(vp); + } + em.persist(c); + } + + public Division createDivision(EntityManager em, int id) { + Division d = new Division(); + d.setId(id); + d.setName("d" + id); + return d; + } + + public VicePresident createVicePresident(EntityManager em, int id) { + VicePresident vp = new VicePresident(); + vp.setId(id); + vp.setName("vp" + id); + return vp; + } + + public void findObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + Company c = em.find(Company.class, 1); + assertCompany(c); + + Division d = em.find(Division.class, 1); + assertDivision(d); + + VicePresident vp = em.find(VicePresident.class, 1); + assertVicePresident(vp); + + em.close(); + } + + public void assertCompany(Company c) { + int id = c.getId(); + Map organization = c.getOrganization(); + Assert.assertEquals(2,organization.size()); + } + + public void assertDivision(Division d) { + int id = d.getId(); + String name = d.getName(); + } + + public void assertVicePresident(VicePresident vp) { + int id = vp.getId(); + String name = vp.getName(); + } + + + public void queryObj(EntityManagerFactory emf) { + queryCompany(emf); + queryDivision(emf); + queryVicePresident(emf); + } + + public void queryCompany(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select c from Company c"); + List cs = q.getResultList(); + for (Company c : cs){ + assertCompany(c); + } + tran.commit(); + em.close(); + } + + public void queryDivision(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select d from Division d"); + List ds = q.getResultList(); + for (Division d : ds){ + assertDivision(d); + } + tran.commit(); + em.close(); + } + + public void queryVicePresident(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select vp from VicePresident vp"); + List vps = q.getResultList(); + for (VicePresident vp : vps){ + assertVicePresident(vp); + } + tran.commit(); + em.close(); + } + + public class Listener extends AbstractJDBCListener { + @Override + public void beforeExecuteStatement(JDBCEvent event) { + if (event.getSQL() != null && sql != null) { + sql.add(event.getSQL()); + sqlCount++; + } + } + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/VicePresident.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/VicePresident.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/VicePresident.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/VicePresident.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,46 @@ +/* + * 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.jdbc.maps.spec_10_1_29_ex1; + +import javax.persistence.*; + +@Entity +@Table(name="T29x1VP") +public class VicePresident { + @Id + int id; + + String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Course.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Course.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Course.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Course.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,63 @@ +/* + * 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.jdbc.maps.spec_10_1_29_ex3; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="T29x3Cors") +public class Course { + @Id + int id; + + String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean equals(Object o) { + Course c = (Course) o; + if (c.name.equals(name) && + c.getId() == id) + return true; + return false; + } + + public int hashCode() { + int ret = 0; + ret = ret * 31 + name.hashCode(); + ret = ret *31 + id; + return ret; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Semester.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Semester.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Semester.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Semester.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,48 @@ +/* + * 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.jdbc.maps.spec_10_1_29_ex3; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="T29x3Sem") +public class Semester { + @Id + int id; + + String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Student.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Student.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Student.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Student.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,65 @@ +/* + * 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.jdbc.maps.spec_10_1_29_ex3; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="T29x3Stud") +public class Student { + @Id + int id; + + String name; + + @ManyToMany + @JoinTable(name="ENROLLMENTS", + joinColumns=@JoinColumn(name="STUDENT"), + inverseJoinColumns=@JoinColumn(name="SEMESTER")) + @MapKeyJoinColumn(name="COURSE") + Map enrollment = + new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getEnrollment() { + return enrollment; + } + + public void addToEnrollment(Course course, Semester semester) { + enrollment.put(course, semester); + } +} Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/TestSpec10_1_29_Ex3.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/TestSpec10_1_29_Ex3.java?rev=751766&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/TestSpec10_1_29_Ex3.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/TestSpec10_1_29_Ex3.java Mon Mar 9 17:08:34 2009 @@ -0,0 +1,231 @@ +/* + * 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.jdbc.maps.spec_10_1_29_ex3; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import junit.framework.Assert; + +import org.apache.openjpa.lib.jdbc.AbstractJDBCListener; +import org.apache.openjpa.lib.jdbc.JDBCEvent; +import org.apache.openjpa.lib.jdbc.JDBCListener; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestSpec10_1_29_Ex3 extends SingleEMFTestCase { + public int numStudents = 2; + public int numCoursesPerStudent = 2; + public List namedQueries = new ArrayList(); + + public int studentId = 1; + public int courseId = 1; + public int semesterId = 1; + + protected List sql = new ArrayList(); + protected int sqlCount; + + public void setUp() { + super.setUp(CLEAR_TABLES, + Course.class, + Semester.class, + Student.class, + "openjpa.jdbc.JDBCListeners", + new JDBCListener[] { this.new Listener() }); + createObj(emf); + } + + public void testQueryObj() throws Exception { + queryObj(emf); + } + + public void testQueryQualifiedId() throws Exception { + EntityManager em = emf.createEntityManager(); + + String query = "select KEY(e), e from Student s, " + + " in (s.enrollment) e order by s.id"; + List rs = em.createQuery(query).getResultList(); + Course c = (Course) ((Object[]) rs.get(0))[0]; + Semester s = (Semester) ((Object[]) rs.get(0))[1]; + + em.clear(); + String query4 = "select ENTRY(e) from Student s, " + + " in (s.enrollment) e order by s.id"; + List rs4 = em.createQuery(query4).getResultList(); + Map.Entry me = (Map.Entry) rs4.get(0); + + assertTrue(c.equals(me.getKey())); + assertEquals(s.getId(), ((Semester) me.getValue()).getId()); + + em.clear(); + query = "select KEY(e), e from Student s " + + " left join s.enrollment e order by s.id"; + rs = em.createQuery(query).getResultList(); + c = (Course) ((Object[]) rs.get(0))[0]; + s = (Semester) ((Object[]) rs.get(0))[1]; + + em.clear(); + query4 = "select ENTRY(e) from Student s " + + " left join s.enrollment e order by s.id"; + rs4 = em.createQuery(query4).getResultList(); + me = (Map.Entry) rs4.get(0); + + assertTrue(c.equals(me.getKey())); + assertEquals(s.getId(), ((Semester) me.getValue()).getId()); + + em.close(); + } + + public List getSql() { + return sql; + } + + public int getSqlCount() { + return sqlCount; + } + + public void createObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numStudents; i++) + createStudent(em, studentId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createStudent(EntityManager em, int id) { + Student s = new Student(); + s.setId(id); + for (int i = 0; i < numCoursesPerStudent; i++) { + Course c = createCourse(em, courseId++); + Semester semester = createSemester(em, semesterId++); + s.addToEnrollment(c, semester); + em.persist(c); + em.persist(semester); + } + em.persist(s); + } + + public Course createCourse(EntityManager em, int id) { + Course c = new Course(); + c.setId(id); + c.setName("s" + id); + return c; + } + + public Semester createSemester(EntityManager em, int id) { + Semester s = new Semester(); + s.setId(id); + s.setName("s" + id); + return s; + } + + public void findObj(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + Student s = em.find(Student.class, 1); + assertStudent(s); + + Course c = em.find(Course.class, 1); + assertCourse(c); + + Semester sm = em.find(Semester.class, 1); + assertSemester(sm); + + em.close(); + } + + public void assertStudent(Student s) { + int id = s.getId(); + Map enrollment = s.getEnrollment(); + Assert.assertEquals(2, enrollment.size()); + } + + public void assertCourse(Course c) { + long id = c.getId(); + String name = c.getName(); + } + + public void assertSemester(Semester s) { + long id = s.getId(); + String name = s.getName(); + } + + public void queryObj(EntityManagerFactory emf) { + queryStudent(emf); + queryCourse(emf); + querySemester(emf); + } + + public void queryStudent(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select s from Student s"); + List ss = q.getResultList(); + for (Student s : ss){ + assertStudent(s); + } + tran.commit(); + em.close(); + } + + public void queryCourse(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select c from Course c"); + List cs = q.getResultList(); + for (Course c : cs){ + assertCourse(c); + } + tran.commit(); + em.close(); + } + + public void querySemester(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q = em.createQuery("select s from Semester s"); + List ss = q.getResultList(); + for (Semester s : ss){ + assertSemester(s); + } + tran.commit(); + em.close(); + } + + public class Listener extends AbstractJDBCListener { + @Override + public void beforeExecuteStatement(JDBCEvent event) { + if (event.getSQL() != null && sql != null) { + sql.add(event.getSQL()); + sqlCount++; + } + } + } +} +