OPENJPA-967 JPA2 Query support for selection of KEY, VALUE, ENTRY of a Map value

more test scenarios for Map having @MapKeyClass or @MapKeyJoinColumn annotations

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@751766 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Catalina Wei 2009-03-09 17:08:34 +00:00
parent 23b0b87674
commit de5e6c9ba9
37 changed files with 3333 additions and 6 deletions

View File

@ -124,7 +124,7 @@ public class HandlerRelationMapTableFieldStrategy
ValueMapping vm = field.getElementMapping(); ValueMapping vm = field.getElementMapping();
ForeignKey fk = vm.getForeignKey(val); ForeignKey fk = vm.getForeignKey(val);
if (fk == null) if (fk == null)
return null; return joins;
return joins.joinRelation(field.getName(), fk, val, return joins.joinRelation(field.getName(), fk, val,
vm.getSelectSubclasses(), false, false); vm.getSelectSubclasses(), false, false);
} }
@ -322,12 +322,15 @@ public class HandlerRelationMapTableFieldStrategy
throw RelationStrategies.unjoinable(val); throw RelationStrategies.unjoinable(val);
return joins; return joins;
} }
ForeignKey fk = val.getForeignKey(clss[0]);
if (fk == null)
return joins;
if (forceOuter) if (forceOuter)
return joins.outerJoinRelation(field.getName(), return joins.outerJoinRelation(field.getName(),
val.getForeignKey(clss[0]), clss[0], val.getSelectSubclasses(), fk, clss[0], val.getSelectSubclasses(),
false, false); false, false);
return joins.joinRelation(field.getName(), return joins.joinRelation(field.getName(),
val.getForeignKey(clss[0]), clss[0], val.getSelectSubclasses(), fk, clss[0], val.getSelectSubclasses(),
false, false); false, false);
} }

View File

@ -167,7 +167,7 @@ public class RelationRelationMapTableFieldStrategy
ValueMapping vm = field.getElementMapping(); ValueMapping vm = field.getElementMapping();
ForeignKey fk = vm.getForeignKey(val); ForeignKey fk = vm.getForeignKey(val);
if (fk == null) if (fk == null)
return null; return joins;
return joins.joinRelation(field.getName(), fk, val, return joins.joinRelation(field.getName(), fk, val,
vm.getSelectSubclasses(), false, false); vm.getSelectSubclasses(), false, false);
} }
@ -364,12 +364,15 @@ public class RelationRelationMapTableFieldStrategy
throw RelationStrategies.unjoinable(val); throw RelationStrategies.unjoinable(val);
return joins; return joins;
} }
ForeignKey fk = val.getForeignKey(clss[0]);
if (fk == null)
return joins;
if (forceOuter) if (forceOuter)
return joins.outerJoinRelation(field.getName(), return joins.outerJoinRelation(field.getName(),
val.getForeignKey(clss[0]), clss[0], val.getSelectSubclasses(), fk, clss[0], val.getSelectSubclasses(),
false, false); false, false);
return joins.joinRelation(field.getName(), return joins.joinRelation(field.getName(),
val.getForeignKey(clss[0]), clss[0], val.getSelectSubclasses(), fk, clss[0], val.getSelectSubclasses(),
false, false); false, false);
} }

View File

