OPENJPA-879 JPA2 Query support for selecting KEY, ENTRY and VALUE of a Map value

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@750517 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Catalina Wei 2009-03-05 17:30:20 +00:00
parent dff6bf561b
commit 30ee661fdc
77 changed files with 7532 additions and 14 deletions

View File

@ -408,6 +408,14 @@ public class JDBCExpressionFactory
return new Type((Val) val);
}
public Value mapEntry(Value key, Value val) {
return new MapEntry((Val) key, (Val) val);
}
public Value mapKey(Value key, Value val) {
return new MapKey((Val) key, (Val) val);
}
public Value getObjectId(Value val) {
if (val instanceof Const)
return new ConstGetObjectId((Const) val);

View File

@ -0,0 +1,162 @@
/*
* 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.jdbc.kernel.exps;
import java.sql.SQLException;
import java.util.Map;
import org.apache.openjpa.jdbc.sql.Result;
import org.apache.openjpa.jdbc.sql.SQLBuffer;
import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.meta.ClassMetaData;
/**
* Returns the Map.Entry<K,V> of a map value.
*
* @author Catalina Wei
* @since 2.0.0
*/
public class MapEntry
extends AbstractVal {
private final Val _key;
private final Val _val;
private ClassMetaData _meta = null;
private Class _cast = null;
private Class _type = null;
/**
* Constructor. Provide the map value to operate on.
*/
public MapEntry(Val key, Val val) {
((PCPath) key).getKey();
_key = key;
_val = val;
}
/**
* Expression state.
*/
public static class EntryExpState
extends ExpState {
public ExpState key;
public ExpState val;
EntryExpState(ExpState key, ExpState val) {
this.key = key;
this.val = val;
}
}
public void appendTo(Select sel, ExpContext ctx, ExpState state,
SQLBuffer sql, int index) {
}
public void calculateValue(Select sel, ExpContext ctx, ExpState state,
Val other, ExpState otherState) {
_val.calculateValue(sel, ctx, state, other, otherState);
_key.calculateValue(sel, ctx, state, other, otherState);
}
public void groupBy(Select sel, ExpContext ctx, ExpState state) {
}
public ExpState initialize(Select sel, ExpContext ctx, int flags) {
ExpState val = _val.initialize(sel, ctx, flags);
ExpState key = _key.initialize(sel, ctx, flags);
return new EntryExpState(key, val);
}
public int length(Select sel, ExpContext ctx, ExpState state) {
return 1;
}
public Object load(ExpContext ctx, ExpState state, Result res)
throws SQLException {
EntryExpState estate = (EntryExpState) state;
Object key = _key.load(ctx, estate.key, res);
Object val = _val.load(ctx, estate.val, res);
return new Entry(key, val);
}
public void orderBy(Select sel, ExpContext ctx, ExpState state, boolean asc) {
}
public void select(Select sel, ExpContext ctx, ExpState state, boolean pks) {
EntryExpState estate = (EntryExpState) state;
_key.selectColumns(sel, ctx, estate.key, pks);
_val.selectColumns(sel, ctx, estate.val, pks);
}
public void selectColumns(Select sel, ExpContext ctx, ExpState state,
boolean pks) {
}
public ClassMetaData getMetaData() {
return _meta;
}
public Class getType() {
return Map.Entry.class;
}
public void setImplicitType(Class type) {
}
public void setMetaData(ClassMetaData meta) {
_meta = meta;
}
private class Entry<K,V> implements Map.Entry<K, V> {
private final K key;
private final V value;
public Entry(K k, V v) {
key = k;
value = v;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public V setValue(V v) {
throw new UnsupportedOperationException();
}
public boolean equals(Object other) {
if (other instanceof Map.Entry == false)
return false;
Map.Entry that = (Map.Entry)other;
return (this.key == null ?
that.getKey() == null : key.equals(that.getKey())) &&
(value == null ?
that.getValue() == null : value.equals(that.getValue()));
}
public int hashCode() {
return (key == null ? 0 : key.hashCode()) ^
(value == null ? 0 : value.hashCode());
}
}
}

View File

@ -0,0 +1,122 @@
/*
* 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.jdbc.kernel.exps;
import java.sql.SQLException;
import org.apache.openjpa.jdbc.sql.Result;
import org.apache.openjpa.jdbc.sql.SQLBuffer;
import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.meta.ClassMetaData;
/**
* Returns the key of a map value.
*
* @author Catalina Wei
* @since 2.0.0
*/
public class MapKey
extends AbstractVal {
private final Val _key;
private final Val _val;
private ClassMetaData _meta = null;
private Class _cast = null;
private Class _type = null;
/**
* Constructor. Provide the map value to operate on.
*/
public MapKey(Val key, Val val) {
((PCPath) key).getKey();
_key = key;
_val = val;
}
/**
* Expression state.
*/
public static class KeyExpState
extends ExpState {
public ExpState key;
public ExpState val;
KeyExpState(ExpState key, ExpState val) {
this.key = key;
this.val = val;
}
}
public void appendTo(Select sel, ExpContext ctx, ExpState state,
SQLBuffer sql, int index) {
}
public void calculateValue(Select sel, ExpContext ctx, ExpState state,
Val other, ExpState otherState) {
_val.calculateValue(sel, ctx, state, other, otherState);
_key.calculateValue(sel, ctx, state, other, otherState);
}
public void groupBy(Select sel, ExpContext ctx, ExpState state) {
}
public ExpState initialize(Select sel, ExpContext ctx, int flags) {
ExpState val = _val.initialize(sel, ctx, flags);
ExpState key = _key.initialize(sel, ctx, flags);
return new KeyExpState(key, val);
}
public int length(Select sel, ExpContext ctx, ExpState state) {
return 1;
}
public Object load(ExpContext ctx, ExpState state, Result res)
throws SQLException {
KeyExpState estate = (KeyExpState) state;
Object key = _key.load(ctx, estate.key, res);
return key;
}
public void orderBy(Select sel, ExpContext ctx, ExpState state, boolean asc) {
}
public void select(Select sel, ExpContext ctx, ExpState state, boolean pks) {
KeyExpState estate = (KeyExpState) state;
_key.selectColumns(sel, ctx, estate.key, pks);
}
public void selectColumns(Select sel, ExpContext ctx, ExpState state,
boolean pks) {
}
public ClassMetaData getMetaData() {
return _meta;
}
public Class getType() {
return _key.getType();
}
public void setImplicitType(Class type) {
}
public void setMetaData(ClassMetaData meta) {
_meta = meta;
}
}

View File

@ -25,11 +25,14 @@ import java.util.LinkedList;
import java.util.ListIterator;
import org.apache.commons.lang.ObjectUtils;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
import org.apache.openjpa.jdbc.meta.ClassMapping;
import org.apache.openjpa.jdbc.meta.Discriminator;
import org.apache.openjpa.jdbc.meta.FieldMapping;
import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
import org.apache.openjpa.jdbc.meta.ValueMapping;
import org.apache.openjpa.jdbc.meta.strats.HandlerRelationMapTableFieldStrategy;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ForeignKey;
import org.apache.openjpa.jdbc.schema.Schemas;
@ -40,6 +43,7 @@ import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.Filters;
import org.apache.openjpa.kernel.OpenJPAStateManager;
import org.apache.openjpa.kernel.StateManagerImpl;
import org.apache.openjpa.kernel.StoreContext;
import org.apache.openjpa.kernel.exps.CandidatePath;
import org.apache.openjpa.lib.util.Localizer;
@ -240,6 +244,8 @@ public class PCPath
if (pstate.field == null)
return _class;
if (_key) {
if (pstate.field.getKey().getValueMappedBy() != null)
return _class;
if (pstate.field.getKey().getTypeCode() == JavaTypes.PC)
return pstate.field.getKeyMapping().getTypeMapping();
return null;
@ -363,8 +369,8 @@ public class PCPath
public FieldMetaData last() {
Action act = lastFieldAction();
return (act == null) ? null : isXPath() ? _xmlfield :
(FieldMetaData) act.data;
return (act == null || act.op == Action.GET_KEY) ? null : isXPath() ?
_xmlfield : (FieldMetaData) act.data;
}
/**
@ -395,7 +401,8 @@ public class PCPath
if (act != null && act.op == Action.GET_XPATH)
return ((XMLMetaData) act.data).getType();
FieldMetaData fld = (act == null) ? null : (FieldMetaData) act.data;
FieldMetaData fld = (act == null || act.op == Action.GET_KEY) ? null :
(FieldMetaData) act.data;
boolean key = act != null && act.op == Action.GET_KEY;
if (fld != null) {
switch (fld.getDeclaredTypeCode()) {
@ -437,7 +444,7 @@ public class PCPath
Action action;
Variable var;
Iterator itr = (_actions == null) ? null : _actions.iterator();
FieldMapping field;
FieldMapping field = null;
while (itr != null && itr.hasNext()) {
action = (Action) itr.next();
@ -459,7 +466,13 @@ public class PCPath
} else {
// move past the previous field, if any
field = (action.op == Action.GET_XPATH) ? (FieldMapping) _xmlfield :
(FieldMapping) action.data;
(action.op == Action.GET_KEY) ? field : (FieldMapping) action.data;
// mark if the next traversal should go through
// the key rather than value
key = action.op == Action.GET_KEY;
forceOuter |= action.op == Action.GET_OUTER;
if (pstate.field != null) {
// if this is the second-to-last field and the last is
// the related field this field joins to, no need to
@ -472,11 +485,6 @@ public class PCPath
rel = traverseField(pstate, key, forceOuter, false);
}
// mark if the next traversal should go through
// the key rather than value
key = action.op == Action.GET_KEY;
forceOuter |= action.op == Action.GET_OUTER;
// get mapping for the current field
pstate.field = field;
owner = pstate.field.getDefiningMapping();
@ -515,6 +523,11 @@ public class PCPath
if (_varName != null)
pstate.joins = pstate.joins.setVariable(_varName);
// if last action is key action, avoid redundant joins
if (key) {
return pstate;
}
// if we're not comparing to null or doing an isEmpty, then
// join into the data on the final field; obviously we can't do these
// joins when comparing to null b/c the whole purpose is to see
@ -740,13 +753,29 @@ public class PCPath
if (pks)
return mapping.getObjectId(ctx.store, res, null, true,
pstate.joins);
if (_key) {
if (pstate.field.getKey().getValueMappedBy() != null) {
Object obj = res.load(mapping, ctx.store, ctx.fetch,
pstate.joins);
StateManagerImpl sm = (StateManagerImpl)
((PersistenceCapable) obj).pcGetStateManager();
obj = sm.fetch(_class.getField(pstate.field.getKey().
getValueMappedBy()).getIndex());
return obj;
}
else if (pstate.field.getKey().isEmbedded())
return loadEmbeddedMapKey(ctx, state, res);
}
return res.load(mapping, ctx.store, ctx.fetch, pstate.joins);
}
Object ret;
if (_key)
ret = pstate.field.loadKeyProjection(ctx.store, ctx.fetch, res,
pstate.joins);
// Map key is a java primitive type
// example: Map<Integer, Employee> emps
ret = res.getObject(pstate.cols[0],
JavaSQLTypes.JDBC_DEFAULT, pstate.joins);
else
ret = pstate.field.loadProjection(ctx.store, ctx.fetch, res,
pstate.joins);
@ -755,6 +784,23 @@ public class PCPath
return ret;
}
private Object loadEmbeddedMapKey(ExpContext ctx, ExpState state,
Result res) throws SQLException {
PathExpState pstate = (PathExpState) state;
// consume keyProjection
PersistenceCapable pc = (PersistenceCapable) res.load(_candidate,
ctx.store, ctx.fetch, pstate.joins);
if (pstate.field.getStrategy() == null ||
!(pstate.field.getStrategy() instanceof
HandlerRelationMapTableFieldStrategy))
throw new RuntimeException("Invalid map field strategy");
HandlerRelationMapTableFieldStrategy strategy =
(HandlerRelationMapTableFieldStrategy) pstate.field.getStrategy();
return strategy
.loadKey((OpenJPAStateManager) pc.pcGetStateManager(),
ctx.store, ctx.fetch, res, pstate.joins);
}
public void calculateValue(Select sel, ExpContext ctx, ExpState state,
Val other, ExpState otherState) {
// we don't create the SQL b/c it forces the Select to cache aliases

View File

@ -40,7 +40,8 @@ class Type
public Type(Val val) {
super(val);
setMetaData(val.getMetaData());
_disc = ((ClassMapping) getMetaData()).getDiscriminator();
if (getMetaData() != null)
_disc = ((ClassMapping) getMetaData()).getDiscriminator();
}
public ExpState initialize(Select sel, ExpContext ctx, int flags) {

View File

@ -415,6 +415,18 @@ public interface ExpressionFactory {
*/
public Value type(Value target);
/**
* Return the map entry of the given value.
*
* @since 2.0.0
*/
public Value mapEntry(Value key, Value val);
/**
* Return the map key of the given value
*/
public Value mapKey(Value key, Value val);
/**
* Return distinct values of the given value. This is typically used
* within aggregates, for example: max(distinct(path))

View File

@ -35,6 +35,7 @@ import org.apache.openjpa.kernel.StoreContext;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.util.ImplHelper;
import org.apache.openjpa.util.UnsupportedException;
import org.apache.openjpa.util.UserException;
/**
@ -647,6 +648,14 @@ public class InMemoryExpressionFactory
return new Type((Val) val);
}
public Value mapEntry(Value key, Value val) {
throw new UnsupportedException("not implemented yet");
}
public Value mapKey(Value key, Value val) {
throw new UnsupportedException("not implemented yet");
}
public Value getObjectId(Value val) {
return new GetObjectId((Val) val);
}

View File

@ -49,6 +49,7 @@ import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.FieldMetaData;
import org.apache.openjpa.meta.JavaTypes;
import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.meta.ValueMetaData;
import org.apache.openjpa.util.InternalException;
@ -958,6 +959,17 @@ public class JPQLExpressionBuilder
case JJTIDENTIFICATIONVARIABLE:
return getIdentifier(node);
case JJTQUALIFIEDPATH:
// TODO: to be implemented.
case JJTQUALIFIEDIDENTIFIER:
// KEY(e), VALUE(e), ENTRY(e)
return getQualifiedIdentifier(onlyChild(node));
case JJTGENERALIDENTIFIER:
// KEY(e), VALUE(e)
return getQualifiedIdentifier(onlyChild(node));
case JJTNOT:
return factory.not(getExpression(onlyChild(node)));
@ -1412,6 +1424,35 @@ public class JPQLExpressionBuilder
new Object[]{ name }, null);
}
private Value getQualifiedIdentifier(JPQLNode node) {
JPQLNode id = onlyChild(node);
Path path = (Path) getValue(id);
FieldMetaData fld = path.last();
if (fld != null) {
// validate the field is of type java.util.Map
if (fld.getDeclaredTypeCode() != JavaTypes.MAP) {
String oper = "VALUE";
if (node.id == JJTENTRY)
oper = "ENTRY";
else if (node.id == JJTKEY)
oper = "KEY";
throw parseException(EX_USER, "bad-qualified-identifier",
new Object[]{ id.text, oper}, null);
}
}
if (node.id == JJTVALUE)
return path;
Value value = getValue(id);
if (node.id == JJTKEY)
return factory.mapKey(path, value);
else
return factory.mapEntry(path, value);
}
private Value getTypeLiteral(JPQLNode node) {
JPQLNode type = onlyChild(node);
final String name = type.text;
@ -1460,7 +1501,7 @@ public class JPQLExpressionBuilder
/**
* Process type_discriminator
* type_discriminator ::=
* TYPE(identification_variable |
* TYPE(general_identification_variable |
* single_valued_object_path_expression |
* input_parameter )
*/
@ -1475,6 +1516,9 @@ public class JPQLExpressionBuilder
case JJTPOSITIONALINPUTPARAMETER:
return factory.type(getParameter(node.text, true, false));
case JJTGENERALIDENTIFIER:
return factory.type(getQualifiedIdentifier(onlyChild(node)));
default:
// TODO: enforce jpa2.0 spec rules.
// A single_valued_object_field is designated by the name of

View File

@ -73,3 +73,5 @@ query-extensions-error: This JPQL query uses non-standard OpenJPA \
jpql-parse-error: "{1}" while parsing JPQL "{0}". See nested stack trace for \
original parse error.
not-type-literal: The specified node ("{0}") is not a valid entity type literal.
bad-qualified-identifier: The identifier "{0}" in "{1}" operator is not \
referring to an association field of type java.util.Map.

View File

@ -0,0 +1,61 @@
/*
* 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.m2mmapex0;
import javax.persistence.*;
@Entity
@Table(name="m2mDivision")
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,71 @@
/*
* 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.m2mmapex0;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="m2mEmp")
public class Employee {
@Id
int empId;
@ManyToMany // Bidirectional
Map<Division, PhoneNumber> phones =
new HashMap<Division, PhoneNumber>();
public Map<Division, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(Division d, PhoneNumber phoneNumber) {
phones.put(d, phoneNumber);
}
public void removePhoneNumber(Division d) {
phones.remove(d);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<Division, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<Division, PhoneNumber>> entries =
(Collection<Map.Entry<Division, PhoneNumber>>) phones.entrySet();
for (Map.Entry<Division, PhoneNumber> entry : entries) {
Division key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

View File

@ -0,0 +1,70 @@
/*
* 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.m2mmapex0;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="m2mPhone")
public class PhoneNumber {
@Id
int number;
@ManyToMany(mappedBy = "phones")
Map<Division, Employee> emps = new HashMap<Division, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<Division, Employee> getEmployees() {
return emps;
}
public void addEmployees(Division d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(Division d) {
emps.remove(d);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<Division, Employee> map = p.getEmployees();
if (map.size() != emps.size())
return false;
Collection<Map.Entry<Division, Employee>> entries =
(Collection<Map.Entry<Division, Employee>>) emps.entrySet();
for (Map.Entry<Division, Employee> entry : entries) {
Division key = entry.getKey();
Employee e = entry.getValue();
Employee e0 = map.get(key);
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,258 @@
/*
* 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.m2mmapex0;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMap extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES, Division.class,
Employee.class, PhoneNumber.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() });
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
Division d = (Division) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
Division d2 = (Division) rs2.get(0);
String query3 = "select VALUE(e) from PhoneNumber p, " +
" in (p.emps) e";
List rs3 = em.createQuery(query3).getResultList();
Employee e = (Employee) rs3.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
String query5 = "select TYPE(KEY(p)) from Employee e, " +
" in (e.phones) p";
List rs5 = em.createQuery(query5).getResultList();
assertEquals(Division.class, rs5.get(0));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
Division d1 = createDivision(em, divId++);
Division d2 = createDivision(em, divId++);
phoneNumber.addEmployees(d1, e);
e.addPhoneNumber(d2, phoneNumber);
em.persist(phoneNumber);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
em.persist(d1);
em.persist(d2);
}
em.persist(e);
return e;
}
public Division createDivision(EntityManager em, int id) {
Division d = new Division();
d.setId(id);
d.setName("d" + id);
return d;
}
public void removeAll() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createNativeQuery("delete from department");
q.executeUpdate();
q = em.createNativeQuery("delete from employee");
q.executeUpdate();
System.out.println("committing removes");
tran.commit();
em.close();
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps) {
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<Division, PhoneNumber> phones0 = e0.getPhoneNumbers();
Map<Division, PhoneNumber> phones = e.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<Division, Employee> es0 = p0.getEmployees();
Map<Division, Employee> es = p.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<Division, PhoneNumber> es0,
Map<Division, PhoneNumber> es) throws Exception {
Collection<Map.Entry<Division, PhoneNumber>> entrySets0 =
es0.entrySet();
for (Map.Entry<Division, PhoneNumber> entry0 : entrySets0) {
Division d0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(d0);
if (!p0.equals(p))
throw new Exception("Assertion failure");
}
}
public void checkEmpMap(Map<Division, Employee> es0,
Map<Division, Employee> es) throws Exception {
Collection<Map.Entry<Division, Employee>> entrySets0 =
es0.entrySet();
for (Map.Entry<Division, Employee> entry0 : entrySets0) {
Division d0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(d0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,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.m2mmapex1;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="MEx1Dept")
public class Department {
@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) {
if (!(o instanceof Department))
return false;
Department other = (Department) o;
if (name.equals(other.name) &&
id == other.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,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.m2mmapex1;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="MEx1Division")
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) {
if (!(o instanceof Division))
return false;
Division other = (Division) o;
if (name.equals(other.name) &&
id == other.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,71 @@
/*
* 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.m2mmapex1;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx1Emp")
public class Employee {
@Id
int empId;
@ManyToMany
Map<Department, PhoneNumber> phones =
new HashMap<Department, PhoneNumber>();
public Map<Department, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(Department d, PhoneNumber phoneNumber) {
phones.put(d, phoneNumber);
}
public void removePhoneNumber(Department d) {
phones.remove(d);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<Department, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<Department, PhoneNumber>> entries =
(Collection<Map.Entry<Department, PhoneNumber>>) phones.entrySet();
for (Map.Entry<Department, PhoneNumber> entry : entries) {
Department key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

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.m2mmapex1;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx1Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
Map<Division, Employee> emps = new HashMap<Division, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<Division, Employee> getEmployees() {
return emps;
}
public void addEmployees(Division d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(Division d) {
emps.remove(d);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<Division, Employee> map = p.getEmployees();
if (map.size() != emps.size())
return false;
Collection<Map.Entry<Division, Employee>> entries =
(Collection<Map.Entry<Division, Employee>>) emps.entrySet();
for (Map.Entry<Division, Employee> entry : entries) {
Division key = entry.getKey();
Employee e = entry.getValue();
Employee e0 = map.get(key);
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,248 @@
/*
* 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.m2mmapex1;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
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 TestMany2ManyMapEx1 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 10;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES,
Department.class,
Division.class,
Employee.class,
PhoneNumber.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), p from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
Division d = (Division) ((Object[]) rs.get(0))[0];
PhoneNumber p = (PhoneNumber) ((Object[]) rs.get(0))[1];
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
Department d2 = (Department) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj(emf);
findObj(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 < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
Division div = createDivision(em, divId++);
Department dept = createDepartment(em, deptId++);
phoneNumber.addEmployees(div, e);
e.addPhoneNumber(dept, phoneNumber);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
em.persist(phoneNumber);
em.persist(dept);
em.persist(div);
}
em.persist(e);
return e;
}
public Division createDivision(EntityManager em, int id) {
Division d = new Division();
d.setId(id);
d.setName("d" + id);
return d;
}
public Department createDepartment(EntityManager em, int id) {
Department d = new Department();
d.setId(id);
d.setName("dept" + id);
return d;
}
public void findObj(EntityManagerFactory emf) throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj(EntityManagerFactory emf) throws Exception {
queryEmployee(emf);
queryPhoneNumber(emf);
}
public void queryPhoneNumber(EntityManagerFactory emf) throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps) {
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee(EntityManagerFactory emf) throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<Department, PhoneNumber> phones0 = e0.getPhoneNumbers();
Map<Department, PhoneNumber> phones = e.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<Division, Employee> es0 = p0.getEmployees();
Map<Division, Employee> es = p.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<Department, PhoneNumber> es0,
Map<Department, PhoneNumber> es) throws Exception {
Collection<Map.Entry<Department, PhoneNumber>> entrySets0 =
es0.entrySet();
for (Map.Entry<Department, PhoneNumber> entry0 : entrySets0) {
Department d0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(d0);
if (!p0.equals(p))
throw new Exception("Assertion failure");
}
}
public void checkEmpMap(Map<Division, Employee> es0,
Map<Division, Employee> es) throws Exception {
Collection<Map.Entry<Division, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<Division, Employee> entry0 : entrySets0) {
Division d0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(d0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,81 @@
/*
* 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.m2mmapex10;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx10Emp")
public class Employee {
@EmbeddedId
EmployeePK empPK;
@ManyToMany // Bidirectional
Map<PhonePK, PhoneNumber> phones = new HashMap<PhonePK, PhoneNumber>();
int salary;
public EmployeePK getEmpPK() {
return empPK;
}
public void setEmpPK(EmployeePK empPK) {
this.empPK = empPK;
}
public Map<PhonePK, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(PhonePK d, PhoneNumber phoneNumber) {
phones.put(d, phoneNumber);
}
public void removePhoneNumber(PhonePK d) {
phones.remove(d);
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<PhonePK, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<PhonePK, PhoneNumber>> entries =
(Collection<Map.Entry<PhonePK, PhoneNumber>>) phones.entrySet();
for (Map.Entry<PhonePK, PhoneNumber> entry : entries) {
PhonePK key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (!p.getPhonePK().equals(p0.getPhonePK()))
return false;
}
return true;
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.m2mmapex10;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Embeddable
public class EmployeePK implements Serializable {
String name;
Date bDay;
public EmployeePK() {}
public EmployeePK(String name, Date bDay) {
this.name = name;
this.bDay = bDay;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof EmployeePK))
return false;
EmployeePK pk = (EmployeePK) o;
if (pk.name.equals(name) &&
pk.bDay.equals(bDay))
return true;
return false;
}
public int hashCode() {
int code = 0;
code += name.hashCode();
code += bDay.hashCode();
return code;
}
}

View File

@ -0,0 +1,80 @@
/*
* 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.m2mmapex10;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx10Phone")
public class PhoneNumber {
@EmbeddedId
PhonePK phonePK;
@ManyToMany(mappedBy="phones")
Map<EmployeePK, Employee> emps = new HashMap<EmployeePK, Employee>();
int room;
public PhonePK getPhonePK() {
return phonePK;
}
public void setPhonePK(PhonePK phonePK) {
this.phonePK = phonePK;
}
public Map<EmployeePK, Employee> getEmployees() {
return emps;
}
public void addEmployees(EmployeePK d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(EmployeePK d) {
emps.remove(d);
}
public int getRoom() {
return room;
}
public void setRoom(int room) {
this.room = room;
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<EmployeePK, Employee> map = p.getEmployees();
if (map.size() != emps.size())
return false;
Collection<Map.Entry<EmployeePK, Employee>> entries =
(Collection<Map.Entry<EmployeePK, Employee>>) emps.entrySet();
for (Map.Entry<EmployeePK, Employee> entry : entries) {
EmployeePK key = entry.getKey();
Employee e0 = map.get(key);
Employee e = emps.get(key);
if (!e.getEmpPK().equals(e0.getEmpPK()))
return false;
}
return true;
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.m2mmapex10;
import java.io.Serializable;
import javax.persistence.*;
@Embeddable
public class PhonePK implements Serializable {
String areaCode;
String phoneNum;
public PhonePK() {}
public PhonePK(String areaCode, String phoneNum) {
this.areaCode = areaCode;
this.phoneNum = phoneNum;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof PhonePK))
return false;
PhonePK pk = (PhonePK) o;
if (pk.areaCode.equals(areaCode) &&
pk.phoneNum.equals(phoneNum))
return true;
return false;
}
public int hashCode() {
int code = 0;
code = code * 31 + areaCode.hashCode();
code = code * 31 + phoneNum.hashCode();
return code;
}
}

View File

@ -0,0 +1,238 @@
/*
* 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.m2mmapex10;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx10 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public List<String> namedQueries = new ArrayList<String>();
public List<EmployeePK> empPKs = new ArrayList<EmployeePK>();
public List<PhonePK> phonePKs = new ArrayList<PhonePK>();
public Map<EmployeePK, Employee> empMap =
new HashMap<EmployeePK, Employee>();
public Map<PhonePK, PhoneNumber> phoneMap =
new HashMap<PhonePK, PhoneNumber>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(DROP_TABLES,EmployeePK.class, PhonePK.class,
Employee.class, PhoneNumber.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] {
this.new Listener()
});
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e where e.empPK = ?1";
List rs = em.createQuery(query).setParameter(1, empPKs.get(0)).
getResultList();
EmployeePK d = (EmployeePK) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
PhonePK k = (PhonePK) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e where e.empPK = ?1";
List rs4 = em.createQuery(query4).setParameter(1, empPKs.get(0)).
getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpPK(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
EmployeePK empPK = new EmployeePK("e" + id, new Date());
empPKs.add(empPK);
e.setEmpPK(empPK);
e.setSalary(1000);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
PhoneNumber phoneNumber = new PhoneNumber();
PhonePK phonePK = new PhonePK("areaCode" + phoneId, "phoneNum" +
phoneId);
phoneNumber.setRoom(phoneId);
phoneId++;
phonePKs.add(phonePK);
phoneNumber.setPhonePK(phonePK);
phoneNumber.addEmployees(empPK, e);
e.addPhoneNumber(phonePK, phoneNumber);
em.persist(phoneNumber);
phoneMap.put(phoneNumber.getPhonePK(), phoneNumber);
}
em.persist(e);
return e;
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, empPKs.get(1));
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, phonePKs.get(1));
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps) {
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
EmployeePK empPK = e.getEmpPK();
Employee e0 = empMap.get(empPK);
Map<PhonePK, PhoneNumber> phones = e.getPhoneNumbers();
Map<PhonePK, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
PhonePK phonePK = p.getPhonePK();
PhoneNumber p0 = phoneMap.get(phonePK);
Map<EmployeePK, Employee> es = p.getEmployees();
Map<EmployeePK, Employee> es0 = p0.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<PhonePK, PhoneNumber> es0,
Map<PhonePK, PhoneNumber> es) throws Exception {
Collection<Map.Entry<PhonePK, PhoneNumber>> entrySets0 = es0.entrySet();
for (Map.Entry<PhonePK, PhoneNumber> entry0 : entrySets0) {
PhonePK d0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(d0);
if (!p0.equals(p))
throw new Exception("Assertion failure");
}
}
public void checkEmpMap(Map<EmployeePK, Employee> es0,
Map<EmployeePK, Employee> es)
throws Exception {
Collection<Map.Entry<EmployeePK, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<EmployeePK, Employee> entry0 : entrySets0) {
EmployeePK key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,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.m2mmapex2;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="MEx2Dept")
public class Department {
@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) {
if (!(o instanceof Department))
return false;
Department other = (Department) o;
if (name.equals(other.name) &&
id == other.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,71 @@
/*
* 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.m2mmapex2;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx2Emp")
public class Employee {
@Id
int empId;
@ManyToMany
Map<Department, PhoneNumber> phones =
new HashMap<Department, PhoneNumber>(); // Bidirectional
public Map<Department, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(Department d, PhoneNumber phoneNumber) {
phones.put(d, phoneNumber);
}
public void removePhoneNumber(Department d) {
phones.remove(d);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<Department, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<Department, PhoneNumber>> entries =
(Collection<Map.Entry<Department, PhoneNumber>>) phones.entrySet();
for (Map.Entry<Department, PhoneNumber> entry : entries) {
Department key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

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.m2mmapex2;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx2Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
Map<String, Employee> emps = new HashMap<String, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<String, Employee> getEmployees() {
return emps;
}
public void addEmployees(String str, Employee employee) {
emps.put(str, employee);
}
public void removeEmployee(String str) {
emps.remove(str);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<String, Employee> map = p.getEmployees();
if (map.size() != emps.size())
return false;
Collection<Map.Entry<String, Employee>> entries =
(Collection<Map.Entry<String, Employee>>) emps.entrySet();
for (Map.Entry<String, Employee> entry : entries) {
String key = entry.getKey();
Employee e0 = map.get(key);
Employee e = emps.get(key);
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,236 @@
/*
* 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.m2mmapex2;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx2 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES,
Department.class,
Employee.class,
PhoneNumber.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() }
);
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
String d = (String) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
Department d2 = (Department) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
Department d = createDepartment(em, deptId++);
phoneNumber.addEmployees("String" + e.getEmpId(), e);
e.addPhoneNumber(d, phoneNumber);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
em.persist(phoneNumber);
em.persist(d);
}
em.persist(e);
return e;
}
public Department createDepartment(EntityManager em, int id) {
Department d = new Department();
d.setId(id);
d.setName("d" + id);
return d;
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps) {
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<Department, PhoneNumber> phones = e.getPhoneNumbers();
Map<Department, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<String, Employee> es = p.getEmployees();
Map<String, Employee> es0 = p0.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<Department, PhoneNumber> es0,
Map<Department, PhoneNumber> es) throws Exception {
Collection<Map.Entry<Department, PhoneNumber>> entrySets0 =
es0.entrySet();
for (Map.Entry<Department, PhoneNumber> entry0 : entrySets0) {
Department d0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(d0);
if (!p0.equals(p))
throw new Exception("Assertion failure");
}
}
public void checkEmpMap(Map<String, Employee> es0, Map<String, Employee> es)
throws Exception {
Collection<Map.Entry<String, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<String, Employee> entry0 : entrySets0) {
String key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,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.m2mmapex3;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="MEx3Dept")
public class Department {
@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) {
if (!(o instanceof Department))
return false;
Department other = (Department) o;
if (name.equals(other.name) &&
id == other.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,71 @@
/*
* 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.m2mmapex3;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx3Emp")
public class Employee {
@Id
int empId;
@ManyToMany
Map<Department, PhoneNumber> phones =
new HashMap<Department, PhoneNumber>(); // Bidirectional
public Map<Department, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(Department d, PhoneNumber phoneNumber) {
phones.put(d, phoneNumber);
}
public void removePhoneNumber(Department d) {
phones.remove(d);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<Department, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<Department, PhoneNumber>> entries =
(Collection<Map.Entry<Department, PhoneNumber>>) phones.entrySet();
for (Map.Entry<Department, PhoneNumber> entry : entries) {
Department key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

View File

@ -0,0 +1,67 @@
/*
* 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.m2mmapex3;
import javax.persistence.Embeddable;
@Embeddable
public class FullName {
String fName;
String lName;
public FullName() {}
public FullName(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 FullName)) return false;
FullName other = (FullName) o;
if (fName.equals(other.fName) &&
lName.equals(other.lName))
return true;
return false;
}
public int hashCode() {
int ret = 0;
ret += lName.hashCode();
ret = 31 * ret + fName.hashCode();
return ret;
}
}

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.m2mmapex3;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx3Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
Map<FullName, Employee> emps = new HashMap<FullName, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<FullName, Employee> getEmployees() {
return emps;
}
public void addEmployees(FullName d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(FullName d) {
emps.remove(d);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<FullName, Employee> map = p.getEmployees();
if (map.size() != emps.size())
return false;
Collection<Map.Entry<FullName, Employee>> entries =
(Collection<Map.Entry<FullName, Employee>>) emps.entrySet();
for (Map.Entry<FullName, Employee> entry : entries) {
FullName key = entry.getKey();
Employee e = entry.getValue();
Employee e0 = map.get(key);
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,237 @@
/*
* 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.m2mmapex3;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx3 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public List<String> namedQueries = new ArrayList<String>();
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 1;
public void setUp() {
super.setUp(CLEAR_TABLES,
Department.class,
Employee.class,
PhoneNumber.class,
FullName.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() }
);
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
FullName d = (FullName) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
Department d2 = (Department) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
FullName name = new FullName("f" + id, "l" + id);
Department d = createDepartment(em, deptId++);
phoneNumber.addEmployees(name, e);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
e.addPhoneNumber(d, phoneNumber);
em.persist(phoneNumber);
em.persist(d);
}
em.persist(e);
return e;
}
public Department createDepartment(EntityManager em, int id) {
Department d = new Department();
d.setId(id);
d.setName("d" + id);
return d;
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps) {
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<Department, PhoneNumber> phones = e.getPhoneNumbers();
Map<Department, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<FullName, Employee> es = p.getEmployees();
Map<FullName, Employee> es0 = p0.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<Department, PhoneNumber> es0,
Map<Department, PhoneNumber> es) throws Exception {
Collection<Map.Entry<Department, PhoneNumber>> entrySets0 =
es0.entrySet();
for (Map.Entry<Department, PhoneNumber> entry0 : entrySets0) {
Department d0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(d0);
if (!p0.equals(p))
throw new Exception("Assertion failure");
}
}
public void checkEmpMap(Map<FullName, Employee> es0,
Map<FullName, Employee> es) throws Exception {
Collection<Map.Entry<FullName, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<FullName, Employee> entry0 : entrySets0) {
FullName key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,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.m2mmapex4;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="MEX4Division")
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) {
if (!(o instanceof Division)) return false;
Division other = (Division) o;
if (name.equals(other.name) &&
id == other.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,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.m2mmapex4;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx4Emp")
public class Employee {
@Id
int empId;
@ManyToMany // Bidirectional
Map<String, PhoneNumber> phones = new HashMap<String, PhoneNumber>();
public Map<String, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(String str, PhoneNumber phoneNumber) {
phones.put(str, phoneNumber);
}
public void removePhoneNumber(String str) {
phones.remove(str);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<String, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<String, PhoneNumber>> entries =
(Collection<Map.Entry<String, PhoneNumber>>) phones.entrySet();
for (Map.Entry<String, PhoneNumber> entry : entries) {
String key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

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.m2mmapex4;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx4Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
Map<Division, Employee> emps = new HashMap<Division, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<Division, Employee> getEmployees() {
return emps;
}
public void addEmployees(Division d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(Division d) {
emps.remove(d);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<Division, Employee> map = p.getEmployees();
if (p.getEmployees().size() != emps.size())
return false;
Collection<Map.Entry<Division, Employee>> entries =
(Collection<Map.Entry<Division, Employee>>) emps.entrySet();
for (Map.Entry<Division, Employee> entry : entries) {
Division key = entry.getKey();
Employee e = map.get(key);
Employee e0 = entry.getValue();
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,237 @@
/*
* 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.m2mmapex4;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx4 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, PhoneNumber> phones =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 1;
public void setUp() {
super.setUp(CLEAR_TABLES,
Division.class,
Employee.class,
PhoneNumber.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() }
);
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
Division d = (Division) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
String k = (String) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
Division div = createDivision(em, divId++);
phoneNumber.addEmployees(div, e);
e.addPhoneNumber("String" + phoneNumber.getNumber(), phoneNumber);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
em.persist(phoneNumber);
em.persist(div);
}
em.persist(e);
return e;
}
public Division createDivision(EntityManager em, int id) {
Division d = new Division();
d.setId(id);
d.setName("d" + id);
return d;
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps) {
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<String, PhoneNumber> phones = e.getPhoneNumbers();
Map<String, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<Division, Employee> es = p.getEmployees();
Map<Division, Employee> es0 = p0.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<String, PhoneNumber> es0,
Map<String, PhoneNumber> es) throws Exception {
Collection<Map.Entry<String, PhoneNumber>> entrySets0 = es0.entrySet();
for (Map.Entry<String, PhoneNumber> entry0 : entrySets0) {
String key0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(key0);
if (!p0.equals(p))
throw new Exception("Assertion Failure");
}
}
public void checkEmpMap(Map<Division, Employee> es0,
Map<Division, Employee> es) throws Exception {
Collection<Map.Entry<Division, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<Division, Employee> entry0 : entrySets0) {
Division key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,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.m2mmapex5;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx5Emp")
public class Employee {
@Id
int empId;
@ManyToMany // bidirectional
Map<String, PhoneNumber> phones = new HashMap<String, PhoneNumber>();
public Map<String, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(String str, PhoneNumber phoneNumber) {
phones.put(str, phoneNumber);
}
public void removePhoneNumber(String str) {
phones.remove(str);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<String, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<String, PhoneNumber>> entries =
(Collection<Map.Entry<String, PhoneNumber>>) phones.entrySet();
for (Map.Entry<String, PhoneNumber> entry : entries) {
String key = entry.getKey();
PhoneNumber p = map.get(key);
if (p.getNumber() != phones.get(key).getNumber())
return false;
}
return true;
}
}

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.m2mmapex5;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx5Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
Map<String, Employee> emps = new HashMap<String, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<String, Employee> getEmployees() {
return emps;
}
public void addEmployees(String str, Employee employee) {
emps.put(str, employee);
}
public void removeEmployee(String str) {
emps.remove(str);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<String, Employee> map = p.getEmployees();
if (map.size() != emps.size())
return false;
Collection<Map.Entry<String, Employee>> entries =
(Collection<Map.Entry<String, Employee>>) emps.entrySet();
for (Map.Entry<String, Employee> entry : entries) {
String key = entry.getKey();
Employee e = entry.getValue();
Employee e0 = map.get(key);
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,226 @@
/*
* 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.m2mmapex5;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx5 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, PhoneNumber> phones =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES,
Employee.class,
PhoneNumber.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() }
);
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
String d = (String) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
String k = (String) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
phoneNumber.addEmployees("String" + e.getEmpId(), e);
e.addPhoneNumber("String" + phoneNumber.getNumber(), phoneNumber);
em.persist(phoneNumber);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
}
em.persist(e);
return e;
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps) {
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<String, PhoneNumber> phones = e.getPhoneNumbers();
Map<String, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<String, Employee> es = p.getEmployees();
Map<String, Employee> es0 = p0.getEmployees();
Assert.assertEquals(1, es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<String, PhoneNumber> es0,
Map<String, PhoneNumber> es) throws Exception {
Collection<Map.Entry<String, PhoneNumber>> entrySets0 = es0.entrySet();
for (Map.Entry<String, PhoneNumber> entry0 : entrySets0) {
String key0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(key0);
if (!p0.equals(p))
throw new Exception("Assertion Failure");
}
}
public void checkEmpMap(Map<String, Employee> es0, Map<String, Employee> es)
throws Exception {
Collection<Map.Entry<String, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<String, Employee> entry0 : entrySets0) {
String key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,70 @@
/*
* 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.m2mmapex6;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx6Emp")
public class Employee {
@Id
int empId;
@ManyToMany // Bidirectional
Map<String, PhoneNumber> phones = new HashMap<String, PhoneNumber>();
public Map<String, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(String str, PhoneNumber phoneNumber) {
phones.put(str, phoneNumber);
}
public void removePhoneNumber(String str) {
phones.remove(str);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<String, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<String, PhoneNumber>> entries =
(Collection<Map.Entry<String, PhoneNumber>>) phones.entrySet();
for (Map.Entry<String, PhoneNumber> entry : entries) {
String key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

View File

@ -0,0 +1,68 @@
/*
* 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.m2mmapex6;
import javax.persistence.Embeddable;
@Embeddable
public class FullName {
String fName;
String lName;
public FullName() {}
public FullName(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 FullName))
return false;
FullName other = (FullName) o;
if (fName.equals(other.fName) &&
lName.equals(other.lName))
return true;
return false;
}
public int hashCode() {
int ret = 0;
ret += lName.hashCode();
ret = 31 * ret + fName.hashCode();
return ret;
}
}

View File

@ -0,0 +1,70 @@
/*
* 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.m2mmapex6;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx6Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
Map<FullName, Employee> emps = new HashMap<FullName, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<FullName, Employee> getEmployees() {
return emps;
}
public void addEmployees(FullName d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(FullName d) {
emps.remove(d);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<FullName, Employee> map = p.getEmployees();
if (map.size() != emps.size())
return false;
Collection<Map.Entry<FullName, Employee>> entries =
(Collection<Map.Entry<FullName, Employee>>) emps.entrySet();
for (Map.Entry<FullName, Employee> entry : entries) {
FullName key = entry.getKey();
Employee e = entry.getValue();
Employee e0 = map.get(key);
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,229 @@
/*
* 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.m2mmapex6;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx6 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, PhoneNumber> phones =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES,
Employee.class,
PhoneNumber.class,
FullName.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() }
);
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
FullName d = (FullName) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
String k = (String) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
FullName name = new FullName("f" + id, "l" + id);
phoneNumber.addEmployees(name, e);
e.addPhoneNumber("String" + phoneNumber.getNumber(), phoneNumber);
em.persist(phoneNumber);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
}
em.persist(e);
return e;
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps){
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es){
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<String, PhoneNumber> phones = e.getPhoneNumbers();
Map<String, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<FullName, Employee> es = p.getEmployees();
Map<FullName, Employee> es0 = p0.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<String, PhoneNumber> es0,
Map<String, PhoneNumber> es) throws Exception {
Collection<Map.Entry<String, PhoneNumber>> entrySets0 = es0.entrySet();
for (Map.Entry<String, PhoneNumber> entry0 : entrySets0) {
String key0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(key0);
if (!p0.equals(p))
throw new Exception("Assertion Failure");
}
}
public void checkEmpMap(Map<FullName, Employee> es0,
Map<FullName, Employee> es) throws Exception {
Collection<Map.Entry<FullName, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<FullName, Employee> entry0 : entrySets0) {
FullName key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,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.m2mmapex7;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="MEx7Division")
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) {
if (!(o instanceof Division))
return false;
Division other = (Division) o;
if (name.equals(other.name) &&
id == other.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,70 @@
/*
* 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.m2mmapex7;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx7Emp")
public class Employee {
@Id
int empId;
@ManyToMany // Bidirectional
Map<FullName, PhoneNumber> phones = new HashMap<FullName, PhoneNumber>();
public Map<FullName, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(FullName name, PhoneNumber phoneNumber) {
phones.put(name, phoneNumber);
}
public void removePhoneNumber(FullName name) {
phones.remove(name);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<FullName, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<FullName, PhoneNumber>> entries =
(Collection<Map.Entry<FullName, PhoneNumber>>) phones.entrySet();
for (Map.Entry<FullName, PhoneNumber> entry : entries) {
FullName key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

View File

@ -0,0 +1,68 @@
/*
* 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.m2mmapex7;
import javax.persistence.Embeddable;
@Embeddable
public class FullName {
String fName;
String lName;
public FullName() {}
public FullName(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 FullName))
return false;
FullName other = (FullName) o;
if (fName.equals(other.fName) &&
lName.equals(other.lName))
return true;
return false;
}
public int hashCode() {
int ret = 0;
ret += lName.hashCode();
ret = 31 * ret + fName.hashCode();
return ret;
}
}

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.m2mmapex7;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx7Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
Map<Division, Employee> emps = new HashMap<Division, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<Division, Employee> getEmployees() {
return emps;
}
public void addEmployees(Division d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(Division d) {
emps.remove(d);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<Division, Employee> map = p.getEmployees();
if (p.getEmployees().size() != emps.size())
return false;
Collection<Map.Entry<Division, Employee>> entries =
(Collection<Map.Entry<Division, Employee>>) emps.entrySet();
for (Map.Entry<Division, Employee> entry : entries) {
Division key = entry.getKey();
Employee e = map.get(key);
Employee e0 = entry.getValue();
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,240 @@
/*
* 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.m2mmapex7;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx7 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, PhoneNumber> phones =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 1;
public void setUp() {
super.setUp(CLEAR_TABLES,
Division.class,
Employee.class,
FullName.class,
PhoneNumber.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() }
);
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
Division d = (Division) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
FullName f = (FullName) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
FullName name = new FullName("f" + id + i, "l" + id + i);
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
Division div = createDivision(em, divId++);
phoneNumber.addEmployees(div, e);
e.addPhoneNumber(name, phoneNumber);
em.persist(phoneNumber);
em.persist(div);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
}
em.persist(e);
return e;
}
public Division createDivision(EntityManager em, int id) {
Division d = new Division();
d.setId(id);
d.setName("d" + id);
return d;
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps){
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es){
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<FullName, PhoneNumber> phones = e.getPhoneNumbers();
Map<FullName, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<Division, Employee> es = p.getEmployees();
Map<Division, Employee> es0 = p0.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<FullName, PhoneNumber> es0,
Map<FullName, PhoneNumber> es) throws Exception {
Collection<Map.Entry<FullName, PhoneNumber>> entrySets0 =
es0.entrySet();
for (Map.Entry<FullName, PhoneNumber> entry0 : entrySets0) {
FullName key0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(key0);
if (!p0.equals(p))
throw new Exception("Assertion Failure");
}
}
public void checkEmpMap(Map<Division, Employee> es0,
Map<Division, Employee> es) throws Exception {
Collection<Map.Entry<Division, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<Division, Employee> entry0 : entrySets0) {
Division key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,70 @@
/*
* 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.m2mmapex8;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx8Emp")
public class Employee {
@Id
int empId;
@ManyToMany // Bidirectional
Map<FullName, PhoneNumber> phones = new HashMap<FullName, PhoneNumber>();
public Map<FullName, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(FullName name, PhoneNumber phoneNumber) {
phones.put(name, phoneNumber);
}
public void removePhoneNumber(FullName name) {
phones.remove(name);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<FullName, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<FullName, PhoneNumber>> entries =
(Collection<Map.Entry<FullName, PhoneNumber>>) phones.entrySet();
for (Map.Entry<FullName, PhoneNumber> entry : entries) {
FullName key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

View File

@ -0,0 +1,68 @@
/*
* 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.m2mmapex8;
import javax.persistence.Embeddable;
@Embeddable
public class FullName {
String fName;
String lName;
public FullName() {}
public FullName(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 FullName))
return false;
FullName other = (FullName) o;
if (fName.equals(other.fName) &&
lName.equals(other.lName))
return true;
return false;
}
public int hashCode() {
int ret = 0;
ret += lName.hashCode();
ret = 31 * ret + fName.hashCode();
return ret;
}
}

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.m2mmapex8;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx8Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
Map<String, Employee> emps = new HashMap<String, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<String, Employee> getEmployees() {
return emps;
}
public void addEmployees(String d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(String d) {
emps.remove(d);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<String, Employee> map = p.getEmployees();
if (p.getEmployees().size() != emps.size())
return false;
Collection<Map.Entry<String, Employee>> entries =
(Collection<Map.Entry<String, Employee>>) emps.entrySet();
for (Map.Entry<String, Employee> entry : entries) {
String key = entry.getKey();
Employee e = map.get(key);
Employee e0 = entry.getValue();
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

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.m2mmapex8;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx8 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, PhoneNumber> phones =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES,
Employee.class,
FullName.class,
PhoneNumber.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() }
);
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
String d = (String) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
FullName d2 = (FullName) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
FullName name = new FullName("f" + id + i, "l" + id + i);
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
phoneNumber.addEmployees("String" + e.getEmpId() + i, e);
e.addPhoneNumber(name, phoneNumber);
em.persist(phoneNumber);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
}
em.persist(e);
return e;
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps){
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es){
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<FullName, PhoneNumber> phones = e.getPhoneNumbers();
Map<FullName, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<String, Employee> es = p.getEmployees();
Map<String, Employee> es0 = p0.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<FullName, PhoneNumber> es0,
Map<FullName, PhoneNumber> es) throws Exception {
Collection<Map.Entry<FullName, PhoneNumber>> entrySets0 =
es0.entrySet();
for (Map.Entry<FullName, PhoneNumber> entry0 : entrySets0) {
FullName key0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(key0);
if (!p0.equals(p))
throw new Exception("Assertion Failure");
}
}
public void checkEmpMap(Map<String, Employee> es0, Map<String, Employee> es)
throws Exception {
Collection<Map.Entry<String, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<String, Employee> entry0 : entrySets0) {
String key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,77 @@
/*
* 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.m2mmapex9;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx9Emp")
public class Employee {
@Id
int empId;
@ManyToMany
//@AttributeOverrides({
// @AttributeOverride(name="fName", column=@Column(name="fName_Emp")),
// @AttributeOverride(name="lName", column=@Column(name="lName_Emp"))
//})
Map<FullPhoneName, PhoneNumber> phones =
new HashMap<FullPhoneName, PhoneNumber>(); // Bidirectional
public Map<FullPhoneName, PhoneNumber> getPhoneNumbers() {
return phones;
}
public void addPhoneNumber(FullPhoneName name, PhoneNumber phoneNumber) {
phones.put(name, phoneNumber);
}
public void removePhoneNumber(FullPhoneName name) {
phones.remove(name);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public boolean equals(Object o) {
Employee e = (Employee) o;
Map<FullPhoneName, PhoneNumber> map = e.getPhoneNumbers();
if (map.size() != phones.size())
return false;
Collection<Map.Entry<FullPhoneName, PhoneNumber>> entries =
(Collection<Map.Entry<FullPhoneName, PhoneNumber>>)
phones.entrySet();
for (Map.Entry<FullPhoneName, PhoneNumber> entry : entries) {
FullPhoneName key = entry.getKey();
PhoneNumber p = entry.getValue();
PhoneNumber p0 = map.get(key);
if (p.getNumber() != p0.getNumber())
return false;
}
return true;
}
}

View File

@ -0,0 +1,68 @@
/*
* 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.m2mmapex9;
import javax.persistence.Embeddable;
@Embeddable
public class FullName {
String fName1;
String lName1;
public FullName() {}
public FullName(String fName, String lName) {
this.fName1 = fName;
this.lName1 = lName;
}
public String getFName() {
return fName1;
}
public void setFName(String fName) {
this.fName1 = fName;
}
public String getLName() {
return lName1;
}
public void setLName(String lName) {
this.lName1 = lName;
}
public boolean equals(Object o) {
if (!(o instanceof FullName))
return false;
FullName other = (FullName) o;
if (fName1.equals(other.fName1) &&
lName1.equals(other.lName1))
return true;
return false;
}
public int hashCode() {
int ret = 0;
ret += lName1.hashCode();
ret = 31 * ret + fName1.hashCode();
return ret;
}
}

View File

@ -0,0 +1,67 @@
/*
* 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.m2mmapex9;
import javax.persistence.Embeddable;
@Embeddable
public class FullPhoneName {
String fName;
String lName;
public FullPhoneName() {}
public FullPhoneName(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 FullPhoneName)) return false;
FullPhoneName other = (FullPhoneName) o;
if (fName.equals(other.fName) &&
lName.equals(other.lName))
return true;
return false;
}
public int hashCode() {
int ret = 0;
ret += lName.hashCode();
ret = 31 * ret + fName.hashCode();
return ret;
}
}

View File

@ -0,0 +1,73 @@
/*
* 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.m2mmapex9;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="MEx9Phone")
public class PhoneNumber {
@Id int number;
@ManyToMany(mappedBy="phones")
//@AttributeOverrides({
// @AttributeOverride(name="fName", column=@Column(name="fName_phone")),
// @AttributeOverride(name="lName", column=@Column(name="lName_phone"))
//})
Map<FullName, Employee> emps = new HashMap<FullName, Employee>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Map<FullName, Employee> getEmployees() {
return emps;
}
public void addEmployees(FullName d, Employee employee) {
emps.put(d, employee);
}
public void removeEmployee(FullName d) {
emps.remove(d);
}
public boolean equals(Object o) {
PhoneNumber p = (PhoneNumber) o;
Map<FullName, Employee> map = p.getEmployees();
if (p.getEmployees().size() != emps.size())
return false;
Collection<Map.Entry<FullName, Employee>> entries =
(Collection<Map.Entry<FullName, Employee>>) emps.entrySet();
for (Map.Entry<FullName, Employee> entry : entries) {
FullName key = entry.getKey();
Employee e = map.get(key);
Employee e0 = entry.getValue();
if (e.getEmpId() != e0.getEmpId())
return false;
}
return true;
}
}

View File

@ -0,0 +1,247 @@
/*
* 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.m2mmapex9;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
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 TestMany2ManyMapEx9 extends SingleEMFTestCase {
public int numEmployees = 2;
public int numPhoneNumbers = numEmployees + 1;
public int numEmployeesPerPhoneNumber = 2;
public int numPhoneNumbersPerEmployee = 2;
public Map<Integer, PhoneNumber> phones =
new HashMap<Integer, PhoneNumber>();
public List<String> namedQueries = new ArrayList<String>();
public int empId = 1;
public int phoneId = 1;
public int divId = 1;
public int deptId = 1;
public Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public Map<Integer, PhoneNumber> phoneMap =
new HashMap<Integer, PhoneNumber>();
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES,
Employee.class,
FullName.class,
PhoneNumber.class,
FullPhoneName.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] { this.new Listener() }
);
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs = em.createQuery(query).getResultList();
FullName d = (FullName) rs.get(0);
String query2 = "select KEY(p) from Employee e, " +
" in (e.phones) p";
List rs2 = em.createQuery(query2).getResultList();
FullPhoneName k = (FullPhoneName) rs2.get(0);
em.clear();
String query4 = "select ENTRY(e) from PhoneNumber p, " +
" in (p.emps) e order by e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpId(), e);
}
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
for (int i = 0; i < numPhoneNumbersPerEmployee; i++) {
FullPhoneName name1 = new FullPhoneName("f1" + id + i,
"l1" + id + i);
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setNumber(phoneId++);
FullName name2 = new FullName("f2" + phoneNumber.getNumber() + i,
"l2" + phoneNumber.getNumber() + i);
phoneNumber.addEmployees(name2, e);
e.addPhoneNumber(name1, phoneNumber);
em.persist(phoneNumber);
phoneMap.put(phoneNumber.getNumber(), phoneNumber);
}
em.persist(e);
return e;
}
public void removeAll() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createNativeQuery("delete from phonenumber");
q.executeUpdate();
q = em.createNativeQuery("delete from employee");
q.executeUpdate();
System.out.println("committing removes");
tran.commit();
em.close();
}
public void findObj() throws Exception {
EntityManager em = emf.createEntityManager();
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
PhoneNumber p = em.find(PhoneNumber.class, 1);
assertPhoneNumber(p);
em.close();
}
public void queryObj() throws Exception {
queryEmployee();
queryPhoneNumber();
}
public void queryPhoneNumber() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select p from PhoneNumber p");
List<PhoneNumber> ps = q.getResultList();
for (PhoneNumber p : ps) {
assertPhoneNumber(p);
}
tran.commit();
em.close();
}
public void queryEmployee() throws Exception {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) throws Exception {
int id = e.getEmpId();
Employee e0 = empMap.get(id);
Map<FullPhoneName, PhoneNumber> phones = e.getPhoneNumbers();
Map<FullPhoneName, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
checkPhoneMap(phones0, phones);
}
public void assertPhoneNumber(PhoneNumber p) throws Exception {
int number = p.getNumber();
PhoneNumber p0 = phoneMap.get(number);
Map<FullName, Employee> es = p.getEmployees();
Map<FullName, Employee> es0 = p0.getEmployees();
Assert.assertEquals(es0.size(), es.size());
checkEmpMap(es0, es);
}
public void checkPhoneMap(Map<FullPhoneName, PhoneNumber> es0,
Map<FullPhoneName, PhoneNumber> es) throws Exception {
Collection<Map.Entry<FullPhoneName, PhoneNumber>> entrySets0 =
es0.entrySet();
for (Map.Entry<FullPhoneName, PhoneNumber> entry0 : entrySets0) {
FullPhoneName key0 = entry0.getKey();
PhoneNumber p0 = entry0.getValue();
PhoneNumber p = es.get(key0);
if (!p0.equals(p))
throw new Exception("Assertion Failure");
}
}
public void checkEmpMap(Map<FullName, Employee> es0,
Map<FullName, Employee> es) throws Exception {
Collection<Map.Entry<FullName, Employee>> entrySets0 = es0.entrySet();
for (Map.Entry<FullName, Employee> entry0 : entrySets0) {
FullName key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}
}
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,60 @@
/*
* 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_26_ex0;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.*;
@Entity
@Table(name="S26Dept1")
//@Access(AccessType.PROPERTY)
public class Department1 {
//@Id
int deptId;
//@OneToMany(mappedBy="department", fetch=FetchType.EAGER)
//@MapKey(name="empId")
Map<Integer, Employee1> empMap = new HashMap<Integer, Employee1>();
@Id
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
@OneToMany(mappedBy="department", fetch=FetchType.EAGER)
@MapKey(name="empId")
public Map<Integer, Employee1> getEmpMap() {
return empMap;
}
public void setEmpMap(Map<Integer, Employee1> empMap) {
this.empMap = empMap;
}
//public void addEmployee(Employee emp) {
// empMap.put(emp.getEmpId(), emp);
//}
}

View File

@ -0,0 +1,55 @@
/*
* 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_26_ex0;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.*;
@Entity
@Table(name="S26Dept2")
public class Department2 {
int deptId;
Map<EmployeePK2, Employee2> empMap = new HashMap<EmployeePK2, Employee2>();
@Id
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
@OneToMany(mappedBy="department")
@MapKey(name="empPK")
public Map<EmployeePK2, Employee2> getEmpMap() {
return empMap;
}
public void setEmpMap(Map<EmployeePK2, Employee2> empMap) {
this.empMap = empMap;
}
public void addEmployee(Employee2 emp) {
empMap.put(emp.getEmpPK(), emp);
}
}

View File

@ -0,0 +1,58 @@
/*
* 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_26_ex0;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="S26Dept3")
public class Department3 {
@Id
int deptId;
@OneToMany(mappedBy="department", fetch=FetchType.EAGER)
@MapKey(name="name")
Map<EmployeeName3, Employee3> emps =
new HashMap<EmployeeName3, Employee3>();
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public Map<EmployeeName3, Employee3> getEmployees() {
return emps;
}
public void addEmployee(Employee3 emp) {
emps.put(emp.getName(), emp);
}
}

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_26_ex0;
import javax.persistence.*;
@Entity
@Table(name="S26Emp1")
public class Employee1 {
@Id
int empId;
@ManyToOne
@JoinColumn(name="dept_id")
Department1 department;
//@Id
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
//@ManyToOne
//@JoinColumn(name="dept_id")
public Department1 getDepartment() {
return department;
}
public void setDepartment(Department1 department) {
this.department = department;
}
}

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_26_ex0;
import java.util.Date;
import javax.persistence.*;
@Entity
@Table(name="S26Emp2")
public class Employee2 {
EmployeePK2 empPK;
Department2 department;
public Employee2() {}
public Employee2(String name, Date bDate) {
this.empPK = new EmployeePK2(name, bDate);
}
@EmbeddedId
public EmployeePK2 getEmpPK() {
return empPK;
}
public void setEmpPK(EmployeePK2 empPK) {
this.empPK = empPK;
}
@ManyToOne
@JoinColumn(name="dept_id")
public Department2 getDepartment() {
return department;
}
public void setDepartment(Department2 department) {
this.department = department;
}
}

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_26_ex0;
import javax.persistence.*;
@Entity
@Table(name="S26Emp3")
public class Employee3 {
@Id
int empId;
@ManyToOne
@JoinColumn(name="dept_id")
Department3 department;
@Embedded
EmployeeName3 name;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public Department3 getDepartment() {
return department;
}
public void setDepartment(Department3 department) {
this.department = department;
}
public EmployeeName3 getName() {
return name;
}
public void setName(EmployeeName3 name) {
this.name = name;
}
}

View File

@ -0,0 +1,68 @@
/*
* 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_26_ex0;
import javax.persistence.Embeddable;
@Embeddable
public class EmployeeName3 {
String fName;
String lName;
public EmployeeName3() {}
public EmployeeName3(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 EmployeeName3))
return false;
EmployeeName3 other = (EmployeeName3) o;
if (fName.equals(other.fName) &&
lName.equals(other.lName))
return true;
return false;
}
public int hashCode() {
int ret = 0;
ret += lName.hashCode();
ret = 31 * ret + fName.hashCode();
return ret;
}
}

View File

@ -0,0 +1,55 @@
/*
* 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_26_ex0;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Embeddable
public class EmployeePK2 implements Serializable {
String name;
Date bDay;
public EmployeePK2() {}
public EmployeePK2(String name, Date bDay) {
this.name = name;
this.bDay = bDay;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof EmployeePK2))
return false;
EmployeePK2 pk = (EmployeePK2) o;
if (pk.name.equals(name) &&
pk.bDay.equals(bDay))
return true;
return false;
}
public int hashCode() {
int code = 0;
code += name.hashCode();
code += bDay.hashCode();
return code;
}
}

View File

@ -0,0 +1,349 @@
/*
* 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_26_ex0;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
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_26 extends SingleEMFTestCase {
public int numDepartments = 2;
public int numEmployeesPerDept = 2;
public List<String> namedQueries = new ArrayList<String>();
public int deptId = 1;
public int empId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(DROP_TABLES,
Department1.class,
Department2.class,
Department3.class,
Employee1.class,
Employee2.class,
Employee3.class,
EmployeeName3.class,
EmployeePK2.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] {
this.new Listener()
});
createObj();
}
public void testQueryQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from Department1 d, " +
" in (d.empMap) e";
List rs = em.createQuery(query).getResultList();
Integer d = (Integer) rs.get(0);
String query2 = "select KEY(e) from Department2 d, " +
" in (d.empMap) e";
List rs2 = em.createQuery(query2).getResultList();
EmployeePK2 d2 = (EmployeePK2) rs2.get(0);
String query3 = "select KEY(e) from Department3 d, " +
" in (d.emps) e";
List rs3 = em.createQuery(query3).getResultList();
EmployeeName3 d3 = (EmployeeName3) rs3.get(0);
em.close();
}
public void testQueryObject() {
queryObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numDepartments; i++)
createDepartment1(em, deptId++);
for (int i = 0; i < numDepartments; i++)
createDepartment2(em, deptId++);
for (int i = 0; i < numDepartments; i++)
createDepartment3(em, deptId++);
tran.begin();
em.flush();
tran.commit();
em.close();
}
public void createDepartment1(EntityManager em, int id) {
Department1 d = new Department1();
d.setDeptId(id);
Map empMap = new HashMap();
for (int i = 0; i < numEmployeesPerDept; i++) {
Employee1 e = createEmployee1(em, empId++);
//d.addEmployee1(e);
empMap.put(e.getEmpId(), e);
e.setDepartment(d);
em.persist(e);
}
d.setEmpMap(empMap);
em.persist(d);
}
public Employee1 createEmployee1(EntityManager em, int id) {
Employee1 e = new Employee1();
e.setEmpId(id);
return e;
}
public void createDepartment2(EntityManager em, int id) {
Department2 d = new Department2();
d.setDeptId(id);
for (int i = 0; i < numEmployeesPerDept; i++) {
Employee2 e = createEmployee2(em, empId++);
d.addEmployee(e);
e.setDepartment(d);
em.persist(e);
}
em.persist(d);
}
public Employee2 createEmployee2(EntityManager em, int id) {
Employee2 e = new Employee2("e" + id, new Date());
return e;
}
public void createDepartment3(EntityManager em, int id) {
Department3 d = new Department3();
d.setDeptId(id);
for (int i = 0; i < numEmployeesPerDept; i++) {
Employee3 e = createEmployee3(em, empId++);
d.addEmployee(e);
e.setDepartment(d);
em.persist(e);
}
em.persist(d);
}
public Employee3 createEmployee3(EntityManager em, int id) {
Employee3 e = new Employee3();
EmployeeName3 name = new EmployeeName3("f" + id, "l" + id);
e.setEmpId(id);
e.setName(name);
return e;
}
public void findObj() {
EntityManager em = emf.createEntityManager();
Department1 d1 = em.find(Department1.class, 1);
assertDepartment1(d1);
Employee1 e1 = em.find(Employee1.class, 1);
assertEmployee1(e1);
Department2 d2 = em.find(Department2.class, 3);
assertDepartment2(d2);
Map empMap = d2.getEmpMap();
Set<EmployeePK2> keys = empMap.keySet();
for (EmployeePK2 key : keys) {
Employee2 e2 = em.find(Employee2.class, key);
assertEmployee2(e2);
}
Department3 d3 = em.find(Department3.class, 5);
assertDepartment3(d3);
Employee3 e3 = em.find(Employee3.class, 9);
assertEmployee3(e3);
em.close();
}
public void assertDepartment1(Department1 d) {
int id = d.getDeptId();
Map<Integer, Employee1> es = d.getEmpMap();
Assert.assertEquals(2,es.size());
Set keys = es.keySet();
for (Object obj : keys) {
Integer empId = (Integer) obj;
Employee1 e = es.get(empId);
Assert.assertEquals(empId.intValue(), e.getEmpId());
}
}
public void assertDepartment2(Department2 d) {
int id = d.getDeptId();
Map<EmployeePK2, Employee2> es = d.getEmpMap();
Assert.assertEquals(2,es.size());
Set<EmployeePK2> keys = es.keySet();
for (EmployeePK2 pk : keys) {
Employee2 e = es.get(pk);
Assert.assertEquals(pk, e.getEmpPK());
}
}
public void assertDepartment3(Department3 d) {
int id = d.getDeptId();
Map<EmployeeName3, Employee3> es = d.getEmployees();
Assert.assertEquals(2,es.size());
Set<EmployeeName3> keys = es.keySet();
for (EmployeeName3 key : keys) {
Employee3 e = es.get(key);
Assert.assertEquals(key, e.getName());
}
}
public void assertEmployee1(Employee1 e) {
int id = e.getEmpId();
Department1 d = e.getDepartment();
assertDepartment1(d);
}
public void assertEmployee2(Employee2 e) {
EmployeePK2 pk = e.getEmpPK();
Department2 d = e.getDepartment();
assertDepartment2(d);
}
public void assertEmployee3(Employee3 e) {
int id = e.getEmpId();
Department3 d = e.getDepartment();
assertDepartment3(d);
}
public void queryObj() {
queryDepartment1(emf);
queryEmployee1(emf);
queryDepartment2(emf);
queryEmployee2(emf);
queryDepartment3(emf);
queryEmployee3(emf);
}
public void queryDepartment1(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select d from Department1 d");
List<Department1> ds = q.getResultList();
for (Department1 d : ds)
assertDepartment1(d);
tran.commit();
em.close();
}
public void queryEmployee1(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee1 e");
List<Employee1> es = q.getResultList();
for (Employee1 e : es)
assertEmployee1(e);
tran.commit();
em.close();
}
public void queryDepartment2(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select d from Department2 d");
List<Department2> ds = q.getResultList();
for (Department2 d : ds)
assertDepartment2(d);
tran.commit();
em.close();
}
public void queryEmployee2(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee2 e");
List<Employee2> es = q.getResultList();
for (Employee2 e : es)
assertEmployee2(e);
tran.commit();
em.close();
}
public void queryDepartment3(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select d from Department3 d");
List<Department3> ds = q.getResultList();
for (Department3 d : ds)
assertDepartment3(d);
tran.commit();
em.close();
}
public void queryEmployee3(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee3 e");
List<Employee3> es = q.getResultList();
for (Employee3 e : es)
assertEmployee3(e);
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,60 @@
/*
* 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_26_ex1;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.*;
@Entity
@Table(name="S26Ex1Dept")
public class Department {
@Id
int deptId;
@OneToMany(cascade=CascadeType.ALL, mappedBy="dept", fetch=FetchType.EAGER)
@MapKey(name="empId")
Map<Integer, Employee> empMap = new HashMap<Integer, Employee>();
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public Map<Integer, Employee> getEmpMap() {
return empMap;
}
public void setEmpMap(Map<Integer, Employee> empMap) {
this.empMap = empMap;
}
public void addEmployee(Employee e) {
empMap.put(e.getEmpId(), e);
}
public void removeEmployee(Integer empId) {
empMap.remove(empId);
}
}

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_26_ex1;
import javax.persistence.*;
@Entity
@Table(name="S26Ex1Emp")
public class Employee {
@Id
int empId;
@ManyToOne
@JoinColumn(name="dept_id")
Department dept;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public Department getDepartment() {
return dept;
}
public void setDepartment(Department department) {
this.dept = department;
}
}

View File

@ -0,0 +1,229 @@
/*
* 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_26_ex1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
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_26_Ex1 extends SingleEMFTestCase {
public int numDepartments = 2;
public int numEmployeesPerDept = 2;
public List<String> namedQueries = new ArrayList<String>();
public int deptId = 1;
public int empId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES,
Department.class,
Employee.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] {
this.new Listener()
});
createObj();
}
public void testQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from Department d, " +
" in (d.empMap) e order by d.deptId, e.empId";
System.err.println(query);
List rs = em.createQuery(query).getResultList();
Integer d = (Integer) rs.get(0);
em.clear();
String query4 = "select ENTRY(e) from Department d, " +
" in (d.empMap) e order by d.deptId, e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() throws Exception {
queryObj();
findObj();
}
public List<String> getSql() {
return sql;
}
public int getSqlCount() {
return sqlCount;
}
public void createObj() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numDepartments; i++)
createDepartment(em, deptId++);
tran.begin();
em.flush();
tran.commit();
em.close();
}
public void createDepartment(EntityManager em, int id) {
Department d = new Department();
d.setDeptId(id);
Map emps = new HashMap();
for (int i = 0; i < numEmployeesPerDept; i++) {
Employee e = createEmployee(em, empId++);
d.addEmployee(e);
emps.put(e.getEmpId(), e);
e.setDepartment(d);
em.persist(e);
}
em.persist(d);
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
e.setEmpId(id);
return e;
}
public void findObj() {
EntityManager em = emf.createEntityManager();
Department d = em.find(Department.class, 1);
assertDepartment(d);
Employee e = em.find(Employee.class, 1);
assertEmployee(e);
// updateObj by adding a new Employee
updateObj(em, d);
deleteObj(em, d);
em.close();
}
public void updateObj(EntityManager em, Department d) {
EntityTransaction tran = em.getTransaction();
// add an element
tran.begin();
Employee e = createEmployee(em,
numDepartments * numEmployeesPerDept + 1);
d.addEmployee(e);
e.setDepartment(d);
em.persist(d);
em.persist(e);
em.flush();
tran.commit();
// remove an element
tran.begin();
d.removeEmployee(e.getEmpId());
e.setDepartment(null);
em.persist(d);
em.persist(e);
em.flush();
tran.commit();
}
public void deleteObj(EntityManager em, Department d) {
EntityTransaction tran = em.getTransaction();
tran.begin();
em.remove(d);
tran.commit();
}
public void assertDepartment(Department d) {
int id = d.getDeptId();
Map<Integer, Employee> es = d.getEmpMap();
Assert.assertEquals(2, es.size());
Set keys = es.keySet();
for (Object obj : keys) {
Integer empId = (Integer) obj;
Employee e = es.get(empId);
Assert.assertEquals(empId.intValue(), e.getEmpId());
}
}
public void queryObj() {
queryDepartment();
queryEmployee();
}
public void queryDepartment() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select d from Department d");
List<Department> ds = q.getResultList();
for (Department d : ds)
assertDepartment(d);
tran.commit();
em.close();
}
public void queryEmployee() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es)
assertEmployee(e);
tran.commit();
em.close();
}
public void assertEmployee(Employee e) {
int id = e.getEmpId();
Department d = e.getDepartment();
assertDepartment(d);
}
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,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_26_ex2;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.*;
@Entity
@Table(name="S26Ex2Dept")
public class Department {
int deptId;
Map<EmployeePK, Employee> empMap = new HashMap<EmployeePK, Employee>();
@Id
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
@OneToMany(cascade=CascadeType.ALL,mappedBy="department")
@MapKey(name="empPK")
public Map<EmployeePK, Employee> getEmpMap() {
return empMap;
}
public void setEmpMap(Map<EmployeePK, Employee> empMap) {
this.empMap = empMap;
}
public void addEmployee(Employee e) {
empMap.put(e.getEmpPK(), e);
}
public void removeEmployee(EmployeePK pk) {
empMap.remove(pk);
}
}

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_26_ex2;
import java.util.Date;
import javax.persistence.*;
@Entity
@Table(name="S26Ex2Emp")
public class Employee {
EmployeePK empPK;
Department department;
public Employee() {}
public Employee(String name, Date bDate) {
this.empPK = new EmployeePK(name, bDate);
}
@EmbeddedId
public EmployeePK getEmpPK() {
return empPK;
}
public void setEmpPK(EmployeePK empPK) {
this.empPK = empPK;
}
@ManyToOne
@JoinColumn(name="dept_id")
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}

View File

@ -0,0 +1,55 @@
/*
* 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_26_ex2;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Embeddable
public class EmployeePK implements Serializable {
String name;
Date bDay;
public EmployeePK() {}
public EmployeePK(String name, Date bDay) {
this.name = name;
this.bDay = bDay;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof EmployeePK))
return false;
EmployeePK pk = (EmployeePK) o;
if (pk.name.equals(name) &&
pk.bDay.equals(bDay))
return true;
return false;
}
public int hashCode() {
int code = 0;
code += name.hashCode();
code += bDay.hashCode();
return code;
}
}

View File

@ -0,0 +1,226 @@
/*
* 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_26_ex2;
import java.util.ArrayList;
import java.util.Date;
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_26_Ex2 extends SingleEMFTestCase {
public int numDepartments = 2;
public int numEmployeesPerDept = 2;
public List<String> namedQueries = new ArrayList<String>();
public int deptId = 1;
public int empId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(CLEAR_TABLES,
Department.class,
Employee.class,
EmployeePK.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] {
this.new Listener()
});
createObj(emf);
}
public void testQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from Department d, " +
" in (d.empMap) e where d.deptId = 1";
List rs = em.createQuery(query).getResultList();
EmployeePK d = (EmployeePK) rs.get(0);
em.clear();
String query4 = "select ENTRY(e) from Department d, " +
" in (d.empMap) e where d.deptId = 1";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() {
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 < numDepartments; i++)
createDepartment(em, deptId++);
tran.begin();
em.flush();
tran.commit();
em.close();
}
public void createDepartment(EntityManager em, int id) {
Department d = new Department();
d.setDeptId(id);
for (int i = 0; i < numEmployeesPerDept; i++) {
Employee e = createEmployee(em, empId++);
d.addEmployee(e);
e.setDepartment(d);
em.persist(e);
}
em.persist(d);
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee("e" + id, new Date());
return e;
}
public void findObj(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
Department d = em.find(Department.class, 1);
assertDepartment(d);
Map emps = d.getEmpMap();
Set<EmployeePK> keys = emps.keySet();
for (EmployeePK key : keys) {
Employee e = em.find(Employee.class, key);
assertEmployee(e);
}
// updateObj by adding a new Employee
updateObj(em, d);
deleteObj(em, d);
em.close();
}
public void updateObj(EntityManager em, Department d) {
EntityTransaction tran = em.getTransaction();
// add an element
tran.begin();
Employee e = createEmployee(em, numDepartments * numEmployeesPerDept
+ 1);
d.addEmployee(e);
e.setDepartment(d);
em.persist(d);
em.persist(e);
em.flush();
tran.commit();
// remove an element
tran.begin();
d.removeEmployee(e.getEmpPK());
e.setDepartment(null);
em.persist(d);
em.persist(e);
em.flush();
tran.commit();
}
public void deleteObj(EntityManager em, Department d) {
EntityTransaction tran = em.getTransaction();
tran.begin();
em.remove(d);
tran.commit();
}
public void assertDepartment(Department d) {
int id = d.getDeptId();
Map es = d.getEmpMap();
Assert.assertEquals(2, es.size());
Set keys = es.keySet();
for (Object obj : keys) {
EmployeePK empPK = (EmployeePK) obj;
Employee e = (Employee) es.get(empPK);
Assert.assertEquals(empPK, e.getEmpPK());
}
}
public void queryObj(EntityManagerFactory emf) {
queryDepartment(emf);
queryEmployee(emf);
}
public void queryDepartment(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select d from Department d");
List<Department> ds = q.getResultList();
for (Department d : ds) {
assertDepartment(d);
}
tran.commit();
em.close();
}
public void queryEmployee(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es) {
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) {
EmployeePK pk = e.getEmpPK();
Department d = e.getDepartment();
}
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,61 @@
/*
* 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_26_ex3;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="S26Ex3Dept")
public class Department {
@Id
int deptId;
@OneToMany(cascade=CascadeType.ALL)
@MapKey(name="name")
Map<EmployeeName, Employee> emps = new HashMap<EmployeeName, Employee>();
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public Map<EmployeeName, Employee> getEmployees() {
return emps;
}
public void addEmployee(Employee emp) {
emps.put(emp.getName(), emp);
}
public void removeEmployee(EmployeeName name) {
emps.remove(name);
}
}

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_26_ex3;
import javax.persistence.*;
@Entity
@Table(name="S26Ex3Emp")
public class Employee {
@Id
int empId;
@ManyToOne
@JoinColumn(name="dept_id")
Department department;
@Embedded
EmployeeName name;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public EmployeeName getName() {
return name;
}
public void setName(EmployeeName name) {
this.name = name;
}
}

View File

@ -0,0 +1,68 @@
/*
* 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_26_ex3;
import javax.persistence.Embeddable;
@Embeddable
public class EmployeeName {
String fName;
String lName;
public EmployeeName() {}
public EmployeeName(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 EmployeeName))
return false;
EmployeeName other = (EmployeeName) o;
if (fName.equals(other.fName) &&
lName.equals(other.lName))
return true;
return false;
}
public int hashCode() {
int ret = 0;
ret += lName.hashCode();
ret = 31 * ret + fName.hashCode();
return ret;
}
}

View File

@ -0,0 +1,223 @@
/*
* 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_26_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_26_Ex3 extends SingleEMFTestCase {
public int numDepartments = 2;
public int numEmployeesPerDept = 2;
public List<String> namedQueries = new ArrayList<String>();
public int deptId = 1;
public int empId = 1;
protected List<String> sql = new ArrayList<String>();
protected int sqlCount;
public void setUp() {
super.setUp(DROP_TABLES,
Department.class,
Employee.class,
EmployeeName.class,
"openjpa.jdbc.JDBCListeners",
new JDBCListener[] {
this.new Listener()
});
createObj(emf);
}
public void testQualifiedId() throws Exception {
EntityManager em = emf.createEntityManager();
String query = "select KEY(e) from Department d, " +
" in (d.emps) e order by d.deptId, e.empId";
List rs = em.createQuery(query).getResultList();
EmployeeName d = (EmployeeName) rs.get(0);
em.clear();
String query4 = "select ENTRY(e) from Department d, " +
" in (d.emps) e order by d.deptId, e.empId";
List rs4 = em.createQuery(query4).getResultList();
Map.Entry me = (Map.Entry) rs4.get(0);
assertTrue(d.equals(me.getKey()));
em.close();
}
public void testQueryObject() {
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 < numDepartments; i++)
createDepartment(em, deptId++);
tran.begin();
em.flush();
tran.commit();
em.close();
}
public void createDepartment(EntityManager em, int id) {
Department d = new Department();
d.setDeptId(id);
for (int i = 0; i < numEmployeesPerDept; i++) {
Employee e = createEmployee(em, empId++);
d.addEmployee(e);
e.setDepartment(d);
em.persist(e);
}
em.persist(d);
}
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
EmployeeName name = new EmployeeName("f" + id, "l" + id);
e.setEmpId(id);
e.setName(name);
return e;
}
public void findObj(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
Department d = em.find(Department.class, 1);
assertDepartment(d);
//Employee e = em.find(Employee.class, 1);
//assertEmployee(e);
updateObj(em, d);
deleteObj(em, d);
em.close();
}
public void updateObj(EntityManager em, Department d) {
EntityTransaction tran = em.getTransaction();
// add an element
tran.begin();
Employee e = createEmployee(em, numDepartments * numEmployeesPerDept + 1);
d.addEmployee(e);
e.setDepartment(d);
em.persist(d);
em.persist(e);
em.flush();
tran.commit();
// remove an element
tran.begin();
d.removeEmployee(e.getName());
e.setDepartment(null);
em.persist(d);
em.flush();
tran.commit();
}
public void deleteObj(EntityManager em, Department d) {
EntityTransaction tran = em.getTransaction();
tran.begin();
em.remove(d);
tran.commit();
}
public void assertDepartment(Department d) {
int id = d.getDeptId();
Map<EmployeeName, Employee> es = d.getEmployees();
Assert.assertEquals(2,es.size());
Set keys = es.keySet();
for (Object obj : keys) {
EmployeeName empName = (EmployeeName) obj;
Employee e = (Employee)es.get(empName);
Assert.assertEquals(empName, e.getName());
}
}
public void queryObj(EntityManagerFactory emf) {
queryDepartment(emf);
queryEmployee(emf);
}
public void queryDepartment(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select d from Department d");
List<Department> ds = q.getResultList();
for (Department d : ds){
assertDepartment(d);
}
tran.commit();
em.close();
}
public void queryEmployee(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select e from Employee e");
List<Employee> es = q.getResultList();
for (Employee e : es){
assertEmployee(e);
}
tran.commit();
em.close();
}
public void assertEmployee(Employee e) {
int id = e.getEmpId();
Department d = e.getDepartment();
assertDepartment(d);
}
public class Listener extends AbstractJDBCListener {
@Override
public void beforeExecuteStatement(JDBCEvent event) {
if (event.getSQL() != null && sql != null) {
sql.add(event.getSQL());
sqlCount++;
}
}
}
}