* Validates explicit field access can be applied to all the access
* strategies from within an entity with explicit property access (except
* for the id field, which is property default)
*/
public void testFieldAccessStrategies() {
OpenJPAEntityManagerSPI em = emf.createEntityManager();
FieldAccessPropStratsEntity fa = new FieldAccessPropStratsEntity();
// Load all persistent fields
EmbedId eid = new EmbedId();
eid.setId(new Random().nextInt());
eid.setCode("IdCode");
fa.setEmbedId(eid); // embedded id
Collection<EmbedPropAccess> elc = new ArrayList<EmbedPropAccess>();
EmbedPropAccess epa1 = new EmbedPropAccess("George", "Washington");
EmbedPropAccess epa2 = new EmbedPropAccess("James", "Carter");
elc.add(epa1);
elc.add(epa2);
fa.setElementCollection(elc); // element collection of embeddables
EmbedFieldAccess efa = new EmbedFieldAccess();
efa.setFirstName("The");
efa.setLastName("President");
fa.setEmbedField(efa); // embedded
fa.setName("FieldAccessPropStratsEntity"); // basic
PropAccess propa = new PropAccess();
propa.setStrProp("PropAccess");
fa.setManyToOne(propa); // many to one
Collection<FieldAccess> fac = new ArrayList<FieldAccess>();
FieldAccess fae = new FieldAccess();
fae.setStrField("FieldAccess");
fac.add(fae);
fa.setOneToMany(fac); // one to many
PropAccess pa = new PropAccess();
pa.setStrProp("PropAccess");
fa.setOneToOne(pa); // one to one
em.getTransaction().begin();
em.persist(fa);
em.getTransaction().commit();
em.clear();
// Verify list of persistent fields
FieldAccessPropStratsEntity newpa =
em.find(FieldAccessPropStratsEntity.class, eid);
assertNotNull(newpa);
// simple key validation
assertEquals(newpa.getEmbedId(), eid);
// Verify the persistent member names
MetaDataRepository mdr =
em.getConfiguration().getMetaDataRepositoryInstance();
ClassMetaData cmd = mdr.getMetaData(FieldAccessPropStratsEntity.class,
null, true);
// Assert expected persistent fields and properties were created
assertNotNull(cmd.getField("eid"));
assertNotNull(cmd.getField("elementCollection"));
assertNotNull(cmd.getField("embedField"));
assertNotNull(cmd.getField("version"));
assertNotNull(cmd.getField("manyToOne"));
assertNotNull(cmd.getField("oneToMany"));
assertNotNull(cmd.getField("oneToOne"));
assertNotNull(cmd.getField("manyToMany"));
// Name has a matching getter/setter. Make sure the access type
// is property & not field
assertNotNull(cmd.getField("name"));
assertTrue((cmd.getField("name").getAccessType() & AccessCode.PROPERTY)
== AccessCode.PROPERTY);
// Assert mappings were not created for fields or properties which
// should not be persistent
assertNull(cmd.getField("embedId"));
assertNull(cmd.getField("m2one"));
assertNull(cmd.getField("one2m"));
assertNull(cmd.getField("one2one"));
assertNull(cmd.getField("ecoll"));
assertNull(cmd.getField("embed"));
assertNull(cmd.getField("ver"));
assertNull(cmd.getField("m2m"));
em.close();
}