diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java index 7a1fdf857..a78c5a2d0 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java @@ -124,7 +124,7 @@ public class HandlerRelationMapTableFieldStrategy ValueMapping vm = field.getElementMapping(); ForeignKey fk = vm.getForeignKey(val); if (fk == null) - return null; + return joins; return joins.joinRelation(field.getName(), fk, val, vm.getSelectSubclasses(), false, false); } @@ -322,12 +322,15 @@ public class HandlerRelationMapTableFieldStrategy throw RelationStrategies.unjoinable(val); return joins; } + ForeignKey fk = val.getForeignKey(clss[0]); + if (fk == null) + return joins; if (forceOuter) return joins.outerJoinRelation(field.getName(), - val.getForeignKey(clss[0]), clss[0], val.getSelectSubclasses(), + fk, clss[0], val.getSelectSubclasses(), false, false); return joins.joinRelation(field.getName(), - val.getForeignKey(clss[0]), clss[0], val.getSelectSubclasses(), + fk, clss[0], val.getSelectSubclasses(), false, false); } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationRelationMapTableFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationRelationMapTableFieldStrategy.java index 8aa28e1a5..71bf23b64 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationRelationMapTableFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationRelationMapTableFieldStrategy.java @@ -167,7 +167,7 @@ public class RelationRelationMapTableFieldStrategy ValueMapping vm = field.getElementMapping(); ForeignKey fk = vm.getForeignKey(val); if (fk == null) - return null; + return joins; return joins.joinRelation(field.getName(), fk, val, vm.getSelectSubclasses(), false, false); } @@ -364,12 +364,15 @@ public class RelationRelationMapTableFieldStrategy throw RelationStrategies.unjoinable(val); return joins; } + ForeignKey fk = val.getForeignKey(clss[0]); + if (fk == null) + return joins; if (forceOuter) return joins.outerJoinRelation(field.getName(), - val.getForeignKey(clss[0]), clss[0], val.getSelectSubclasses(), + fk, clss[0], val.getSelectSubclasses(), false, false); return joins.joinRelation(field.getName(), - val.getForeignKey(clss[0]), clss[0], val.getSelectSubclasses(), + fk, clss[0], val.getSelectSubclasses(), false, false); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Company1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Company1.java index c35c97bf8..2031ad209 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Company1.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Company1.java @@ -24,6 +24,7 @@ import java.util.Map; import javax.persistence.*; @Entity +@Table(schema="embed", name="Comp1") public class Company1 { @Id int id; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Company2.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Company2.java index 0aad92ec1..5f7ebd2e0 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Company2.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Company2.java @@ -24,6 +24,7 @@ import java.util.Map; import javax.persistence.*; @Entity +@Table(schema="embed", name="Comp2") public class Company2 { @Id int id; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Division.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Division.java index 64849c930..12ce53cac 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Division.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Division.java @@ -20,8 +20,10 @@ package org.apache.openjpa.persistence.embed; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; @Entity +@Table(schema="embed", name="Division") public class Division { @Id int id; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/VicePresident.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/VicePresident.java index 692c2e5ab..933d26e66 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/VicePresident.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/VicePresident.java @@ -21,6 +21,7 @@ package org.apache.openjpa.persistence.embed; import javax.persistence.*; @Entity +@Table(schema="embed", name="VP") public class VicePresident { @Id int id; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Compny1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Compny1.java new file mode 100644 index 000000000..fc0d6fbd2 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Compny1.java @@ -0,0 +1,51 @@ +/* + * 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_ex0; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="S27x0Comp1") +public class Compny1 { + @Id + int id; + + @OneToMany(targetEntity=VicePresident.class) + @MapKeyClass(Division.class) + Map orgs = new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getOrganization() { + return orgs; + } + + public void addToOrganization(Division division, VicePresident vp) { + orgs.put(division, vp); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Compny2.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Compny2.java new file mode 100644 index 000000000..d2f83007c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Compny2.java @@ -0,0 +1,50 @@ +/* + * 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_ex0; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +public class Compny2 { + @Id + int id; + + @OneToMany + Map orgs = + new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getOrganization() { + return orgs; + } + + public void addToOrganization(Division division, VicePresident vp) { + orgs.put(division, vp); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Division.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Division.java new file mode 100644 index 000000000..1cb231ad9 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Division.java @@ -0,0 +1,64 @@ +/* + * 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_ex0; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="S27x0Div") +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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Item1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Item1.java new file mode 100644 index 000000000..d57697e9a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Item1.java @@ -0,0 +1,51 @@ +/* + * 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_ex0; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="S27x0Itm1") +public class Item1 { + @Id + int id; + + @ElementCollection(targetClass=String.class) + @MapKeyClass(String.class) + Map images = new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getImages() { + return images; + } + + public void addImage(String imageName, String fileName) { + images.put(imageName, fileName); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Item2.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Item2.java new file mode 100644 index 000000000..27380eae0 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/Item2.java @@ -0,0 +1,50 @@ +/* + * 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_ex0; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="S27x0Itm2") +public class Item2 { + @Id + int id; + + @ElementCollection + Map images = new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getImages() { + return images; + } + + public void addImage(String imageName, String fileName) { + images.put(imageName, fileName); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/TestSpec10_1_27.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/TestSpec10_1_27.java new file mode 100644 index 000000000..707d6a4f5 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/TestSpec10_1_27.java @@ -0,0 +1,315 @@ +/* + * 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_ex0; + +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_27 extends SingleEMFTestCase { + public int numItems = 2; + public int numImagesPerItem = 3; + public int numCompany = 2; + public int numDivisionsPerCo = 2; + public int itemId = 1; + public int compId = 1; + public int divId = 1; + public int vpId = 1; + + public List namedQueries = new ArrayList(); + protected List sql = new ArrayList(); + protected int sqlCount; + + public void setUp() { + super.setUp(CLEAR_TABLES, + Compny1.class, Compny2.class, + Item1.class, Item2.class, + Division.class, VicePresident.class, + "openjpa.jdbc.JDBCListeners", + new JDBCListener[] { this.new Listener() }); + createObj(emf); + } + + public void testQueryQualifiedId() throws Exception { + EntityManager em = emf.createEntityManager(); + + String query = "select KEY(e) from Compny1 c, " + + " in (c.orgs) e order by c.id"; + List rs = em.createQuery(query).getResultList(); + Division d = (Division) rs.get(0); + + em.clear(); + String query4 = "select ENTRY(e) from Compny1 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())); + + query = "select KEY(e) from Compny2 c, " + + " in (c.orgs) e order by c.id"; + rs = em.createQuery(query).getResultList(); + d = (Division) rs.get(0); + + em.clear(); + query4 = "select ENTRY(e) from Compny2 c, " + + " in (c.orgs) e order by c.id"; + rs4 = em.createQuery(query4).getResultList(); + me = (Map.Entry) rs4.get(0); + + assertTrue(d.equals(me.getKey())); + + em.close(); + } + + public void testQueryObj() throws Exception { + queryObj(emf); + } + + 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 < numItems; i++) + createItem1(em, itemId++); + for (int i = 0; i < numItems; i++) + createItem2(em, itemId++); + for (int i = 0; i < numCompany; i++) + createCompany1(em, compId++); + for (int i = 0; i < numCompany; i++) + createCompany2(em, compId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public void createItem1(EntityManager em, int id) { + Item1 item = new Item1(); + item.setId(id); + for (int i = 0; i < numImagesPerItem; i++) { + item.addImage("image" + id + i, "file" + id + i); + } + em.persist(item); + } + + public void createItem2(EntityManager em, int id) { + Item2 item = new Item2(); + item.setId(id); + for (int i = 0; i < numImagesPerItem; i++) { + item.addImage("image" + id + i, "file" + id + i); + } + em.persist(item); + } + + public void createCompany1(EntityManager em, int id) { + Compny1 c = new Compny1(); + 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 void createCompany2(EntityManager em, int id) { + Compny2 c = new Compny2(); + 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(); + + Item1 item1 = em.find(Item1.class, 1); + assertItem1(item1); + + Item2 item2 = em.find(Item2.class, 4); + assertItem2(item2); + + Compny1 c1 = em.find(Compny1.class, 1); + assertCompany1(c1); + + Compny2 c2 = em.find(Compny2.class, 3); + assertCompany2(c2); + + Division d = em.find(Division.class, 1); + assertDivision(d); + + VicePresident vp = em.find(VicePresident.class, 1); + assertVicePresident(vp); + } + + public void assertItem1(Item1 item) { + int id = item.getId(); + Map images = item.getImages(); + Assert.assertEquals(numImagesPerItem, images.size()); + } + + public void assertItem2(Item2 item) { + int id = item.getId(); + Map images = item.getImages(); + Assert.assertEquals(numImagesPerItem, images.size()); + } + + public void assertCompany1(Compny1 c) { + int id = c.getId(); + Map organization = c.getOrganization(); + Assert.assertEquals(2,organization.size()); + } + + public void assertCompany2(Compny2 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) { + queryItem(emf); + queryCompany(emf); + queryDivision(emf); + queryVicePresident(emf); + } + + public void queryItem(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q1 = em.createQuery("select i from Item1 i"); + List is1 = q1.getResultList(); + for (Item1 item : is1){ + assertItem1(item); + } + + Query q2 = em.createQuery("select i from Item2 i"); + List is2 = q2.getResultList(); + for (Item2 item : is2){ + assertItem2(item); + } + tran.commit(); + em.close(); + } + + public void queryCompany(EntityManagerFactory emf) { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + Query q1 = em.createQuery("select c from Compny1 c"); + List cs1 = q1.getResultList(); + for (Compny1 c : cs1){ + assertCompany1(c); + } + Query q2 = em.createQuery("select c from Compny2 c"); + List cs2 = q2.getResultList(); + for (Compny2 c : cs2){ + assertCompany2(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++; + } + } + } +} \ No newline at end of file diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/VicePresident.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/VicePresident.java new file mode 100644 index 000000000..289b27216 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex0/VicePresident.java @@ -0,0 +1,51 @@ +package org.apache.openjpa.persistence.jdbc.maps.spec_10_1_27_ex0; + +import javax.persistence.*; + +@Entity +@Table(name="S27x0VP") +public class VicePresident { + @Id + int id; + + String name; + + @ManyToOne + Compny1 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 Compny1 getCompany() { + return co; + } + + public void setCompany(Compny1 co) { + this.co = co; + } + + public boolean equals(Object v) { + if (this == v) + return true; + if (!(v instanceof VicePresident)) + return false; + VicePresident o = (VicePresident) v; + if (this.id == o.getId() && + this.name.equals(o.getName())) + return true; + return false; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/Company.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/Company.java new file mode 100644 index 000000000..047d6c9dd --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/Company.java @@ -0,0 +1,59 @@ +/* + * 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_ex3; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="S27x3Comp") +public class Company { + @Id + int id; + + @OneToMany(targetEntity=VicePresident.class , mappedBy="co") + @MapKeyClass(Division.class) + Map orgs = new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getOrganization() { + return orgs; + } + + public void addToOrganization(Division division, VicePresident vp) { + orgs.put(division, vp); + } + + public void removeFromOrganization(Division d) { + orgs.remove(d); + } + + public VicePresident getOrganization(Division d) { + return (VicePresident)orgs.get(d); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/Division.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/Division.java new file mode 100644 index 000000000..101e56624 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/Division.java @@ -0,0 +1,64 @@ +/* + * 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_ex3; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="S27x3Div") +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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/TestSpec10_1_27_Ex3.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/TestSpec10_1_27_Ex3.java new file mode 100644 index 000000000..a70b65456 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/TestSpec10_1_27_Ex3.java @@ -0,0 +1,286 @@ +/* + * 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_ex3; + +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_Ex3 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, + 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) from Company c, " + + " in (c.orgs) e order by c.id"; + List rs = em.createQuery(query).getResultList(); + Division d = (Division) rs.get(0); + + 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())); + + em.clear(); + query = "select KEY(e) from Company c " + + " left join c.orgs e order by c.id"; + rs = em.createQuery(query).getResultList(); + d = (Division) rs.get(0); + + 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())); + + 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++) { + Division d = createDivision(em, divId++); + VicePresident vp = createVicePresident(em, vpId++); + c.addToOrganization(d, vp); + vp.setCompany(c); + 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); + + 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) { + Division d = (Division) key; + VicePresident vp = c.getOrganization(d); + vp.setCompany(null); + em.persist(vp); + c.removeFromOrganization(d); + break; + } + em.persist(c); + em.flush(); + tran.commit(); + + // add an element + tran.begin(); + Division d = createDivision(em, newDivId++); + VicePresident vp = createVicePresident(em, newVpId++); + c.addToOrganization(d, vp); + vp.setCompany(c); + em.persist(d); + em.persist(vp); + em.persist(c); + em.flush(); + tran.commit(); + + // modify an element + tran.begin(); + orgs = c.getOrganization(); + vp = c.getOrganization(d); + vp.setName("newNameAgain"); + 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 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++; + } + } + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/VicePresident.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/VicePresident.java new file mode 100644 index 000000000..34c134244 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex3/VicePresident.java @@ -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_ex3; + +import javax.persistence.*; + +@Entity +@Table(name="S27x3VP") +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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/Company.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/Company.java new file mode 100644 index 000000000..debec94bf --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/Company.java @@ -0,0 +1,59 @@ +/* + * 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_ex4; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="S27x4Comp") +public class Company { + @Id + int id; + + @OneToMany + 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); + } + + public void removeFromOrganization(Division d) { + organization.remove(d); + } + + public VicePresident getOrganization(Division d) { + return organization.get(d); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/Division.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/Division.java new file mode 100644 index 000000000..ed7b65b49 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/Division.java @@ -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_27_ex4; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="S27x4Div") +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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/TestSpec10_1_27_Ex4.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/TestSpec10_1_27_Ex4.java new file mode 100644 index 000000000..8f882db2b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/TestSpec10_1_27_Ex4.java @@ -0,0 +1,285 @@ +/* + * 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_ex4; + +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_Ex4 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, + 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) from Company c, " + + " in (c.organization) e order by c.id"; + List rs = em.createQuery(query).getResultList(); + Division d = (Division) rs.get(0); + + 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())); + + em.clear(); + query = "select KEY(e) from Company c " + + " left join c.organization e order by c.id"; + rs = em.createQuery(query).getResultList(); + d = (Division) rs.get(0); + + 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())); + + 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++) { + 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); + + 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) { + Division d = (Division)key; + VicePresident vp = c.getOrganization(d); + vp.setCompany(null); + em.persist(vp); + orgs.remove(d); + break; + } + em.persist(c); + em.flush(); + tran.commit(); + + // add an element + tran.begin(); + Division d = createDivision(em, newDivId++); + VicePresident vp = createVicePresident(em, newVpId++); + c.addToOrganization(d, vp); + vp.setCompany(c); + em.persist(d); + em.persist(vp); + em.persist(c); + em.flush(); + tran.commit(); + + // modify an element + tran.begin(); + vp = c.getOrganization(d); + vp.setName("newAgain"); + em.persist(vp); + em.persist(c); + 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 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++; + } + } + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/VicePresident.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/VicePresident.java new file mode 100644 index 000000000..c982e5f56 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex4/VicePresident.java @@ -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_ex4; + +import javax.persistence.*; + +@Entity +@Table(name="S27x4VP") +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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/Company.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/Company.java new file mode 100644 index 000000000..7bdd9c0cc --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/Company.java @@ -0,0 +1,59 @@ +/* + * 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_ex5; + +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="S27x5Comp") +public class Company { + @Id + int id; + + @OneToMany(mappedBy="co") + Map orgs = + new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getOrganization() { + return orgs; + } + + public void addToOrganization(Division division, VicePresident vp) { + orgs.put(division, vp); + } + + public void removeFromOrganization(Division d) { + orgs.remove(d); + } + + public VicePresident getOrganization(Division d) { + return orgs.get(d); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/Division.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/Division.java new file mode 100644 index 000000000..973bf97ed --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/Division.java @@ -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_27_ex5; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="S27x5Div") +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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/TestSpec10_1_27_Ex5.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/TestSpec10_1_27_Ex5.java new file mode 100644 index 000000000..11dd527c7 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/TestSpec10_1_27_Ex5.java @@ -0,0 +1,327 @@ +/* + * 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_ex5; + +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_Ex5 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, + 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) from Company c, " + + " in (c.orgs) e order by c.id"; + List rs = em.createQuery(query).getResultList(); + Division d = (Division) rs.get(0); + + 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())); + + em.clear(); + query = "select KEY(e) from Company c " + + " left join c.orgs e order by c.id"; + rs = em.createQuery(query).getResultList(); + d = (Division) rs.get(0); + + 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())); + + 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++) { + Division d = createDivision(em, divId++); + VicePresident vp = createVicePresident(em, vpId++); + c.addToOrganization(d, vp); + vp.setCompany(c); + 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); + + updateObj1(em, c); + +/* + updateObj(em, c); + em.close(); + + em = emf.createEntityManager(); + c = em.find(Company.class, 1); + assertCompany(c); + deleteObj(em, c); + em.close(); +*/ + } + + public void updateObj1(EntityManager em, Company c) { + EntityTransaction tran = em.getTransaction(); + // remove an element + tran.begin(); + Map orgs = c.getOrganization(); + VicePresident vp = null; + Set keys = orgs.keySet(); + for (Object key : keys) { + Division d = (Division)key; + vp = c.getOrganization(d); + vp.setCompany(null); + em.persist(vp); + c.removeFromOrganization(d); + break; + } + em.persist(c); + em.flush(); + tran.commit(); + + tran.begin(); + em.remove(c); + em.flush(); + tran.commit(); + + tran.begin(); + Company c1 = new Company(); + c1.setId(1); + Division d = createDivision(em, divId++); + c1.addToOrganization(d, vp); + vp.setCompany(c1); + em.persist(d); + em.persist(c1); + em.flush(); + tran.commit(); + } + + 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) { + Division d = (Division)key; + VicePresident vp = c.getOrganization(d); + vp.setCompany(null); + em.persist(vp); + c.removeFromOrganization(d); + break; + } + em.persist(c); + em.flush(); + tran.commit(); + + // add an element + tran.begin(); + Division d = createDivision(em, newDivId++); + VicePresident vp = createVicePresident(em, newVpId++); + c.addToOrganization(d, vp); + vp.setCompany(c); + em.persist(d); + em.persist(vp); + em.persist(c); + em.flush(); + tran.commit(); + + // modify an element + tran.begin(); + vp = c.getOrganization(d); + vp.setName("newAgain"); + em.persist(vp); + em.persist(c); + 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 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++; + } + } + } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/VicePresident.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/VicePresident.java new file mode 100644 index 000000000..31ae0fadb --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex5/VicePresident.java @@ -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_ex5; + +import javax.persistence.*; + +@Entity +@Table(name="S27x5VP") +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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/Company.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/Company.java new file mode 100644 index 000000000..d9e82dc8a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/Company.java @@ -0,0 +1,59 @@ +/* + * 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.HashMap; +import java.util.Map; + +import javax.persistence.*; + +@Entity +@Table(name="S27x8Comp") +public class Company { + @Id + int id; + + @OneToMany(mappedBy="co") + Map orgs = + new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getOrganization() { + return orgs; + } + + public void addToOrganization(VicePresident vp, FileName fileName) { + orgs.put(fileName, vp); + } + + public void removeFromOrganization(FileName fileName) { + orgs.remove(fileName); + } + + public VicePresident getOrganization(FileName fileName) { + return orgs.get(fileName); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/FileName.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/FileName.java new file mode 100644 index 000000000..b967f2d24 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/FileName.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/TestSpec10_1_27_Ex8.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/TestSpec10_1_27_Ex8.java new file mode 100644 index 000000000..2f5b8526a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/TestSpec10_1_27_Ex8.java @@ -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++; + } + } + } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/VicePresident.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/VicePresident.java new file mode 100644 index 000000000..659826414 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_27_ex8/VicePresident.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Company.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Company.java new file mode 100644 index 000000000..436811c5a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Company.java @@ -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); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Division.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Division.java new file mode 100644 index 000000000..e692b2a33 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/Division.java @@ -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 diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/TestSpec10_1_29_Ex1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/TestSpec10_1_29_Ex1.java new file mode 100644 index 000000000..07bbcb5d6 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/TestSpec10_1_29_Ex1.java @@ -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++; + } + } + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/VicePresident.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/VicePresident.java new file mode 100644 index 000000000..b5048857f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex1/VicePresident.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Course.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Course.java new file mode 100644 index 000000000..e89d1fff9 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Course.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Semester.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Semester.java new file mode 100644 index 000000000..ecacbf90e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Semester.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Student.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Student.java new file mode 100644 index 000000000..bf78655e5 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/Student.java @@ -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); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/TestSpec10_1_29_Ex3.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/TestSpec10_1_29_Ex3.java new file mode 100644 index 000000000..5745ba619 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/spec_10_1_29_ex3/TestSpec10_1_29_Ex3.java @@ -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++; + } + } + } +} +