@Test
public void testCreateForPersistenceContextField1() throws Exception {
class Bean {
@PersistenceContext
private Object foo;
}
Field field = Bean.class.getDeclaredField("foo");
PersistenceContext ctx = field.getAnnotation(PersistenceContext.class);
Reference r = Reference.createFor(ctx, field);
assertEquals(EntityManager.class, r.getInterfaceOrClass());
assertEquals(Bean.class.getName() + "/foo", r.getName());
}
java类javax.persistence.PersistenceContext的实例源码
ReferenceTest.java 文件源码
项目:oscm
阅读 27
收藏 0
点赞 0
评论 0
ReferenceTest.java 文件源码
项目:oscm
阅读 39
收藏 0
点赞 0
评论 0
@Test
public void testCreateForPersistenceContextField2() throws Exception {
class Bean {
@PersistenceContext(name = "other")
private Object foo;
}
Field field = Bean.class.getDeclaredField("foo");
PersistenceContext ctx = field.getAnnotation(PersistenceContext.class);
Reference r = Reference.createFor(ctx, field);
assertEquals(EntityManager.class, r.getInterfaceOrClass());
assertEquals("other", r.getName());
}
Key.java 文件源码
项目:minijax
阅读 31
收藏 0
点赞 0
评论 0
private void processAnnotation(final Annotation annotation) {
final Class<? extends Annotation> annType = annotation.annotationType();
if (annType == Context.class) {
setStrategy(Strategy.CONTEXT);
} else if (annType == CookieParam.class) {
processCookieParamAnnotation((CookieParam) annotation);
} else if (annType == FormParam.class) {
processFormParamAnnotation((FormParam) annotation);
} else if (annType == HeaderParam.class) {
processHeaderParamAnnotation((HeaderParam) annotation);
} else if (annType == Named.class) {
processNamedAnnotation((Named) annotation);
} else if (annType == PathParam.class) {
processPathParamAnnotation((PathParam) annotation);
} else if (annType == OptionalClasses.PERSISTENCE_CONTEXT) {
processPersistenceContextAnnotation((PersistenceContext) annotation);
} else if (annType == QueryParam.class) {
processQueryParamAnnotation((QueryParam) annotation);
} else if (annType == DefaultValue.class) {
defaultValue = (DefaultValue) annotation;
} else if (annType.isAnnotationPresent(Qualifier.class)) {
processQualifierAnnotation(annType);
}
}
PersistenceAnnotationBeanPostProcessor.java 文件源码
项目:spring4-understanding
阅读 36
收藏 0
点赞 0
评论 0
public PersistenceElement(Member member, AnnotatedElement ae, PropertyDescriptor pd) {
super(member, pd);
PersistenceContext pc = ae.getAnnotation(PersistenceContext.class);
PersistenceUnit pu = ae.getAnnotation(PersistenceUnit.class);
Class<?> resourceType = EntityManager.class;
if (pc != null) {
if (pu != null) {
throw new IllegalStateException("Member may only be annotated with either " +
"@PersistenceContext or @PersistenceUnit, not both: " + member);
}
Properties properties = null;
PersistenceProperty[] pps = pc.properties();
if (!ObjectUtils.isEmpty(pps)) {
properties = new Properties();
for (PersistenceProperty pp : pps) {
properties.setProperty(pp.name(), pp.value());
}
}
this.unitName = pc.unitName();
this.type = pc.type();
this.synchronizedWithTransaction = (synchronizationAttribute == null ||
"SYNCHRONIZED".equals(ReflectionUtils.invokeMethod(synchronizationAttribute, pc).toString()));
this.properties = properties;
}
else {
resourceType = EntityManagerFactory.class;
this.unitName = pu.unitName();
}
checkResourceType(resourceType);
}
PersistenceInjectionTests.java 文件源码
项目:spring4-understanding
阅读 28
收藏 0
点赞 0
评论 0
@PersistenceContext(type = PersistenceContextType.EXTENDED)
public void setEntityManager(EntityManager em) {
if (this.em != null) {
throw new IllegalStateException("Already called");
}
this.em = em;
}
JpaModule.java 文件源码
项目:unitils
阅读 25
收藏 0
点赞 0
评论 0
/**
* Injects the currently active JPA <code>EntityManager</code> into all fields and methods that are
* annotated with <code>javax.persistence.PersistenceContext</code>
*
* @param testObject The test object, not null
*/
public void injectEntityManager(Object testObject, Object target) {
Set<Field> fields = getFieldsAnnotatedWith(target.getClass(), PersistenceContext.class);
Set<Method> methods = getMethodsAnnotatedWith(target.getClass(), PersistenceContext.class);
if (fields.isEmpty() && methods.isEmpty()) {
// Jump out to make sure that we don't try to instantiate the EntityManagerFactory
return;
}
EntityManager entityManager = getPersistenceContext(testObject);
setFieldAndSetterValue(target, fields, methods, entityManager);
}
JpaUnitRuleTest.java 文件源码
项目:jpa-unit
阅读 22
收藏 0
点赞 0
评论 0
@Test
public void testClassWithMultiplePersistenceContextFields() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar em1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em1");
em1Field.annotate(PersistenceContext.class);
final JFieldVar em2Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em2");
em2Field.annotate(PersistenceContext.class);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
try {
// WHEN
new JpaUnitRule(cut);
fail("IllegalArgumentException expected");
} catch (final IllegalArgumentException e) {
// THEN
assertThat(e.getMessage(), containsString("Only single field is allowed"));
}
}
JpaUnitRuleTest.java 文件源码
项目:jpa-unit
阅读 23
收藏 0
点赞 0
评论 0
@Test
public void testClassWithPersistenceContextAndPersistenceUnitFields() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
emf1Field.annotate(PersistenceContext.class);
final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf");
emf2Field.annotate(PersistenceUnit.class);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
try {
// WHEN
new JpaUnitRule(cut);
fail("IllegalArgumentException expected");
} catch (final IllegalArgumentException e) {
// THEN
assertThat(e.getMessage(), containsString("either @PersistenceUnit or @PersistenceContext"));
}
}
JpaUnitRuleTest.java 文件源码
项目:jpa-unit
阅读 30
收藏 0
点赞 0
评论 0
@Test
public void testClassWithPersistenceContextFieldOfWrongType() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "em");
emField.annotate(PersistenceContext.class);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
try {
// WHEN
new JpaUnitRule(cut);
fail("IllegalArgumentException expected");
} catch (final IllegalArgumentException e) {
// THEN
assertThat(e.getMessage(), containsString("annotated with @PersistenceContext is not of type EntityManager"));
}
}
JpaUnitRuleTest.java 文件源码
项目:jpa-unit
阅读 21
收藏 0
点赞 0
评论 0
@Test
public void testClassWithPersistenceContextWithoutUnitNameSpecified() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
emField.annotate(PersistenceContext.class);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
try {
// WHEN
new JpaUnitRule(cut);
fail("JpaUnitException expected");
} catch (final JpaUnitException e) {
// THEN
assertThat(e.getMessage(), containsString("No Persistence"));
}
}