本文整理汇总了Java中javax.persistence.PersistenceUnit类的典型用法代码示例。如果您正苦于以下问题:Java PersistenceUnit类的具体用法?Java PersistenceUnit怎么用?Java PersistenceUnit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PersistenceUnit类属于javax.persistence包,在下文中一共展示了PersistenceUnit类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
/**
* @param args the command line arguments
*/
@PersistenceUnit
public static void main(String[] args) {
System.out.println("Creating entity information...");
EntityManager entityManager = Persistence.createEntityManagerFactory("DataAppLibraryPULocal").createEntityManager();
EntityTransaction et = entityManager.getTransaction();
et.begin();
loadDiscountRate(entityManager);
loadRegion(entityManager);
loadRole(entityManager);
loadTransmission(entityManager);
loadProductType(entityManager);
loadEngine(entityManager);
loadProduct(entityManager);
et.commit();
EntityManager specialEntityManager = new InitialLoadEntityManagerProxy(entityManager);
SalesSimulator simulator = new SalesSimulator(specialEntityManager);
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
cal.clear();
cal.set(year-1, 0, 1, 0, 0, 0); // go back to begining of year, 3 years ago
System.out.println("Creating historical data...");
System.out.println(" This may take 5 to 15min depending on machine speed.");
simulator.run(cal.getTime(), new Date());
entityManager.close();
}
示例2: decorateUnit
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
private <X> AnnotatedField<X> decorateUnit(AnnotatedField<X> field) {
final PersistenceUnit persistenceUnit = field.getAnnotation(PersistenceUnit.class);
final UniqueIdentifier identifier = UniqueIdentifierLitteral.random();
Set<Annotation> templateQualifiers = new HashSet<>();
templateQualifiers.add(ServiceLiteral.SERVICE);
if (hasUnitName(persistenceUnit)) {
templateQualifiers.add(new FilterLiteral("(osgi.unit.name=" + persistenceUnit.unitName() + ")"));
}
Bean<EntityManagerFactory> bean = manager.getExtension(OsgiExtension.class)
.globalDependency(EntityManagerFactory.class, templateQualifiers);
Set<Annotation> qualifiers = new HashSet<>();
qualifiers.add(identifier);
Bean<EntityManagerFactory> b = new SimpleBean<>(EntityManagerFactory.class, Dependent.class, Collections.singleton(EntityManagerFactory.class), qualifiers, () -> {
CreationalContext<EntityManagerFactory> context = manager.createCreationalContext(bean);
return (EntityManagerFactory) manager.getReference(bean, EntityManagerFactory.class, context);
});
beans.add(b);
Set<Annotation> fieldAnnotations = new HashSet<>();
fieldAnnotations.add(InjectLiteral.INJECT);
fieldAnnotations.add(identifier);
return new SyntheticAnnotatedField<>(field, fieldAnnotations);
}
示例3: inject
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
@Override
public void inject(Object result, Field field) {
try {
String unitName = field.getAnnotation(PersistenceUnit.class).unitName();
if (StringUtils.isEmpty(unitName))
unitName = PersistenceUnitProvider.DEFAULT_DS;
if(!field.getType().equals(EntityManagerFactory.class)){
log.error("Failed to create PersistenceUnit, type of field is not a EntityManagerFactory");
return;
}
EntityManagerFactory emf = provider.getProvider();
field.setAccessible(true);
field.set(result, emf);
} catch (Exception e) {
log.error("Failed to create PersistenceUnit", e);
}
}
示例4: setUp
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
public void setUp() throws OpenEJBException, NamingException, IOException {
//avoid linkage error on mac, only used for tests so don't need to add it in Core
JULLoggerFactory.class.getName();
final ConfigurationFactory config = new ConfigurationFactory();
final Assembler assembler = new Assembler();
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
final AppModule app = new AppModule(this.getClass().getClassLoader(), "test-app");
final Persistence persistence = new Persistence(new org.apache.openejb.jee.jpa.unit.PersistenceUnit("foo-unit"));
app.addPersistenceModule(new PersistenceModule("root", persistence));
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new StatelessBean(SuperBean.class));
app.getEjbModules().add(new EjbModule(ejbJar));
final ClientModule clientModule = new ClientModule(null, app.getClassLoader(), app.getJarLocation(), null, null);
clientModule.getLocalClients().add(this.getClass().getName());
app.getClientModules().add(clientModule);
assembler.createApplication(config.configureApplication(app));
}
示例5: retriveConnection
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
/**
* Retrieves and caches {@link Field}s with injection
*
* @param field
* @throws IOException
*/
private void retriveConnection(Field field) throws IOException {
PersistenceContext context = field.getAnnotation(PersistenceContext.class);
Resource resource = field.getAnnotation(Resource.class);
PersistenceUnit unit = field.getAnnotation(PersistenceUnit.class);
EJB ejbAnnot = field.getAnnotation(EJB.class);
if (ObjectUtils.notNull(context)) {
identifyConnections(context, field);
addAccessibleField(field);
} else if (ObjectUtils.notNull(resource)) {
metaData.setTransactionField(field);
addAccessibleField(field);
} else if (ObjectUtils.notNull(unit)) {
addUnitField(field);
addAccessibleField(field);
} else if (ObjectUtils.notNull(ejbAnnot)) {
// caches EJB annotated fields
cacheInjectFields(field);
addAccessibleField(field);
}
}
示例6: getPUAnnotatedMembersTest
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
@Test
public void getPUAnnotatedMembersTest() {
AnnotationScanner scanner = new AnnotationScanner();
List<AccessibleObject> members = scanner.getJpaAnnotatedMembers(TestClass.class, PersistenceUnit.class);
Assert.assertEquals(1, members.size());
AccessibleObject member = members.get(0);
Assert.assertEquals(Method.class, member.getClass());
Method method = (Method)member;
Assert.assertEquals("setEmf", method.getName());
}
示例7: getFactoryTest
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
/**
* When using a factory the class can be an interface. We need to make sure this does not cause a NPE
*/
@Test
public void getFactoryTest() {
AnnotationScanner scanner = new AnnotationScanner();
List<AccessibleObject> members = scanner.getJpaAnnotatedMembers(TestInterface.class, PersistenceUnit.class);
Assert.assertEquals(0, members.size());
}
示例8: processAnnotatedType
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
public <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> event, BeanManager manager) {
boolean hasPersistenceField = false;
for (AnnotatedField<? super T> field : event.getAnnotatedType().getFields()) {
if (field.isAnnotationPresent(PersistenceContext.class)
|| field.isAnnotationPresent(PersistenceUnit.class)) {
hasPersistenceField = true;
break;
}
}
if (hasPersistenceField) {
PersistenceAnnotatedType<T> pat = new PersistenceAnnotatedType<T>(manager, event.getAnnotatedType());
beans.addAll(pat.getProducers());
event.setAnnotatedType(pat);
}
}
示例9: PersistenceAnnotatedType
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
public PersistenceAnnotatedType(BeanManager manager, AnnotatedType<T> delegate) {
super(delegate);
this.manager = manager;
this.fields = new HashSet<>();
for (AnnotatedField<? super T> field : delegate.getFields()) {
if (field.isAnnotationPresent(PersistenceContext.class)) {
field = decorateContext(field);
} else if (field.isAnnotationPresent(PersistenceUnit.class)) {
field = decorateUnit(field);
}
this.fields.add(field);
}
}
示例10: PersistenceElement
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
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);
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:31,代码来源:PersistenceAnnotationBeanPostProcessor.java
示例11: setEmf
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
@PersistenceUnit
public void setEmf(EntityManagerFactory emf) {
if (this.emf != null) {
throw new IllegalStateException("Already called");
}
this.emf = emf;
}
示例12: injectEntityManagerFactory
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
/**
* Injects the JPA <code>EntityManagerFactory</code> into all fields and methods that are
* annotated with <code>javax.persistence.PersistenceUnit</code>
*
* @param testObject The test object, not null
*/
public void injectEntityManagerFactory(Object testObject, Object target) {
Set<Field> fields = getFieldsAnnotatedWith(target.getClass(), PersistenceUnit.class);
Set<Method> methods = getMethodsAnnotatedWith(target.getClass(), PersistenceUnit.class);
if (fields.isEmpty() && methods.isEmpty()) {
// Jump out to make sure that we don't try to instantiate the EntityManagerFactory
return;
}
EntityManagerFactory entityManagerFactory = getPersistenceUnit(testObject);
setFieldAndSetterValue(target, fields, methods, entityManagerFactory);
}
示例13: testClassWithMultiplePersistenceUnitFields
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
@Test
public void testClassWithMultiplePersistenceUnitFields() 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, EntityManagerFactory.class, "emf1");
emf1Field.annotate(PersistenceUnit.class);
final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf2");
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("Only single field is allowed"));
}
}
示例14: testClassWithPersistenceContextAndPersistenceUnitFields
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
@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"));
}
}
示例15: testClassWithPersistenceUnitFieldOfWrongType
import javax.persistence.PersistenceUnit; //导入依赖的package包/类
@Test
public void testClassWithPersistenceUnitFieldOfWrongType() 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, "emf");
emField.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("annotated with @PersistenceUnit is not of type EntityManagerFactory"));
}
}