Search in sources :

Example 1 with HibernateSessionSource

use of org.apache.tapestry5.hibernate.HibernateSessionSource in project tapestry-5 by apache.

the class HibernateSessionSourceImplTest method startup_without_packages.

@Test
public void startup_without_packages() {
    Collection<String> packageNames = CollectionFactory.newList("org.example.myapp.entities", "org.example.app0.entities");
    HibernateEntityPackageManager packageManager = newMock(HibernateEntityPackageManager.class);
    TestBase.expect(packageManager.getPackageNames()).andReturn(packageNames);
    ClasspathScannerImpl scanner = new ClasspathScannerImpl(new ClasspathURLConverterImpl());
    ClassNameLocatorImpl classNameLocator = new ClassNameLocatorImpl(scanner);
    List<HibernateConfigurer> filters = Arrays.asList(new DefaultHibernateConfigurer(true), new PackageNameHibernateConfigurer(packageManager, classNameLocator));
    replay();
    HibernateSessionSource source = new HibernateSessionSourceImpl(log, filters);
    Session session = source.create();
    Assert.assertNotNull(session);
    // make sure it found the entity in the package
    ClassMetadata meta = session.getSessionFactory().getClassMetadata(User.class);
    Assert.assertEquals(meta.getEntityName(), "org.example.app0.entities.User");
    verify();
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) HibernateSessionSourceImpl(org.apache.tapestry5.hibernate.internal.HibernateSessionSourceImpl) PackageNameHibernateConfigurer(org.apache.tapestry5.hibernate.internal.PackageNameHibernateConfigurer) DefaultHibernateConfigurer(org.apache.tapestry5.hibernate.internal.DefaultHibernateConfigurer) HibernateSessionSource(org.apache.tapestry5.hibernate.HibernateSessionSource) ClasspathScannerImpl(org.apache.tapestry5.ioc.internal.services.ClasspathScannerImpl) DefaultHibernateConfigurer(org.apache.tapestry5.hibernate.internal.DefaultHibernateConfigurer) PackageNameHibernateConfigurer(org.apache.tapestry5.hibernate.internal.PackageNameHibernateConfigurer) HibernateConfigurer(org.apache.tapestry5.hibernate.HibernateConfigurer) ClasspathURLConverterImpl(org.apache.tapestry5.ioc.internal.services.ClasspathURLConverterImpl) ClassNameLocatorImpl(org.apache.tapestry5.ioc.internal.services.ClassNameLocatorImpl) HibernateEntityPackageManager(org.apache.tapestry5.hibernate.HibernateEntityPackageManager) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 2 with HibernateSessionSource

use of org.apache.tapestry5.hibernate.HibernateSessionSource in project tapestry-5 by apache.

the class HibernateModule method contributeValueEncoderSource.

/**
 * Contributes {@link ValueEncoderFactory}s for all registered Hibernate entity classes. Encoding and decoding are
 * based on the id property value of the entity using type coercion. Hence, if the id can be coerced to a String and
 * back then the entity can be coerced.
 */
@SuppressWarnings("unchecked")
public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration, @Symbol(HibernateSymbols.PROVIDE_ENTITY_VALUE_ENCODERS) boolean provideEncoders, final HibernateSessionSource sessionSource, final Session session, final TypeCoercer typeCoercer, final PropertyAccess propertyAccess, final LoggerSource loggerSource) {
    if (!provideEncoders)
        return;
    Set<EntityType<?>> entities = sessionSource.getSessionFactory().getMetamodel().getEntities();
    for (EntityType<?> entityType : entities) {
        Class<?> entityClass = entityType.getJavaType();
        if (entityClass != null) {
            SingularAttribute<?, ?> id = entityType.getId(entityType.getIdType().getJavaType());
            final String idenfierPropertyName = id.getName();
            ValueEncoderFactory factory = new ValueEncoderFactory() {

                @Override
                public ValueEncoder create(Class type) {
                    return new HibernateEntityValueEncoder(entityClass, idenfierPropertyName, session, propertyAccess, typeCoercer, loggerSource.getLogger(entityClass));
                }
            };
            configuration.add(entityClass, factory);
        }
    }
}
Also used : EntityType(javax.persistence.metamodel.EntityType) HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory)

Example 3 with HibernateSessionSource

use of org.apache.tapestry5.hibernate.HibernateSessionSource in project tapestry-5 by apache.

the class HibernateSessionSourceImplTest method get_configuration.

@Test
public void get_configuration() {
    HibernateConfigurer configurer = new HibernateConfigurer() {

        @Override
        public void configure(Configuration configuration) {
            configuration.setProperty("foo", "bar");
            configuration.configure();
        }
    };
    HibernateSessionSource source = new HibernateSessionSourceImpl(log, Arrays.asList(configurer));
    Configuration config = source.getConfiguration();
    Assert.assertNotNull(config);
    Assert.assertEquals("bar", config.getProperty("foo"));
// Configuration was immutable in 5.1, but Hibernate 3.6.0.Final made that impossible
}
Also used : HibernateSessionSource(org.apache.tapestry5.hibernate.HibernateSessionSource) HibernateSessionSourceImpl(org.apache.tapestry5.hibernate.internal.HibernateSessionSourceImpl) Configuration(org.hibernate.cfg.Configuration) DefaultHibernateConfigurer(org.apache.tapestry5.hibernate.internal.DefaultHibernateConfigurer) PackageNameHibernateConfigurer(org.apache.tapestry5.hibernate.internal.PackageNameHibernateConfigurer) HibernateConfigurer(org.apache.tapestry5.hibernate.HibernateConfigurer) Test(org.testng.annotations.Test)

Aggregations

HibernateConfigurer (org.apache.tapestry5.hibernate.HibernateConfigurer)2 HibernateSessionSource (org.apache.tapestry5.hibernate.HibernateSessionSource)2 DefaultHibernateConfigurer (org.apache.tapestry5.hibernate.internal.DefaultHibernateConfigurer)2 HibernateSessionSourceImpl (org.apache.tapestry5.hibernate.internal.HibernateSessionSourceImpl)2 PackageNameHibernateConfigurer (org.apache.tapestry5.hibernate.internal.PackageNameHibernateConfigurer)2 Test (org.testng.annotations.Test)2 EntityType (javax.persistence.metamodel.EntityType)1 HibernateEntityPackageManager (org.apache.tapestry5.hibernate.HibernateEntityPackageManager)1 HibernateEntityValueEncoder (org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder)1 ClassNameLocatorImpl (org.apache.tapestry5.ioc.internal.services.ClassNameLocatorImpl)1 ClasspathScannerImpl (org.apache.tapestry5.ioc.internal.services.ClasspathScannerImpl)1 ClasspathURLConverterImpl (org.apache.tapestry5.ioc.internal.services.ClasspathURLConverterImpl)1 ValueEncoderFactory (org.apache.tapestry5.services.ValueEncoderFactory)1 Session (org.hibernate.Session)1 Configuration (org.hibernate.cfg.Configuration)1 ClassMetadata (org.hibernate.metadata.ClassMetadata)1