@ -24,6 +24,7 @@ import java.util.Map;
import javax.persistence.*; import javax.persistence.*;
@Entity @Entity
@Table(schema="embed", name="Comp1")
public class Company1 { public class Company1 {
@Id @Id
int id; int id;

View File

@ -24,6 +24,7 @@ import java.util.Map;
import javax.persistence.*; import javax.persistence.*;
@Entity @Entity
@Table(schema="embed", name="Comp2")
public class Company2 { public class Company2 {
@Id @Id
int id; int id;

View File

@ -20,8 +20,10 @@ package org.apache.openjpa.persistence.embed;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table;
@Entity @Entity
@Table(schema="embed", name="Division")
public class Division { public class Division {
@Id @Id
int id; int id;

View File

@ -21,6 +21,7 @@ package org.apache.openjpa.persistence.embed;
import javax.persistence.*; import javax.persistence.*;
@Entity @Entity
@Table(schema="embed", name="VP")
public class VicePresident { public class VicePresident {
@Id @Id
int id; int id;

View File

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

View File

@ -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<Division, VicePresident> orgs =
new HashMap<Division, VicePresident>();
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);
}
}

View File

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

View File

@ -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<String, String> getImages() {
return images;
}
public void addImage(String imageName, String fileName) {
images.put(imageName, fileName);
}
}

View File

@ -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<String, String> images = new HashMap<String, String>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Map<String, String> getImages() {
return images;
}
public void addImage(String imageName, String fileName) {
images.put(imageName, fileName);
}
}

View File

@ -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<String> namedQueries = new ArrayList<String>();
protected List<String> sql = new ArrayList<String>();
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<String> 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<Item1> is1 = q1.getResultList();
for (Item1 item : is1){
assertItem1(item);
}
Query q2 = em.createQuery("select i from Item2 i");
List<Item2> 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<Compny1> cs1 = q1.getResultList();
for (Compny1 c : cs1){
assertCompany1(c);
}
Query q2 = em.createQuery("select c from Compny2 c");
List<Compny2> 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<Division> 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<VicePresident> 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++;
}
}
}
}

View File

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

View File

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

View File

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

View File

@ -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<String> namedQueries = new ArrayList<String>();
public int compId = 1;
public int divId = 1;
public int vpId = 1;
public int newDivId = 100;
public int newVpId = 100;
protected List<String> sql = new ArrayList<String>();
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<String> 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<Company> 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<Division> 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<VicePresident> 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++;
}
}
}
}

View File

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

View File

@ -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<Division, VicePresident> organization =
new HashMap<Division, VicePresident>();
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);
}
}

View File

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

View File

@ -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<String> namedQueries = new ArrayList<String>();
public int compId = 1;
public int divId = 1;
public int vpId = 1;
public int newDivId = 100;
public int newVpId = 100;
protected List<String> sql = new ArrayList<String>();
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<String> 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<Company> 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<Division> 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<VicePresident> 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++;
}
}
}
}

View File

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

View File

@ -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<Division, VicePresident> orgs =
new HashMap<Division, VicePresident>();
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);
}
}

View File

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

View File

@ -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<String> namedQueries = new ArrayList<String>();
public int compId = 1;
public int divId = 1;
public int vpId = 1;
public int newDivId = 100;
public int newVpId = 100;
protected List<String> sql = new ArrayList<String>();
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<String> 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<Company> 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<Division> 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<VicePresident> 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++;
}
}
}
}

View File

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

View File

@ -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<FileName, VicePresident> orgs =
new HashMap<FileName, VicePresident>();
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);
}
}

View File

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

View File

@ -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<String> namedQueries = new ArrayList<String>();
public int compId = 1;
public int divId = 1;
public int vpId = 1;
public int newDivId = 100;
public int newVpId = 100;
protected List<String> sql = new ArrayList<String>();
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<String> 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<Company> 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<VicePresident> 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++;
}
}
}
}

View File

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

View File

@ -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<Division, VicePresident> organization =
new HashMap<Division, VicePresident>();
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);
}
}

View File

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

View File

@ -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<String> namedQueries = new ArrayList<String>();
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<String> sql = new ArrayList<String>();
protected int sqlCount;
public List<String> 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<Company> 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<Division> 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<VicePresident> 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++;
}
}
}
}

View File

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

View File

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

View File

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

View File

@ -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<Course, Semester> enrollment =
new HashMap<Course, Semester>();
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);
}
}

View File

@ -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<String> namedQueries = new ArrayList<String>();
public int studentId = 1;
public int courseId = 1;
public int semesterId = 1;
protected List<String> sql = new ArrayList<String>();
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<String> 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<Student> 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<Course> 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<Semester> 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++;
}
}
}
}