Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HibernateDomainExporter does not register spatial types #2320

Closed
ridoo opened this issue Jun 18, 2018 · 3 comments · Fixed by #2713
Closed

HibernateDomainExporter does not register spatial types #2320

ridoo opened this issue Jun 18, 2018 · 3 comments · Fixed by #2713
Assignees
Labels
Projects
Milestone

Comments

@ridoo
Copy link
Contributor

ridoo commented Jun 18, 2018

tl;dr

Is it possible to either

  • configure the Maven plugin generating code from a set of hbm.xml files
  • configure the JPADomainExporter to register spatial types

I use version 4.1.3.

Context

Section 2.1.4. Generating the model from hbm.xml files describes a way to generate code from XML mapping files. Having a simple Spring Boot Application:

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    @Qualifier("entityManagerFactory")
    private EntityManagerFactory entityManagerFactory;

    @Bean
    @Primary
    public SessionFactory sessionFactory() {
        if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
            throw new NullPointerException("factory is not a hibernate factory");
        }
        return entityManagerFactory.unwrap(SessionFactory.class);
    }

    @Bean
    public EntityManagerFactory entityManagerFactory(DataSource datasource, JpaProperties properties)
            throws IOException {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        emf.setJpaPropertyMap(properties.getProperties());
        emf.setDataSource(datasource);
        emf.afterPropertiesSet();
        return emf.getNativeEntityManagerFactory();
    }

    @Override
    public void run(String... args) throws Exception {
        createExporter("Q").execute();
    }

    private JPADomainExporter createExporter(String prefix) {
        final Metamodel metamodel = entityManagerFactory.getMetamodel();
        return new JPADomainExporter(prefix, new File("src/main/java"), metamodel);
    }

}

I first tried to use HibernateDomainExporter but the above config also worked out for JPADomainExporter. The mapping files are declared within a META-INF/persistence.xml on the classpath, like so:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">
    <persistence-unit name="default-unit">
        <description>Default Mappings</description>
        <mapping-file>/hbm/mapping-resource_1.hbm.xml</mapping-file>
        <mapping-file>/hbm/mapping-resource_2.hbm.xml</mapping-file>

        <!-- et cetera --> 

    </persistence-unit>
</persistence>

This works rather well. However, I do have spatial types which are not mapped by default. The documentation hints me to add <spatial>true</spatial> to the Maven plugin config, but I did not find a way to configure the maven codegen plugin to work with hbm.xml mappings.

@ridoo
Copy link
Contributor Author

ridoo commented Jun 18, 2018

I opened a PR which makes it possible to register spatial paths in a non-Maven run.

@Shredder121
Copy link
Member

I saw it, thanks, first glance looks good!
Yes if you want it to be used in Querydsl 4, then keeping the old class is needed.

ridoo added a commit to 52North/sensorweb-server-db-model that referenced this issue Jun 18, 2018
This includes a (hopefully) temporary workaround how querydsl
registers spatial path types, i.e. customized versions of

  - AbstractDomainExporter

  - JPADomainExporter

  - SpatialSupport

are used to register needed types. There is a PR open which
discusses the enablement registering such paths from non-apt
use cases.

See:
  - querydsl/querydsl#2320
  - querydsl/querydsl#2322
@ridoo
Copy link
Contributor Author

ridoo commented Jun 19, 2018

I've restored the old SpatialSupport now delegating to SpatialTypeMapper which encapsulates the actual type registration. I shamelessly marked SpatialSupport as @Deprecated, so feel free to revert if you have reasons to not doing so. I am not aware if you have any rules when to deprecate something or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
5.x
  
Awaiting triage
3 participants