|
Lines 27-48
Link Here
|
| 27 |
import javax.persistence.EntityManager; |
27 |
import javax.persistence.EntityManager; |
| 28 |
import javax.persistence.EntityManagerFactory; |
28 |
import javax.persistence.EntityManagerFactory; |
| 29 |
import javax.persistence.Query; |
29 |
import javax.persistence.Query; |
| 30 |
import javax.persistence.Tuple; |
|
|
| 31 |
import javax.persistence.TypedQuery; |
| 32 |
import javax.persistence.criteria.CriteriaQuery; |
30 |
import javax.persistence.criteria.CriteriaQuery; |
| 33 |
import javax.persistence.criteria.Join; |
|
|
| 34 |
import javax.persistence.criteria.ParameterExpression; |
| 35 |
import javax.persistence.criteria.Path; |
31 |
import javax.persistence.criteria.Path; |
| 36 |
import javax.persistence.criteria.QueryBuilder; |
32 |
import javax.persistence.criteria.QueryBuilder; |
| 37 |
import javax.persistence.criteria.Root; |
33 |
import javax.persistence.criteria.Root; |
| 38 |
import javax.persistence.metamodel.Attribute; |
34 |
import javax.persistence.metamodel.Attribute; |
|
|
35 |
import javax.persistence.metamodel.Bindable; |
| 39 |
import javax.persistence.metamodel.CollectionAttribute; |
36 |
import javax.persistence.metamodel.CollectionAttribute; |
|
|
37 |
import javax.persistence.metamodel.EmbeddableType; |
| 38 |
import javax.persistence.metamodel.EntityType; |
| 40 |
import javax.persistence.metamodel.IdentifiableType; |
39 |
import javax.persistence.metamodel.IdentifiableType; |
| 41 |
import javax.persistence.metamodel.ListAttribute; |
40 |
import javax.persistence.metamodel.ListAttribute; |
| 42 |
import javax.persistence.metamodel.MapAttribute; |
41 |
import javax.persistence.metamodel.ManagedType; |
| 43 |
import javax.persistence.metamodel.Metamodel; |
42 |
import javax.persistence.metamodel.Metamodel; |
| 44 |
import javax.persistence.metamodel.PluralAttribute; |
43 |
import javax.persistence.metamodel.PluralAttribute; |
| 45 |
import javax.persistence.metamodel.SetAttribute; |
|
|
| 46 |
import javax.persistence.metamodel.SingularAttribute; |
44 |
import javax.persistence.metamodel.SingularAttribute; |
| 47 |
import javax.persistence.metamodel.Type; |
45 |
import javax.persistence.metamodel.Type; |
| 48 |
import javax.persistence.metamodel.Type.PersistenceType; |
46 |
import javax.persistence.metamodel.Type.PersistenceType; |
|
Lines 50-85
Link Here
|
| 50 |
import junit.framework.Test; |
48 |
import junit.framework.Test; |
| 51 |
import junit.framework.TestSuite; |
49 |
import junit.framework.TestSuite; |
| 52 |
|
50 |
|
| 53 |
import org.eclipse.persistence.config.CacheUsage; |
|
|
| 54 |
import org.eclipse.persistence.config.QueryHints; |
| 55 |
import org.eclipse.persistence.descriptors.RelationalDescriptor; |
51 |
import org.eclipse.persistence.descriptors.RelationalDescriptor; |
| 56 |
import org.eclipse.persistence.expressions.Expression; |
52 |
import org.eclipse.persistence.expressions.Expression; |
| 57 |
import org.eclipse.persistence.internal.expressions.ClassTypeExpression; |
53 |
import org.eclipse.persistence.internal.expressions.ClassTypeExpression; |
|
|
54 |
import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl; |
| 55 |
import org.eclipse.persistence.internal.jpa.metamodel.AttributeImpl; |
| 56 |
import org.eclipse.persistence.internal.jpa.metamodel.EmbeddableTypeImpl; |
| 58 |
import org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl; |
57 |
import org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl; |
|
|
58 |
import org.eclipse.persistence.internal.jpa.metamodel.ManagedTypeImpl; |
| 59 |
import org.eclipse.persistence.internal.jpa.metamodel.MappedSuperclassTypeImpl; |
59 |
import org.eclipse.persistence.internal.jpa.metamodel.MappedSuperclassTypeImpl; |
| 60 |
import org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl; |
60 |
import org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl; |
|
|
61 |
import org.eclipse.persistence.internal.jpa.metamodel.SingularAttributeImpl; |
| 61 |
import org.eclipse.persistence.internal.jpa.metamodel.TypeImpl; |
62 |
import org.eclipse.persistence.internal.jpa.metamodel.TypeImpl; |
| 62 |
import org.eclipse.persistence.internal.jpa.querydef.ExpressionImpl; |
|
|
| 63 |
import org.eclipse.persistence.internal.jpa.querydef.SelectionImpl; |
| 64 |
import org.eclipse.persistence.mappings.DatabaseMapping; |
63 |
import org.eclipse.persistence.mappings.DatabaseMapping; |
| 65 |
import org.eclipse.persistence.testing.framework.QuerySQLTracker; |
64 |
import org.eclipse.persistence.sessions.DatabaseRecord; |
| 66 |
import org.eclipse.persistence.testing.models.jpa.advanced.Address; |
65 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
| 67 |
import org.eclipse.persistence.testing.models.jpa.advanced.Employee; |
|
|
| 68 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Board; |
66 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Board; |
| 69 |
import org.eclipse.persistence.testing.models.jpa.metamodel.CompositePK; |
|
|
| 70 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Computer; |
67 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Computer; |
| 71 |
import org.eclipse.persistence.testing.models.jpa.metamodel.EmbeddedPK; |
68 |
import org.eclipse.persistence.testing.models.jpa.metamodel.EmbeddedPK; |
| 72 |
import org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner; |
69 |
import org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner; |
| 73 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Location; |
70 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Location; |
| 74 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer; |
71 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer; |
| 75 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Memory; |
72 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Memory; |
|
|
73 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Person; |
| 76 |
import org.eclipse.persistence.testing.models.jpa.metamodel.SoftwareDesigner; |
74 |
import org.eclipse.persistence.testing.models.jpa.metamodel.SoftwareDesigner; |
| 77 |
import org.eclipse.persistence.testing.models.jpa.metamodel.User; |
75 |
import org.eclipse.persistence.testing.models.jpa.metamodel.User; |
| 78 |
import org.eclipse.persistence.testing.models.jpa.metamodel.VectorProcessor; |
76 |
import org.eclipse.persistence.testing.models.jpa.metamodel.VectorProcessor; |
| 79 |
|
77 |
|
| 80 |
/** |
78 |
/** |
| 81 |
* Disclaimer: |
79 |
* Disclaimer: |
| 82 |
* Yes I know the following are true for this test suite - but implementation time must be ""triaged"", and this testing code is at the bottom of the list when placed against actual implementation in the time provided. |
80 |
* Yes, the following are true for this test suite - but implementation time must be ""triaged"", and this testing code is at the bottom of the list when placed against actual implementation in the time provided. |
| 83 |
* - Tests must be modular - not one big huge test case that either passes or fails - it is better to have 10's of granular failures instead of only 1 |
81 |
* - Tests must be modular - not one big huge test case that either passes or fails - it is better to have 10's of granular failures instead of only 1 |
| 84 |
* - proper and fully optimized test cases |
82 |
* - proper and fully optimized test cases |
| 85 |
* - full exception handling |
83 |
* - full exception handling |
|
Lines 123-128
Link Here
|
| 123 |
|
121 |
|
| 124 |
public void setUp() { |
122 |
public void setUp() { |
| 125 |
super.setUp(); |
123 |
super.setUp(); |
|
|
124 |
// Drop all tables : Thank you Chris |
| 125 |
/*java.util.Vector v = JUnitTestCase.getServerSession("metamodel1").executeSQL("select tablename from sys.systables where tabletype='T'"); |
| 126 |
for (int i=0; i<v.size(); i++){ |
| 127 |
try{ |
| 128 |
DatabaseRecord dr = (DatabaseRecord)v.get(i); |
| 129 |
JUnitTestCase.getServerSession().executeNonSelectingSQL("Drop table "+dr.getValues().get(0)); |
| 130 |
} catch (Exception e){ |
| 131 |
System.out.println(e); |
| 132 |
} |
| 133 |
}*/ |
| 134 |
/*JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_HIST_EMPLOY"); |
| 135 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_MANUF_MM_HWDES_MAP"); |
| 136 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_MANUF_MM_CORPCOMPUTER"); |
| 137 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_MANUF_MM_COMPUTER"); |
| 138 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_MANUF_MM_HWDESIGNER"); |
| 139 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_BOARD_MM_MEMORY"); |
| 140 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_BOARD_MM_PRO"); |
| 141 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_COMPUTER_MM_USER"); |
| 142 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_BOARD_SEQ"); |
| 143 |
|
| 144 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_COMPUTER"); |
| 145 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_USER"); |
| 146 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_HWDESIGNER"); |
| 147 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_MEMORY"); |
| 148 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_PROC"); |
| 149 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_LOCATION"); |
| 150 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_BOARD"); |
| 151 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_SWDESIGNER"); |
| 152 |
JUnitTestCase.getServerSession(PERSISTENCE_UNIT_NAME).executeNonSelectingSQL("DROP TABLE CMP3_MM_MANUF"); |
| 153 |
*/ |
| 126 |
} |
154 |
} |
| 127 |
|
155 |
|
| 128 |
public static Test suite() { |
156 |
public static Test suite() { |
|
Lines 130-387
Link Here
|
| 130 |
|
158 |
|
| 131 |
//suite.addTest(new MetamodelMetamodelTest("testMetamodelStringBasedQuery")); |
159 |
//suite.addTest(new MetamodelMetamodelTest("testMetamodelStringBasedQuery")); |
| 132 |
//suite.addTest(new MetamodelMetamodelTest("testMetamodelTypeSafeBasedQuery")); |
160 |
//suite.addTest(new MetamodelMetamodelTest("testMetamodelTypeSafeBasedQuery")); |
| 133 |
suite.addTest(new MetamodelMetamodelTest("testImplementation")); |
161 |
suite.addTest(new MetamodelMetamodelTest("testMetamodelFullImplementation")); |
| 134 |
return suite; |
162 |
return suite; |
| 135 |
} |
163 |
} |
| 136 |
|
164 |
|
| 137 |
/** |
165 |
/** |
| 138 |
* Test the Metamodel API using a TypeSafe query via the Criteria API (a user of the Metamodel) |
166 |
* The following large single test case contains signatures of all the spec functions. |
|
|
167 |
* Those that have a test are implemented, the missing ones may still be in development. |
| 139 |
*/ |
168 |
*/ |
| 140 |
public void testMetamodelTypeSafeBasedQuery() { |
169 |
public void testMetamodelFullImplementation() { |
| 141 |
EntityManagerFactory emf = null; |
170 |
EntityManagerFactory emf = null; |
| 142 |
EntityManager em = null; |
171 |
EntityManager em = null; |
| 143 |
|
|
|
| 144 |
Set<Computer> computersList = new HashSet(); |
| 145 |
List<Memory> memories = new ArrayList(); |
| 146 |
List<VectorProcessor> processors = new ArrayList(); |
| 147 |
List<HardwareDesigner> hardwareDesigners = new ArrayList(); |
| 148 |
Computer computer1 = null; |
| 149 |
Computer computer2 = null; |
| 150 |
Manufacturer manufacturer = null; |
| 151 |
User user = null; |
| 152 |
HardwareDesigner hardwareDesigner1 = null; |
| 153 |
SoftwareDesigner softwareDesigner1 = null; |
| 154 |
VectorProcessor vectorProcessor1 = null; |
| 155 |
Board board1 = null; |
| 156 |
Memory memory1 = null; |
| 157 |
Memory memory2 = null; |
| 158 |
Location location1 = null; |
| 159 |
Location location2 = null; |
| 160 |
boolean exceptionThrown = false; |
| 161 |
Metamodel metamodel = null; |
| 162 |
|
| 163 |
try { |
| 164 |
emf = initialize(); |
| 165 |
em = emf.createEntityManager(); |
| 166 |
em.getTransaction().begin(); |
| 167 |
|
| 168 |
// setup entity relationships |
| 169 |
computer1 = new Computer(); |
| 170 |
computer2 = new Computer(); |
| 171 |
memory1 = new Memory(); |
| 172 |
memory2 = new Memory(); |
| 173 |
manufacturer = new Manufacturer(); |
| 174 |
user = new User(); |
| 175 |
hardwareDesigner1 = new HardwareDesigner(); |
| 176 |
softwareDesigner1 = new SoftwareDesigner(); |
| 177 |
vectorProcessor1 = new VectorProcessor(); |
| 178 |
board1 = new Board(); |
| 179 |
location1 = new Location(); |
| 180 |
location2 = new Location(); |
| 181 |
|
| 182 |
// setup collections |
| 183 |
computersList.add(computer1); |
| 184 |
computersList.add(computer2); |
| 185 |
processors.add(vectorProcessor1); |
| 186 |
memories.add(memory1); |
| 187 |
memories.add(memory2); |
| 188 |
hardwareDesigners.add(hardwareDesigner1); |
| 189 |
|
| 190 |
// set owning and inverse sides of 1:m and m:1 relationships |
| 191 |
manufacturer.setComputers(computersList); |
| 192 |
manufacturer.setHardwareDesigners(hardwareDesigners); |
| 193 |
hardwareDesigner1.setEmployer(manufacturer); |
| 194 |
hardwareDesigner1.setPrimaryEmployer(manufacturer); |
| 195 |
hardwareDesigner1.setSecondaryEmployer(manufacturer); |
| 196 |
computer1.setManufacturer(manufacturer); |
| 197 |
computer2.setManufacturer(manufacturer); |
| 198 |
board1.setMemories(memories); |
| 199 |
memory1.setBoard(board1); |
| 200 |
memory2.setBoard(board1); |
| 201 |
board1.setProcessors(processors); |
| 202 |
vectorProcessor1.setBoard(board1); |
| 203 |
softwareDesigner1.setPrimaryEmployer(manufacturer); |
| 204 |
softwareDesigner1.setSecondaryEmployer(manufacturer); |
| 205 |
|
| 206 |
// set 1:1 relationships |
| 207 |
computer1.setLocation(location1); |
| 208 |
computer2.setLocation(location2); |
| 209 |
|
| 210 |
// set attributes |
| 211 |
computer1.setName("CDC-6600"); |
| 212 |
computer2.setName("CM-5"); |
| 213 |
|
| 214 |
// persist all entities to the database in a single transaction |
| 215 |
em.persist(computer1); |
| 216 |
em.persist(computer2); |
| 217 |
em.persist(manufacturer); |
| 218 |
em.persist(user); |
| 219 |
em.persist(hardwareDesigner1); |
| 220 |
em.persist(softwareDesigner1); |
| 221 |
em.persist(vectorProcessor1); |
| 222 |
em.persist(board1); |
| 223 |
em.persist(memory1); |
| 224 |
em.persist(memory2); |
| 225 |
em.persist(location1); |
| 226 |
em.persist(location2); |
| 227 |
|
| 228 |
em.getTransaction().commit(); |
| 229 |
|
| 230 |
// get Metamodel representation of the entity schema |
| 231 |
metamodel = em.getMetamodel(); |
| 232 |
assertNotNull(metamodel); |
| 233 |
|
| 234 |
// Setup TypeSafe Criteria API query |
| 235 |
QueryBuilder aQueryBuilder = em.getQueryBuilder(); |
| 236 |
// Setup a query to get a list (the JPA 1.0 way) and compare it to the (JPA 2.0 way) |
| 237 |
// avoid a NPE on .where in query.setExpressionBuilder(((ExpressionImpl)this.where).getCurrentNode().getBuilder()); |
| 238 |
//TypedQuery<Computer> aManufacturerQuery = em.createQuery(aQueryBuilder.createQuery(Computer.class)); |
| 239 |
Query aManufacturerQuery = em.createQuery(aQueryBuilder.createQuery(Computer.class)); |
| 240 |
List<Computer> computersResultsList = aManufacturerQuery.getResultList(); |
| 241 |
Computer aComputer = (Computer)computersResultsList.get(0); |
| 242 |
/* |
| 243 |
// Get the primary key of the Computer |
| 244 |
CriteriaQuery<Tuple> aCriteriaQuery = aQueryBuilder.createQuery(Tuple.class); |
| 245 |
Root from = cq.from(Employee.class); |
| 246 |
cq.multiselect(from.get("id"), from.get("firstName")); |
| 247 |
cq.where(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")).add(qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName")))); |
| 248 |
TypedQuery<Tuple> typedQuery = em.createQuery(cq); |
| 249 |
|
| 250 |
typedQuery.setParameter("id", employee.getId()); |
| 251 |
typedQuery.setParameter("firstName", employee.getFirstName()); |
| 252 |
|
| 253 |
Tuple queryResult = typedQuery.getSingleResult(); |
| 254 |
assertTrue("Query Results do not match selection", queryResult.get(0).equals(employee.getId()) && queryResult.get(1).equals(employee.getFirstName())); |
| 255 |
*/ |
| 256 |
} catch (Exception e) { |
| 257 |
// we enter here on a failed commit() - for example if the table schema is incorrectly defined |
| 258 |
e.printStackTrace(); |
| 259 |
exceptionThrown = true; |
| 260 |
} finally { |
| 261 |
assertFalse(exceptionThrown); |
| 262 |
//finalizeForTest(em, entityMap); |
| 263 |
if(null != em) { |
| 264 |
cleanup(em); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Test the Metamodel API or lack of using it via a Criteria API using string based queries |
| 271 |
*/ |
| 272 |
public void testMetamodelStringBasedQuery() { |
| 273 |
EntityManagerFactory emf = null; |
| 274 |
EntityManager em = null; |
| 275 |
Set<Computer> computersList = new HashSet(); |
| 276 |
List<Memory> memories = new ArrayList(); |
| 277 |
List<VectorProcessor> processors = new ArrayList(); |
| 278 |
List<HardwareDesigner> hardwareDesigners = new ArrayList(); |
| 279 |
Computer computer1 = null; |
| 280 |
Computer computer2 = null; |
| 281 |
Manufacturer manufacturer = null; |
| 282 |
User user = null; |
| 283 |
HardwareDesigner hardwareDesigner1 = null; |
| 284 |
SoftwareDesigner softwareDesigner1 = null; |
| 285 |
VectorProcessor vectorProcessor1 = null; |
| 286 |
//ArrayProcessor arrayProcessor1 = null; |
| 287 |
Board board1 = null; |
| 288 |
Memory memory1 = null; |
| 289 |
Memory memory2 = null; |
| 290 |
Location location1 = null; |
| 291 |
Location location2 = null; |
| 292 |
boolean exceptionThrown = false; |
| 293 |
Metamodel metamodel = null; |
| 294 |
|
| 295 |
try { |
| 296 |
emf = initialize(); |
| 297 |
em = emf.createEntityManager(); |
| 298 |
em.getTransaction().begin(); |
| 299 |
|
| 300 |
// setup entity relationships |
| 301 |
computer1 = new Computer(); |
| 302 |
computer2 = new Computer(); |
| 303 |
memory1 = new Memory(); |
| 304 |
memory2 = new Memory(); |
| 305 |
manufacturer = new Manufacturer(); |
| 306 |
user = new User(); |
| 307 |
hardwareDesigner1 = new HardwareDesigner(); |
| 308 |
softwareDesigner1 = new SoftwareDesigner(); |
| 309 |
vectorProcessor1 = new VectorProcessor(); |
| 310 |
board1 = new Board(); |
| 311 |
location1 = new Location(); |
| 312 |
location2 = new Location(); |
| 313 |
|
| 314 |
// setup collections |
| 315 |
computersList.add(computer1); |
| 316 |
computersList.add(computer2); |
| 317 |
processors.add(vectorProcessor1); |
| 318 |
memories.add(memory1); |
| 319 |
memories.add(memory2); |
| 320 |
hardwareDesigners.add(hardwareDesigner1); |
| 321 |
|
| 322 |
// set owning and inverse sides of 1:m and m:1 relationships |
| 323 |
manufacturer.setComputers(computersList); |
| 324 |
manufacturer.setHardwareDesigners(hardwareDesigners); |
| 325 |
hardwareDesigner1.setEmployer(manufacturer); |
| 326 |
hardwareDesigner1.setPrimaryEmployer(manufacturer); |
| 327 |
hardwareDesigner1.setSecondaryEmployer(manufacturer); |
| 328 |
computer1.setManufacturer(manufacturer); |
| 329 |
computer2.setManufacturer(manufacturer); |
| 330 |
board1.setMemories(memories); |
| 331 |
memory1.setBoard(board1); |
| 332 |
memory2.setBoard(board1); |
| 333 |
board1.setProcessors(processors); |
| 334 |
vectorProcessor1.setBoard(board1); |
| 335 |
softwareDesigner1.setPrimaryEmployer(manufacturer); |
| 336 |
softwareDesigner1.setSecondaryEmployer(manufacturer); |
| 337 |
|
| 338 |
// set 1:1 relationships |
| 339 |
computer1.setLocation(location1); |
| 340 |
computer2.setLocation(location2); |
| 341 |
|
| 342 |
// set attributes |
| 343 |
computer1.setName("CDC-6600"); |
| 344 |
computer2.setName("CM-5"); |
| 345 |
|
| 346 |
// persist all entities to the database in a single transaction |
| 347 |
em.persist(computer1); |
| 348 |
em.persist(computer2); |
| 349 |
em.persist(manufacturer); |
| 350 |
em.persist(user); |
| 351 |
em.persist(hardwareDesigner1); |
| 352 |
em.persist(softwareDesigner1); |
| 353 |
em.persist(vectorProcessor1); |
| 354 |
//em.persist(arrayProcessor1); |
| 355 |
em.persist(board1); |
| 356 |
em.persist(memory1); |
| 357 |
em.persist(memory2); |
| 358 |
em.persist(location1); |
| 359 |
em.persist(location2); |
| 360 |
|
| 361 |
em.getTransaction().commit(); |
| 362 |
|
| 363 |
// get Metamodel representation of the entity schema |
| 364 |
metamodel = em.getMetamodel(); |
| 365 |
assertNotNull(metamodel); |
| 366 |
|
| 367 |
// Setup a non TypeSafe Criteria API query |
| 368 |
|
| 369 |
} catch (Exception e) { |
| 370 |
// we enter here on a failed commit() - for example if the table schema is incorrectly defined |
| 371 |
e.printStackTrace(); |
| 372 |
exceptionThrown = true; |
| 373 |
} finally { |
| 374 |
assertFalse(exceptionThrown); |
| 375 |
//finalizeForTest(em, entityMap); |
| 376 |
if(null != em) { |
| 377 |
cleanup(em); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
public void testImplementation() { |
| 383 |
EntityManagerFactory emf = null; |
| 384 |
EntityManager em = null; |
| 385 |
Set<Computer> computersList = new HashSet<Computer>(); |
172 |
Set<Computer> computersList = new HashSet<Computer>(); |
| 386 |
Collection<Memory> memories = new HashSet<Memory>(); |
173 |
Collection<Memory> memories = new HashSet<Memory>(); |
| 387 |
Collection<VectorProcessor> processors = new HashSet<VectorProcessor>(); |
174 |
Collection<VectorProcessor> processors = new HashSet<VectorProcessor>(); |
|
Lines 410-421
Link Here
|
| 410 |
|
197 |
|
| 411 |
try { |
198 |
try { |
| 412 |
emf = initialize(); |
199 |
emf = initialize(); |
| 413 |
//emf = initialize(); |
|
|
| 414 |
em = emf.createEntityManager(); |
200 |
em = emf.createEntityManager(); |
| 415 |
|
201 |
|
|
|
202 |
// Unset the metamodel - for repeated runs through this test |
| 203 |
((EntityManagerFactoryImpl)emf).setMetamodel(null); |
| 416 |
// Pre-Persist: get Metamodel representation of the entity schema |
204 |
// Pre-Persist: get Metamodel representation of the entity schema |
| 417 |
metamodel = em.getMetamodel(); |
205 |
metamodel = em.getMetamodel(); |
| 418 |
assertNotNull(metamodel); |
206 |
assertNotNull(metamodel); |
|
|
207 |
//System.out.println("_Metamodel just after initialization: " + metamodel); |
| 419 |
|
208 |
|
| 420 |
em.getTransaction().begin(); |
209 |
em.getTransaction().begin(); |
| 421 |
|
210 |
|
|
Lines 541-554
Link Here
|
| 541 |
EntityTypeImpl<Computer> entityComputer2 = (EntityTypeImpl)metamodel.entity(Computer.class); |
330 |
EntityTypeImpl<Computer> entityComputer2 = (EntityTypeImpl)metamodel.entity(Computer.class); |
| 542 |
Root from = criteriaQuery.from(entityComputer2); |
331 |
Root from = criteriaQuery.from(entityComputer2); |
| 543 |
Path path = from.get("name"); |
332 |
Path path = from.get("name"); |
| 544 |
criteriaQuery.where(qb.equal(path, "CM-5")); |
333 |
criteriaQuery.where(qb.equal(path, "CM-5")); |
| 545 |
Query query = em.createQuery(criteriaQuery); |
334 |
Query query = em.createQuery(criteriaQuery); |
| 546 |
results = query.getResultList(); |
335 |
results = query.getResultList(); |
| 547 |
if(results.size() > 0) { |
336 |
if(results.size() > 0) { |
| 548 |
Computer computer = (Computer)results.get(0); |
337 |
Computer computer = (Computer)results.get(0); |
| 549 |
assertNotNull(computer); |
338 |
assertNotNull(computer); |
| 550 |
} else { |
339 |
} else { |
| 551 |
fail("Results from criteria query (ReadAllQuery(referenceClass=Computer sql=SELECT COMPUTER_ID, NAME, COMPUTER_VERSION, MANUFACTURER_PERSON_ID, LOCATION_LOCATION_ID FROM CMP3_MM_COMPUTER WHERE NAME = 'CM-5') were expected"); |
340 |
fail("Results from criteria query (ReadAllQuery(referenceClass=Computer sql=SELECT COMPUTER_ID, NAME, COMPUTER_VERSION, MANUFACTURER_PERSON_ID, LOCATION_LOCATION_ID FROM CMP3_MM_COMPUTER WHERE NAME='CM-5') were expected"); |
| 552 |
} |
341 |
} |
| 553 |
} catch (Exception e) { |
342 |
} catch (Exception e) { |
| 554 |
e.printStackTrace(); |
343 |
e.printStackTrace(); |
|
Lines 716-722
Link Here
|
| 716 |
* Return the type that represents the type of the id. |
505 |
* Return the type that represents the type of the id. |
| 717 |
* @return type of id |
506 |
* @return type of id |
| 718 |
*/ |
507 |
*/ |
| 719 |
//Type<?> getIdType(); |
508 |
//Type<?> getIdType(); |
|
|
509 |
|
| 510 |
// Test EntityType |
| 511 |
|
| 720 |
// Test normal path for an [Embeddable] type via @EmbeddedId |
512 |
// Test normal path for an [Embeddable] type via @EmbeddedId |
| 721 |
expectedIAExceptionThrown = false; |
513 |
expectedIAExceptionThrown = false; |
| 722 |
Type<?> locationIdType = null; |
514 |
Type<?> locationIdType = null; |
|
Lines 732-738
Link Here
|
| 732 |
assertEquals(PersistenceType.EMBEDDABLE, locationIdType.getPersistenceType()); |
524 |
assertEquals(PersistenceType.EMBEDDABLE, locationIdType.getPersistenceType()); |
| 733 |
assertEquals(EmbeddedPK.class, locationIdType.getJavaType()); |
525 |
assertEquals(EmbeddedPK.class, locationIdType.getJavaType()); |
| 734 |
|
526 |
|
|
|
527 |
// check that the elementType and the owningType (managedType) are set correctly |
| 528 |
// See issue 50 where some mapping types were not setting the elementType correctly (this includes aggregate types like Embeddable) |
| 529 |
// http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_50:_20090727:_Handle_all_mapping_types_in_the_SingularAttribute_constructor |
| 530 |
// Get the ManagedType and check this SingularAttribute PK |
| 531 |
Attribute locationIdAttribute = entityLocation.getAttribute("primaryKey"); |
| 532 |
assertNotNull(locationIdAttribute); |
| 533 |
assertTrue(locationIdAttribute instanceof SingularAttributeImpl); |
| 534 |
assertFalse(locationIdAttribute.isCollection()); |
| 535 |
assertFalse(((AttributeImpl)locationIdAttribute).isPlural()); // non-spec. |
| 536 |
ManagedType locationIdAttributeManagedType = locationIdAttribute.getDeclaringType(); |
| 537 |
assertEquals(entityLocation, locationIdAttributeManagedType); |
| 538 |
ManagedTypeImpl locationIdAttributeManagedTypeImpl = ((SingularAttributeImpl)locationIdAttribute).getManagedTypeImpl(); |
| 539 |
assertEquals(locationIdType.getJavaType(), ((SingularAttributeImpl)locationIdAttribute).getBindableJavaType()); |
| 540 |
assertEquals(Bindable.BindableType.SINGULAR_ATTRIBUTE, ((SingularAttributeImpl)locationIdAttribute).getBindableType()); |
| 541 |
assertEquals(locationIdType.getJavaType(), locationIdAttribute.getJavaType()); |
| 542 |
Type embeddableType = ((SingularAttributeImpl)locationIdAttribute).getType(); |
| 543 |
assertNotNull(embeddableType); |
| 544 |
assertNotSame(embeddableType, locationIdAttributeManagedType); |
| 545 |
|
| 735 |
|
546 |
|
|
|
547 |
|
| 736 |
// Test normal path for a [Basic] type |
548 |
// Test normal path for a [Basic] type |
| 737 |
expectedIAExceptionThrown = false; |
549 |
expectedIAExceptionThrown = false; |
| 738 |
Type<?> computerIdType = null; |
550 |
Type<?> computerIdType = null; |
|
Lines 749-758
Link Here
|
| 749 |
assertEquals(Integer.class, computerIdType.getJavaType()); |
561 |
assertEquals(Integer.class, computerIdType.getJavaType()); |
| 750 |
|
562 |
|
| 751 |
|
563 |
|
|
|
564 |
|
| 565 |
// Test MappedSuperclassType |
| 566 |
// Test normal path for a [Basic] type |
| 567 |
expectedIAExceptionThrown = false; |
| 568 |
Type<?> personIdType = null; |
| 569 |
MappedSuperclassTypeImpl<Person> msPerson = (MappedSuperclassTypeImpl)metamodel.type(Person.class); |
| 570 |
assertNotNull(msPerson); |
| 571 |
|
| 572 |
try { |
| 573 |
personIdType = msPerson.getIdType(); |
| 574 |
} catch (IllegalArgumentException iae) { |
| 575 |
// expecting no exception |
| 576 |
iae.printStackTrace(); |
| 577 |
expectedIAExceptionThrown = true; |
| 578 |
} |
| 579 |
assertFalse(expectedIAExceptionThrown); |
| 580 |
assertNotNull(personIdType); |
| 581 |
assertEquals(PersistenceType.BASIC, personIdType.getPersistenceType()); |
| 582 |
assertEquals(Integer.class, personIdType.getJavaType()); |
| 583 |
|
| 752 |
// Verify all types (entities, embeddables, mappedsuperclasses and basic) |
584 |
// Verify all types (entities, embeddables, mappedsuperclasses and basic) |
| 753 |
try { |
585 |
try { |
| 754 |
// get all 19 types (a non spec function - for testing introspection) |
586 |
// get all 19 types (a non spec function - for testing introspection) |
| 755 |
Map<Class, TypeImpl<?>> typesMap = ((MetamodelImpl)metamodel).getTypes(); |
587 |
Map<Class, TypeImpl<?>> typesMap = ((MetamodelImpl)metamodel).getTypes(); |
|
|
588 |
System.out.println("_MetamodelMetamodelTest: all Types: " + typesMap); |
| 756 |
// verify each one |
589 |
// verify each one |
| 757 |
assertNotNull(typesMap); |
590 |
assertNotNull(typesMap); |
| 758 |
assertEquals(19, typesMap.size()); |
591 |
assertEquals(19, typesMap.size()); |
|
Lines 760-784
Link Here
|
| 760 |
e.printStackTrace(); |
593 |
e.printStackTrace(); |
| 761 |
} |
594 |
} |
| 762 |
/* |
595 |
/* |
| 763 |
{class org.eclipse.persistence.testing.models.jpa.metamodel.SoftwareDesigner=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.SoftwareDesigner --> [DatabaseTable(CMP3_MM_SWDESIGNER)])], |
596 |
* Metamodel model toString |
| 764 |
class org.eclipse.persistence.testing.models.jpa.metamodel.EmbeddedPK=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.EmbeddedPK --> [])], |
597 |
* ************************************************************************************ |
| 765 |
class org.eclipse.persistence.testing.models.jpa.metamodel.User=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.User --> [DatabaseTable(CMP3_MM_USER)])], |
598 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Person=MappedSuperclassTypeImpl@9206757 [ |
| 766 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Location=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Location --> [DatabaseTable(CMP3_MM_LOCATION)])], |
599 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Person |
| 767 |
class org.eclipse.persistence.testing.models.jpa.metamodel.CompositePK=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.CompositePK --> [])], |
600 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Person --> [DatabaseTable(__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME)]), |
| 768 |
class org.eclipse.persistence.testing.models.jpa.metamodel.ArrayProcessor=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.ArrayProcessor --> [DatabaseTable(CMP3_MM_ARRAYPROC)])], |
601 |
mappings: [ |
| 769 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer --> [DatabaseTable(CMP3_MM_MANUF)])], |
602 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 770 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Memory=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Memory --> [DatabaseTable(CMP3_MM_MEMORY)])], |
603 |
id-->__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME.PERSON_ID], |
| 771 |
class org.eclipse.persistence.testing.models.jpa.metamodel.VectorProcessor=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.VectorProcessor --> [DatabaseTable(CMP3_MM_PROC)])], |
604 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 772 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Board=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Board --> [DatabaseTable(CMP3_MM_BOARD)])], |
605 |
name-->__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME.NAME]]], |
| 773 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Computer=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Computer --> [DatabaseTable(CMP3_MM_COMPUTER)])], |
606 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Corporation=MappedSuperclassTypeImpl@27921979 [ |
| 774 |
class org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner=ManagedTypeImpl[RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner --> [DatabaseTable(CMP3_MM_HWDESIGNER)])], |
607 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Corporation |
| 775 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Corporation=MappedSuperclassTypeImpl@24202381 [descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Corporation --> [DatabaseTable(__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME)]), mappings: [org.eclipse.persistence.mappings.ManyToManyMapping[corporateComputers]]], |
608 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Corporation --> [DatabaseTable(__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME)]), |
| 776 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Designer=MappedSuperclassTypeImpl@22595578 [descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Designer --> [DatabaseTable(__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME)]), mappings: [org.eclipse.persistence.mappings.OneToOneMapping[secondaryEmployer], org.eclipse.persistence.mappings.OneToOneMapping[primaryEmployer]]], |
609 |
mappings: [ |
| 777 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Processor=MappedSuperclassTypeImpl@28381082 [descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Processor --> [DatabaseTable(__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME)]), mappings: []], |
610 |
org.eclipse.persistence.mappings.ManyToManyMapping[corporateComputers]]], |
| 778 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Person=MappedSuperclassTypeImpl@24600030 [descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Person --> [DatabaseTable(__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME)]), mappings: [org.eclipse.persistence.mappings.DirectToFieldMapping[id-->__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME.PERSON_ID], org.eclipse.persistence.mappings.DirectToFieldMapping[name-->__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME.NAME]]], |
611 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer=EntityTypeImpl@12565475 [ |
| 779 |
class java.lang.Integer=org.eclipse.persistence.internal.jpa.metamodel.BasicTypeImpl@1b7e37, |
612 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer |
| 780 |
class java.lang.String=org.eclipse.persistence.internal.jpa.metamodel.BasicTypeImpl@fb541d, |
613 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer --> [DatabaseTable(CMP3_MM_MANUF)]), |
| 781 |
int=org.eclipse.persistence.internal.jpa.metamodel.BasicTypeImpl@4f5403} |
614 |
mappings: [ |
|
|
615 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 616 |
id-->CMP3_MM_MANUF.PERSON_ID], |
| 617 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 618 |
name-->CMP3_MM_MANUF.NAME], |
| 619 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 620 |
version-->CMP3_MM_MANUF.MANUF_VERSION], |
| 621 |
org.eclipse.persistence.mappings.OneToManyMapping[ |
| 622 |
computers], |
| 623 |
org.eclipse.persistence.mappings.OneToManyMapping[ |
| 624 |
hardwareDesignersMap], |
| 625 |
org.eclipse.persistence.mappings.ManyToManyMapping[ |
| 626 |
corporateComputers], |
| 627 |
org.eclipse.persistence.mappings.OneToManyMapping[ |
| 628 |
hardwareDesigners]]], |
| 629 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Memory=EntityTypeImpl@29905988 [ |
| 630 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Memory |
| 631 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Memory --> [DatabaseTable(CMP3_MM_MEMORY)]), |
| 632 |
mappings: [ |
| 633 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 634 |
id-->CMP3_MM_MEMORY.MEMORY_ID], |
| 635 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 636 |
version-->CMP3_MM_MEMORY.MEMORY_VERSION], |
| 637 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 638 |
board]]], |
| 639 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Designer=MappedSuperclassTypeImpl@25971327 [ |
| 640 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Designer |
| 641 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Designer --> [DatabaseTable(__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME)]), |
| 642 |
mappings: [ |
| 643 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 644 |
secondaryEmployer], |
| 645 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 646 |
primaryEmployer], |
| 647 |
org.eclipse.persistence.mappings.ManyToManyMapping[ |
| 648 |
historicalEmployers]]], |
| 649 |
class org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner=EntityTypeImpl@18107298 [ |
| 650 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner |
| 651 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.HardwareDesigner --> [DatabaseTable(CMP3_MM_HWDESIGNER)]), |
| 652 |
mappings: [ |
| 653 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 654 |
id-->CMP3_MM_HWDESIGNER.PERSON_ID], |
| 655 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 656 |
name-->CMP3_MM_HWDESIGNER.NAME], |
| 657 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 658 |
version-->CMP3_MM_HWDESIGNER.HWDESIGNER_VERSION], |
| 659 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 660 |
employer], |
| 661 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 662 |
mappedEmployer], |
| 663 |
org.eclipse.persistence.mappings.ManyToManyMapping[ |
| 664 |
historicalEmployers], |
| 665 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 666 |
secondaryEmployer], |
| 667 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 668 |
primaryEmployer]]], |
| 669 |
class org.eclipse.persistence.testing.models.jpa.metamodel.SoftwareDesigner=EntityTypeImpl@26130360 [ |
| 670 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.SoftwareDesigner |
| 671 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.SoftwareDesigner --> [DatabaseTable(CMP3_MM_SWDESIGNER)]), |
| 672 |
mappings: [ |
| 673 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 674 |
id-->CMP3_MM_SWDESIGNER.PERSON_ID], |
| 675 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 676 |
name-->CMP3_MM_SWDESIGNER.NAME], |
| 677 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 678 |
version-->CMP3_MM_SWDESIGNER.SWDESIGNER_VERSION], |
| 679 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 680 |
secondaryEmployer], |
| 681 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 682 |
primaryEmployer], |
| 683 |
org.eclipse.persistence.mappings.ManyToManyMapping[ |
| 684 |
historicalEmployers]]], |
| 685 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Board=EntityTypeImpl@24223536 [ |
| 686 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Board |
| 687 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Board --> [DatabaseTable(CMP3_MM_BOARD)]), |
| 688 |
mappings: [ |
| 689 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 690 |
id-->CMP3_MM_BOARD.BOARD_ID], |
| 691 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 692 |
version-->CMP3_MM_BOARD.BOARD_VERSION], |
| 693 |
org.eclipse.persistence.mappings.OneToManyMapping[ |
| 694 |
memories], |
| 695 |
org.eclipse.persistence.mappings.OneToManyMapping[ |
| 696 |
processors]]], |
| 697 |
class org.eclipse.persistence.testing.models.jpa.metamodel.EmbeddedPK=EmbeddableTypeImpl@29441291 [ |
| 698 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.EmbeddedPK descriptor: |
| 699 |
RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.EmbeddedPK --> []), |
| 700 |
mappings: [ |
| 701 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 702 |
pk_part1-->LOCATION_ID]]], |
| 703 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Location=EntityTypeImpl@9050487 [ |
| 704 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Location |
| 705 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Location --> [DatabaseTable(CMP3_MM_LOCATION)]), |
| 706 |
mappings: [ |
| 707 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 708 |
version-->CMP3_MM_LOCATION.LOCATION_VERSION], |
| 709 |
org.eclipse.persistence.mappings.AggregateObjectMapping[ |
| 710 |
primaryKey]]], |
| 711 |
class org.eclipse.persistence.testing.models.jpa.metamodel.VectorProcessor=EntityTypeImpl@9300338 [ |
| 712 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.VectorProcessor |
| 713 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.VectorProcessor --> [DatabaseTable(CMP3_MM_PROC)]), |
| 714 |
mappings: [ |
| 715 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 716 |
id-->CMP3_MM_PROC.VECTPROC_ID], |
| 717 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 718 |
version-->CMP3_MM_PROC.VECTPROC_VERSION], |
| 719 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 720 |
board]]], |
| 721 |
class org.eclipse.persistence.testing.models.jpa.metamodel.ArrayProcessor=EntityTypeImpl@14247087 [ |
| 722 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.ArrayProcessor |
| 723 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.ArrayProcessor --> [DatabaseTable(CMP3_MM_ARRAYPROC)]), |
| 724 |
mappings: [ |
| 725 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 726 |
id-->CMP3_MM_ARRAYPROC.ARRAYPROC_ID], |
| 727 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 728 |
version-->CMP3_MM_ARRAYPROC.ARRAYPROC_VERSION], |
| 729 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 730 |
board]]], |
| 731 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Computer=EntityTypeImpl@8355938 [ |
| 732 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Computer |
| 733 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Computer --> [DatabaseTable(CMP3_MM_COMPUTER)]), |
| 734 |
mappings: [ |
| 735 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 736 |
id-->CMP3_MM_COMPUTER.COMPUTER_ID], |
| 737 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 738 |
name-->CMP3_MM_COMPUTER.NAME], |
| 739 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 740 |
version-->CMP3_MM_COMPUTER.COMPUTER_VERSION], |
| 741 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 742 |
manufacturer], |
| 743 |
org.eclipse.persistence.mappings.OneToOneMapping[ |
| 744 |
location]]], |
| 745 |
class org.eclipse.persistence.testing.models.jpa.metamodel.User=EntityTypeImpl@12968655 [ |
| 746 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.User |
| 747 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.User --> [DatabaseTable(CMP3_MM_USER)]), |
| 748 |
mappings: [ |
| 749 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 750 |
id-->CMP3_MM_USER.PERSON_ID], |
| 751 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 752 |
name-->CMP3_MM_USER.NAME], |
| 753 |
org.eclipse.persistence.mappings.DirectToFieldMapping[ |
| 754 |
version-->CMP3_MM_USER.USER_VERSION]]], |
| 755 |
class org.eclipse.persistence.testing.models.jpa.metamodel.Processor=MappedSuperclassTypeImpl@24044524 [ |
| 756 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.Processor |
| 757 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.Processor --> [DatabaseTable(__METAMODEL_RESERVED_IN_MEM_ONLY_TABLE_NAME)]), |
| 758 |
mappings: []], |
| 759 |
class org.eclipse.persistence.testing.models.jpa.metamodel.CompositePK=EmbeddableTypeImpl@6367194 [ |
| 760 |
javaType: class org.eclipse.persistence.testing.models.jpa.metamodel.CompositePK |
| 761 |
descriptor: RelationalDescriptor(org.eclipse.persistence.testing.models.jpa.metamodel.CompositePK --> []), |
| 762 |
mappings: []], |
| 763 |
class java.lang.Integer=BasicTypeImpl@33083511 [ |
| 764 |
javaType: class java.lang.Integer], |
| 765 |
class java.lang.String=BasicTypeImpl@4086417 [ |
| 766 |
javaType: class java.lang.String], |
| 767 |
int=BasicTypeImpl@28057122 [ |
| 768 |
javaType: int]} |
| 782 |
*/ |
769 |
*/ |
| 783 |
|
770 |
|
| 784 |
// Verify ManagedType operations |
771 |
// Verify ManagedType operations |
|
Lines 813-818
Link Here
|
| 813 |
* Return the attributes declared by the managed type. |
800 |
* Return the attributes declared by the managed type. |
| 814 |
*/ |
801 |
*/ |
| 815 |
//java.util.Set<Attribute<X, ?>> getDeclaredAttributes(); |
802 |
//java.util.Set<Attribute<X, ?>> getDeclaredAttributes(); |
|
|
803 |
expectedIAExceptionThrown = false; |
| 804 |
try { |
| 805 |
/** |
| 806 |
* Hierarchy: |
| 807 |
* Person : MappedSuperclass |
| 808 |
* + |
| 809 |
* +- id : Integer |
| 810 |
* +- name : String |
| 811 |
* |
| 812 |
* Corporation : MappedSuperclass extends Person |
| 813 |
* + |
| 814 |
* +- corpComputers : Set |
| 815 |
* |
| 816 |
* Manufacturer : Entity extends Corporation |
| 817 |
* + |
| 818 |
* +- computers : Set |
| 819 |
* +- hardwareDesigners : Set |
| 820 |
* +- hardwareDesignersMap : Map |
| 821 |
* +- version : int |
| 822 |
*/ |
| 823 |
Set<Attribute<Manufacturer, ?>> declaredAttributesSet = entityManufacturer.getDeclaredAttributes(); |
| 824 |
//System.out.println("entityManufacturer.getDeclaredAttributes() " + declaredAttributesSet); |
| 825 |
assertNotNull(declaredAttributesSet); |
| 826 |
// We should see 4 declared out of 7 attributes for Manufacturer |
| 827 |
assertEquals(4, declaredAttributesSet.size()); |
| 828 |
// Id is declared 2 levels above |
| 829 |
assertFalse(declaredAttributesSet.contains(entityManufacturer.getAttribute("id"))); // |
| 830 |
// name is declared 2 levels above |
| 831 |
assertFalse(declaredAttributesSet.contains(entityManufacturer.getAttribute("name"))); // |
| 832 |
// corpComputers is declared 1 level above |
| 833 |
assertFalse(declaredAttributesSet.contains(entityManufacturer.getAttribute("corporateComputers"))); // |
| 834 |
// version is declared at this level |
| 835 |
assertTrue(declaredAttributesSet.contains(entityManufacturer.getAttribute("version"))); // |
| 836 |
// computers is declared at this level |
| 837 |
assertTrue(declaredAttributesSet.contains(entityManufacturer.getAttribute("computers"))); // |
| 838 |
// hardwareDesigners is declared at this level |
| 839 |
assertTrue(declaredAttributesSet.contains(entityManufacturer.getAttribute("hardwareDesigners"))); // |
| 840 |
// hardwareDesignersMap is declared at this level |
| 841 |
assertTrue(declaredAttributesSet.contains(entityManufacturer.getAttribute("hardwareDesignersMap"))); // |
| 842 |
} catch (IllegalArgumentException iae) { |
| 843 |
iae.printStackTrace(); |
| 844 |
expectedIAExceptionThrown = true; |
| 845 |
} |
| 846 |
assertFalse(expectedIAExceptionThrown); |
| 847 |
|
| 848 |
|
| 816 |
|
849 |
|
| 817 |
/** |
850 |
/** |
| 818 |
* Return the single-valued attribute of the managed |
851 |
* Return the single-valued attribute of the managed |
|
Lines 1374-1382
Link Here
|
| 1374 |
*/ |
1407 |
*/ |
| 1375 |
//public boolean hasVersionAttribute() { |
1408 |
//public boolean hasVersionAttribute() { |
| 1376 |
|
1409 |
|
|
|
1410 |
|
| 1411 |
// Verify MetamodelImpl operations |
| 1412 |
//////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1413 |
/** |
| 1414 |
* Return the metamodel entity type representing the entity. |
| 1415 |
* @param cls the type of the represented entity |
| 1416 |
* @return the metamodel entity type |
| 1417 |
* @throws IllegalArgumentException if not an entity |
| 1418 |
*/ |
| 1419 |
//<X> EntityType<X> entity(Class<X> cls); |
| 1420 |
// test normal path |
| 1421 |
expectedIAExceptionThrown = false; |
| 1422 |
try { |
| 1423 |
EntityType<Manufacturer> aType = metamodel.entity(Manufacturer.class); |
| 1424 |
} catch (IllegalArgumentException iae) { |
| 1425 |
//iae.printStackTrace(); |
| 1426 |
expectedIAExceptionThrown = true; |
| 1427 |
} |
| 1428 |
assertFalse(expectedIAExceptionThrown); |
| 1377 |
|
1429 |
|
|
|
1430 |
// test variant path: null causes IAE |
| 1431 |
expectedIAExceptionThrown = false; |
| 1432 |
try { |
| 1433 |
EntityType<Manufacturer> aType = metamodel.entity(null); |
| 1434 |
} catch (IllegalArgumentException iae) { |
| 1435 |
//iae.printStackTrace(); |
| 1436 |
expectedIAExceptionThrown = true; |
| 1437 |
} |
| 1438 |
assertTrue(expectedIAExceptionThrown); |
| 1439 |
|
| 1440 |
// test variant path: wrong type (java simple type) |
| 1441 |
expectedIAExceptionThrown = false; |
| 1442 |
try { |
| 1443 |
EntityType<Integer> aType = metamodel.entity(Integer.class); |
| 1444 |
} catch (IllegalArgumentException iae) { |
| 1445 |
//iae.printStackTrace(); |
| 1446 |
expectedIAExceptionThrown = true; |
| 1447 |
} |
| 1448 |
assertTrue(expectedIAExceptionThrown); |
| 1449 |
|
| 1450 |
// test variant path: wrong type (BasicType) |
| 1378 |
|
1451 |
|
|
|
1452 |
|
| 1453 |
/** |
| 1454 |
* Return the metamodel managed type representing the |
| 1455 |
* entity, mapped superclass, or embeddable class. |
| 1456 |
* @param cls the type of the represented managed class |
| 1457 |
* @return the metamodel managed type |
| 1458 |
* @throws IllegalArgumentException if not a managed class |
| 1459 |
*/ |
| 1460 |
//<X> ManagedType<X> type(Class<X> cls); |
| 1461 |
// test normal path (subtype = Basic) |
| 1462 |
/* expectedIAExceptionThrown = false; |
| 1463 |
try { |
| 1464 |
Type<Manufacturer> aType = metamodel.type(Basic.class); |
| 1465 |
} catch (IllegalArgumentException iae) { |
| 1466 |
iae.printStackTrace(); |
| 1467 |
expectedIAExceptionThrown = true; |
| 1468 |
} |
| 1469 |
assertFalse(expectedIAExceptionThrown); |
| 1470 |
*/ |
| 1471 |
// test normal path (subtype = Embeddable) |
| 1472 |
expectedIAExceptionThrown = false; |
| 1473 |
try { |
| 1474 |
Type<EmbeddedPK> aType = metamodel.type(EmbeddedPK.class); |
| 1475 |
} catch (IllegalArgumentException iae) { |
| 1476 |
iae.printStackTrace(); |
| 1477 |
expectedIAExceptionThrown = true; |
| 1478 |
} |
| 1479 |
assertFalse(expectedIAExceptionThrown); |
| 1379 |
|
1480 |
|
|
|
1481 |
// test normal path: (subtype = Entity) |
| 1482 |
expectedIAExceptionThrown = false; |
| 1483 |
try { |
| 1484 |
Type<Manufacturer> aType = metamodel.type(Manufacturer.class); |
| 1485 |
} catch (IllegalArgumentException iae) { |
| 1486 |
iae.printStackTrace(); |
| 1487 |
expectedIAExceptionThrown = true; |
| 1488 |
} |
| 1489 |
assertFalse(expectedIAExceptionThrown); |
| 1490 |
|
| 1491 |
// test normal path: (subtype = MappedSuperclass) |
| 1492 |
expectedIAExceptionThrown = false; |
| 1493 |
try { |
| 1494 |
Type<Person> aType = metamodel.type(Person.class); |
| 1495 |
} catch (IllegalArgumentException iae) { |
| 1496 |
iae.printStackTrace(); |
| 1497 |
expectedIAExceptionThrown = true; |
| 1498 |
} |
| 1499 |
assertFalse(expectedIAExceptionThrown); |
| 1500 |
|
| 1501 |
// 20090803: 2 tests below commented until bug# 285512 is fixed |
| 1502 |
/* |
| 1503 |
// test variant path: null causes IAE |
| 1504 |
expectedIAExceptionThrown = false; |
| 1505 |
try { |
| 1506 |
Type<?> aType = metamodel.type(null); |
| 1507 |
} catch (IllegalArgumentException iae) { |
| 1508 |
//iae.printStackTrace(); |
| 1509 |
expectedIAExceptionThrown = true; |
| 1510 |
} |
| 1511 |
assertTrue(expectedIAExceptionThrown); |
| 1512 |
|
| 1513 |
// test variant path: wrong type (java simple type) |
| 1514 |
expectedIAExceptionThrown = false; |
| 1515 |
try { |
| 1516 |
Type<?> aType = metamodel.embeddable(Integer.class); |
| 1517 |
} catch (IllegalArgumentException iae) { |
| 1518 |
//iae.printStackTrace(); |
| 1519 |
expectedIAExceptionThrown = true; |
| 1520 |
} |
| 1521 |
assertTrue(expectedIAExceptionThrown); |
| 1522 |
|
| 1523 |
// test variant path: wrong type (BasicType) |
| 1524 |
*/ |
| 1525 |
/** |
| 1526 |
* Return the metamodel embeddable type representing the |
| 1527 |
* embeddable class. |
| 1528 |
* @param cls the type of the represented embeddable class |
| 1529 |
* @return the metamodel embeddable type |
| 1530 |
* @throws IllegalArgumentException if not an embeddable class |
| 1531 |
*/ |
| 1532 |
//<X> EmbeddableType<X> embeddable(Class<X> cls); |
| 1533 |
// test normal path |
| 1534 |
expectedIAExceptionThrown = false; |
| 1535 |
try { |
| 1536 |
EmbeddableType<EmbeddedPK> aType = metamodel.embeddable(EmbeddedPK.class); |
| 1537 |
} catch (IllegalArgumentException iae) { |
| 1538 |
iae.printStackTrace(); |
| 1539 |
expectedIAExceptionThrown = true; |
| 1540 |
} |
| 1541 |
assertFalse(expectedIAExceptionThrown); |
| 1542 |
|
| 1543 |
// test variant path: null causes IAE |
| 1544 |
expectedIAExceptionThrown = false; |
| 1545 |
try { |
| 1546 |
EmbeddableType<Manufacturer> aType = metamodel.embeddable(null); |
| 1547 |
} catch (IllegalArgumentException iae) { |
| 1548 |
//iae.printStackTrace(); |
| 1549 |
expectedIAExceptionThrown = true; |
| 1550 |
} |
| 1551 |
assertTrue(expectedIAExceptionThrown); |
| 1552 |
|
| 1553 |
// test variant path: wrong type (subtype = Entity) |
| 1554 |
expectedIAExceptionThrown = false; |
| 1555 |
try { |
| 1556 |
EmbeddableType<Manufacturer> aType = metamodel.embeddable(Manufacturer.class); |
| 1557 |
} catch (IllegalArgumentException iae) { |
| 1558 |
//iae.printStackTrace(); |
| 1559 |
expectedIAExceptionThrown = true; |
| 1560 |
} |
| 1561 |
assertTrue(expectedIAExceptionThrown); |
| 1562 |
|
| 1563 |
// test variant path: wrong type (java simple type) |
| 1564 |
expectedIAExceptionThrown = false; |
| 1565 |
try { |
| 1566 |
EmbeddableType<?> aType = metamodel.embeddable(Integer.class); |
| 1567 |
} catch (IllegalArgumentException iae) { |
| 1568 |
//iae.printStackTrace(); |
| 1569 |
expectedIAExceptionThrown = true; |
| 1570 |
} |
| 1571 |
assertTrue(expectedIAExceptionThrown); |
| 1572 |
|
| 1573 |
// test variant path: wrong type (BasicType) |
| 1574 |
|
| 1575 |
/** |
| 1576 |
* Return the metamodel managed types. |
| 1577 |
* @return the metamodel managed types |
| 1578 |
*/ |
| 1579 |
//java.util.Set<ManagedType<?>> getManagedTypes(); |
| 1580 |
|
| 1581 |
/** |
| 1582 |
* Return the metamodel entity types. |
| 1583 |
* @return the metamodel entity types |
| 1584 |
*/ |
| 1585 |
//java.util.Set<EntityType<?>> getEntities(); |
| 1586 |
|
| 1587 |
/** |
| 1588 |
* Return the metamodel embeddable types. |
| 1589 |
* @return the metamodel embeddable types |
| 1590 |
*/ |
| 1591 |
//java.util.Set<EmbeddableType<?>> getEmbeddables(); |
| 1592 |
|
| 1593 |
|
| 1380 |
// get some static (non-runtime) attributes parameterized by <Owning type, return Type> |
1594 |
// get some static (non-runtime) attributes parameterized by <Owning type, return Type> |
| 1381 |
// Note: the String based attribute names are non type-safe |
1595 |
// Note: the String based attribute names are non type-safe |
| 1382 |
/* |
1596 |
/* |
|
Lines 1557-1562
Link Here
|
| 1557 |
e.printStackTrace(); |
1771 |
e.printStackTrace(); |
| 1558 |
exceptionThrown = true; |
1772 |
exceptionThrown = true; |
| 1559 |
} finally { |
1773 |
} finally { |
|
|
1774 |
// Runtime behavior should not affect the metamodel |
| 1775 |
// MetamodelImpl@15868511 [ 19 Types: , 16 ManagedTypes: , 10 EntityTypes: , 4 MappedSuperclassTypes: , 2 EmbeddableTypes: ] |
| 1776 |
//System.out.println("_Metamodel at test end: " + metamodel); |
| 1560 |
assertFalse(exceptionThrown); |
1777 |
assertFalse(exceptionThrown); |
| 1561 |
//finalizeForTest(em, entityMap); |
1778 |
//finalizeForTest(em, entityMap); |
| 1562 |
try { |
1779 |
try { |
|
Lines 1607-1610
Link Here
|
| 1607 |
public java.util.Set<MappedSuperclassTypeImpl<?>> getMappedSuperclasses() { |
1824 |
public java.util.Set<MappedSuperclassTypeImpl<?>> getMappedSuperclasses() { |
| 1608 |
public void setMappedSuperclasses( |
1825 |
public void setMappedSuperclasses( |
| 1609 |
*/ |
1826 |
*/ |
|
|
1827 |
|
| 1828 |
/** |
| 1829 |
* Test the Metamodel API using a TypeSafe query via the Criteria API (a user of the Metamodel) |
| 1830 |
*/ |
| 1831 |
public void testMetamodelTypeSafeBasedQuery() { |
| 1832 |
EntityManagerFactory emf = null; |
| 1833 |
EntityManager em = null; |
| 1834 |
|
| 1835 |
Set<Computer> computersList = new HashSet(); |
| 1836 |
List<Memory> memories = new ArrayList(); |
| 1837 |
List<VectorProcessor> processors = new ArrayList(); |
| 1838 |
List<HardwareDesigner> hardwareDesigners = new ArrayList(); |
| 1839 |
Computer computer1 = null; |
| 1840 |
Computer computer2 = null; |
| 1841 |
Manufacturer manufacturer = null; |
| 1842 |
User user = null; |
| 1843 |
HardwareDesigner hardwareDesigner1 = null; |
| 1844 |
SoftwareDesigner softwareDesigner1 = null; |
| 1845 |
VectorProcessor vectorProcessor1 = null; |
| 1846 |
Board board1 = null; |
| 1847 |
Memory memory1 = null; |
| 1848 |
Memory memory2 = null; |
| 1849 |
Location location1 = null; |
| 1850 |
Location location2 = null; |
| 1851 |
boolean exceptionThrown = false; |
| 1852 |
Metamodel metamodel = null; |
| 1853 |
|
| 1854 |
try { |
| 1855 |
emf = initialize(); |
| 1856 |
em = emf.createEntityManager(); |
| 1857 |
em.getTransaction().begin(); |
| 1858 |
|
| 1859 |
// setup entity relationships |
| 1860 |
computer1 = new Computer(); |
| 1861 |
computer2 = new Computer(); |
| 1862 |
memory1 = new Memory(); |
| 1863 |
memory2 = new Memory(); |
| 1864 |
manufacturer = new Manufacturer(); |
| 1865 |
user = new User(); |
| 1866 |
hardwareDesigner1 = new HardwareDesigner(); |
| 1867 |
softwareDesigner1 = new SoftwareDesigner(); |
| 1868 |
vectorProcessor1 = new VectorProcessor(); |
| 1869 |
board1 = new Board(); |
| 1870 |
location1 = new Location(); |
| 1871 |
location2 = new Location(); |
| 1872 |
|
| 1873 |
// setup collections |
| 1874 |
computersList.add(computer1); |
| 1875 |
computersList.add(computer2); |
| 1876 |
processors.add(vectorProcessor1); |
| 1877 |
memories.add(memory1); |
| 1878 |
memories.add(memory2); |
| 1879 |
hardwareDesigners.add(hardwareDesigner1); |
| 1880 |
|
| 1881 |
// set owning and inverse sides of 1:m and m:1 relationships |
| 1882 |
manufacturer.setComputers(computersList); |
| 1883 |
manufacturer.setHardwareDesigners(hardwareDesigners); |
| 1884 |
hardwareDesigner1.setEmployer(manufacturer); |
| 1885 |
hardwareDesigner1.setPrimaryEmployer(manufacturer); |
| 1886 |
hardwareDesigner1.setSecondaryEmployer(manufacturer); |
| 1887 |
computer1.setManufacturer(manufacturer); |
| 1888 |
computer2.setManufacturer(manufacturer); |
| 1889 |
board1.setMemories(memories); |
| 1890 |
memory1.setBoard(board1); |
| 1891 |
memory2.setBoard(board1); |
| 1892 |
board1.setProcessors(processors); |
| 1893 |
vectorProcessor1.setBoard(board1); |
| 1894 |
softwareDesigner1.setPrimaryEmployer(manufacturer); |
| 1895 |
softwareDesigner1.setSecondaryEmployer(manufacturer); |
| 1896 |
|
| 1897 |
// set 1:1 relationships |
| 1898 |
computer1.setLocation(location1); |
| 1899 |
computer2.setLocation(location2); |
| 1900 |
|
| 1901 |
// set attributes |
| 1902 |
computer1.setName("CDC-6600"); |
| 1903 |
computer2.setName("CM-5"); |
| 1904 |
|
| 1905 |
// persist all entities to the database in a single transaction |
| 1906 |
em.persist(computer1); |
| 1907 |
em.persist(computer2); |
| 1908 |
em.persist(manufacturer); |
| 1909 |
em.persist(user); |
| 1910 |
em.persist(hardwareDesigner1); |
| 1911 |
em.persist(softwareDesigner1); |
| 1912 |
em.persist(vectorProcessor1); |
| 1913 |
em.persist(board1); |
| 1914 |
em.persist(memory1); |
| 1915 |
em.persist(memory2); |
| 1916 |
em.persist(location1); |
| 1917 |
em.persist(location2); |
| 1918 |
|
| 1919 |
em.getTransaction().commit(); |
| 1920 |
|
| 1921 |
// get Metamodel representation of the entity schema |
| 1922 |
metamodel = em.getMetamodel(); |
| 1923 |
assertNotNull(metamodel); |
| 1924 |
|
| 1925 |
// Setup TypeSafe Criteria API query |
| 1926 |
QueryBuilder aQueryBuilder = em.getQueryBuilder(); |
| 1927 |
// Setup a query to get a list (the JPA 1.0 way) and compare it to the (JPA 2.0 way) |
| 1928 |
// avoid a NPE on .where in query.setExpressionBuilder(((ExpressionImpl)this.where).getCurrentNode().getBuilder()); |
| 1929 |
//TypedQuery<Computer> aManufacturerQuery = em.createQuery(aQueryBuilder.createQuery(Computer.class)); |
| 1930 |
Query aManufacturerQuery = em.createQuery(aQueryBuilder.createQuery(Computer.class)); |
| 1931 |
List<Computer> computersResultsList = aManufacturerQuery.getResultList(); |
| 1932 |
Computer aComputer = (Computer)computersResultsList.get(0); |
| 1933 |
/* |
| 1934 |
// Get the primary key of the Computer |
| 1935 |
CriteriaQuery<Tuple> aCriteriaQuery = aQueryBuilder.createQuery(Tuple.class); |
| 1936 |
Root from = cq.from(Employee.class); |
| 1937 |
cq.multiselect(from.get("id"), from.get("firstName")); |
| 1938 |
cq.where(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")).add(qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName")))); |
| 1939 |
TypedQuery<Tuple> typedQuery = em.createQuery(cq); |
| 1940 |
|
| 1941 |
typedQuery.setParameter("id", employee.getId()); |
| 1942 |
typedQuery.setParameter("firstName", employee.getFirstName()); |
| 1943 |
|
| 1944 |
Tuple queryResult = typedQuery.getSingleResult(); |
| 1945 |
assertTrue("Query Results do not match selection", queryResult.get(0).equals(employee.getId()) && queryResult.get(1).equals(employee.getFirstName())); |
| 1946 |
*/ |
| 1947 |
} catch (Exception e) { |
| 1948 |
// we enter here on a failed commit() - for example if the table schema is incorrectly defined |
| 1949 |
e.printStackTrace(); |
| 1950 |
exceptionThrown = true; |
| 1951 |
} finally { |
| 1952 |
assertFalse(exceptionThrown); |
| 1953 |
//finalizeForTest(em, entityMap); |
| 1954 |
if(null != em) { |
| 1955 |
cleanup(em); |
| 1956 |
} |
| 1957 |
} |
| 1958 |
} |
| 1959 |
|
| 1960 |
/** |
| 1961 |
* Test the Metamodel API or lack of using it via a Criteria API using string based queries |
| 1962 |
*/ |
| 1963 |
public void testMetamodelStringBasedQuery() { |
| 1964 |
EntityManagerFactory emf = null; |
| 1965 |
EntityManager em = null; |
| 1966 |
Set<Computer> computersList = new HashSet(); |
| 1967 |
List<Memory> memories = new ArrayList(); |
| 1968 |
List<VectorProcessor> processors = new ArrayList(); |
| 1969 |
List<HardwareDesigner> hardwareDesigners = new ArrayList(); |
| 1970 |
Computer computer1 = null; |
| 1971 |
Computer computer2 = null; |
| 1972 |
Manufacturer manufacturer = null; |
| 1973 |
User user = null; |
| 1974 |
HardwareDesigner hardwareDesigner1 = null; |
| 1975 |
SoftwareDesigner softwareDesigner1 = null; |
| 1976 |
VectorProcessor vectorProcessor1 = null; |
| 1977 |
//ArrayProcessor arrayProcessor1 = null; |
| 1978 |
Board board1 = null; |
| 1979 |
Memory memory1 = null; |
| 1980 |
Memory memory2 = null; |
| 1981 |
Location location1 = null; |
| 1982 |
Location location2 = null; |
| 1983 |
boolean exceptionThrown = false; |
| 1984 |
Metamodel metamodel = null; |
| 1985 |
|
| 1986 |
try { |
| 1987 |
emf = initialize(); |
| 1988 |
em = emf.createEntityManager(); |
| 1989 |
em.getTransaction().begin(); |
| 1990 |
|
| 1991 |
// setup entity relationships |
| 1992 |
computer1 = new Computer(); |
| 1993 |
computer2 = new Computer(); |
| 1994 |
memory1 = new Memory(); |
| 1995 |
memory2 = new Memory(); |
| 1996 |
manufacturer = new Manufacturer(); |
| 1997 |
user = new User(); |
| 1998 |
hardwareDesigner1 = new HardwareDesigner(); |
| 1999 |
softwareDesigner1 = new SoftwareDesigner(); |
| 2000 |
vectorProcessor1 = new VectorProcessor(); |
| 2001 |
board1 = new Board(); |
| 2002 |
location1 = new Location(); |
| 2003 |
location2 = new Location(); |
| 2004 |
|
| 2005 |
// setup collections |
| 2006 |
computersList.add(computer1); |
| 2007 |
computersList.add(computer2); |
| 2008 |
processors.add(vectorProcessor1); |
| 2009 |
memories.add(memory1); |
| 2010 |
memories.add(memory2); |
| 2011 |
hardwareDesigners.add(hardwareDesigner1); |
| 2012 |
|
| 2013 |
// set owning and inverse sides of 1:m and m:1 relationships |
| 2014 |
manufacturer.setComputers(computersList); |
| 2015 |
manufacturer.setHardwareDesigners(hardwareDesigners); |
| 2016 |
hardwareDesigner1.setEmployer(manufacturer); |
| 2017 |
hardwareDesigner1.setPrimaryEmployer(manufacturer); |
| 2018 |
hardwareDesigner1.setSecondaryEmployer(manufacturer); |
| 2019 |
computer1.setManufacturer(manufacturer); |
| 2020 |
computer2.setManufacturer(manufacturer); |
| 2021 |
board1.setMemories(memories); |
| 2022 |
memory1.setBoard(board1); |
| 2023 |
memory2.setBoard(board1); |
| 2024 |
board1.setProcessors(processors); |
| 2025 |
vectorProcessor1.setBoard(board1); |
| 2026 |
softwareDesigner1.setPrimaryEmployer(manufacturer); |
| 2027 |
softwareDesigner1.setSecondaryEmployer(manufacturer); |
| 2028 |
|
| 2029 |
// set 1:1 relationships |
| 2030 |
computer1.setLocation(location1); |
| 2031 |
computer2.setLocation(location2); |
| 2032 |
|
| 2033 |
// set attributes |
| 2034 |
computer1.setName("CDC-6600"); |
| 2035 |
computer2.setName("CM-5"); |
| 2036 |
|
| 2037 |
// persist all entities to the database in a single transaction |
| 2038 |
em.persist(computer1); |
| 2039 |
em.persist(computer2); |
| 2040 |
em.persist(manufacturer); |
| 2041 |
em.persist(user); |
| 2042 |
em.persist(hardwareDesigner1); |
| 2043 |
em.persist(softwareDesigner1); |
| 2044 |
em.persist(vectorProcessor1); |
| 2045 |
//em.persist(arrayProcessor1); |
| 2046 |
em.persist(board1); |
| 2047 |
em.persist(memory1); |
| 2048 |
em.persist(memory2); |
| 2049 |
em.persist(location1); |
| 2050 |
em.persist(location2); |
| 2051 |
|
| 2052 |
em.getTransaction().commit(); |
| 2053 |
|
| 2054 |
// get Metamodel representation of the entity schema |
| 2055 |
metamodel = em.getMetamodel(); |
| 2056 |
assertNotNull(metamodel); |
| 2057 |
|
| 2058 |
// Setup a non TypeSafe Criteria API query |
| 2059 |
|
| 2060 |
} catch (Exception e) { |
| 2061 |
// we enter here on a failed commit() - for example if the table schema is incorrectly defined |
| 2062 |
e.printStackTrace(); |
| 2063 |
exceptionThrown = true; |
| 2064 |
} finally { |
| 2065 |
assertFalse(exceptionThrown); |
| 2066 |
//finalizeForTest(em, entityMap); |
| 2067 |
if(null != em) { |
| 2068 |
cleanup(em); |
| 2069 |
} |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
|
| 1610 |
} |
2074 |
} |