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

Override JpaContentTemplate #19

Closed
brunnels opened this issue Mar 5, 2018 · 9 comments
Closed

Override JpaContentTemplate #19

brunnels opened this issue Mar 5, 2018 · 9 comments

Comments

@brunnels
Copy link

brunnels commented Mar 5, 2018

I'm trying to use a sql server backend which doesn't work with the sql in the JpaContentTemplate. What is the best way to override the bean that is created in JpaStoresRegistrar? Would be great if spring boot autoconfig looked for an existing bean. Setting my bean as @primary and giving it the same name is also not working.

@paulcwarren
Copy link
Owner

Hi @brunnels. Thanks for raising the issue. That doesn't surprise me. The current JPA module is a little bit of a POC. However, we are wrapping up a major revision that takes the same approach as Spring Batch with platform-specific .SQL files and Spring Batch auto-configuration that picks and runs the right set of .SQL files.

This sounds pretty close/almost exactly what u need. Would u be willing to test/contribute .SQL files for SQL Server?

@brunnels
Copy link
Author

brunnels commented Mar 6, 2018

Sure. Just point me at the branch and I can send a merge request.

@paulcwarren
Copy link
Owner

Great. Give me a day or two to pull it together and push the latest of the feature branch and I'll ping you here. Thanks

@paulcwarren
Copy link
Owner

paulcwarren commented Mar 12, 2018

Hey @brunnels I have pushed a "work in progress" on the wip/jpa-resource branch. It isn't particularly clean (in terms of test-driven) but it has been manually tested against mysql and postgres.

The way the JPA module is now intended to work is the same as Spring Batch (another Spring library that requires schema initialization). When using the JPA module the developer will have to supply the DataSource bean (as he always has) and also a DataSourceInitializer bean to initialize the given DataSource with the relevant Spring Content schema. In this case the DataSourceInitializer would look like this:-

    @Value("/org/springframework/content/jpa/schema-drop-postgresql.sql")
    private Resource dropReopsitoryTables;

    @Value("/org/springframework/content/jpa/schema-postgresql.sql")
    private Resource dataReopsitorySchema;

    @Bean
    public DataSource dataSource() {
        ...
        return ds;
    }

    @Bean
    DataSourceInitializer datasourceInitializer() {
        ResourceDatabasePopulator databasePopulator =
                new ResourceDatabasePopulator();

        databasePopulator.addScript(dropReopsitoryTables);
        databasePopulator.addScript(dataReopsitorySchema);
        databasePopulator.setIgnoreFailedDrops(true);

        DataSourceInitializer initializer = new DataSourceInitializer();
        initializer.setDataSource(dataSource());
        initializer.setDatabasePopulator(databasePopulator);

        return initializer;
    }

entity transaction manager beans ommitted for brevity

I have (manually) tested this using the example/integration project spring-eg-content-jpa by hacking up the configuration to provide the new datasourecinitializer bean.

Point to note that when using the boot starter for Spring Content for JPA this datasource initialization will eventually be auto-configured. This is still to be done BTW so don't expect it to work.

I also refactored the central DefaultJPAStoreImpl class to use a new (Blob)Resource abstraction based on the standard Spring Resource interface (this is actually part of a broader experimental Store API piece of work). This is where the relevant SQL will be executed as content is set, updated and unset (see AbstractBlobResource). There is a possibility that the hardcoded SQL statements wont work on SQLServer and if that is the case we should first take a look at whether we have done anything non-standard in the SQL, or not. Possibly the case. But I am also finding that the various drivers don't support the full compliment of JDBC API and we may be forced to have database-specific blob resource implementations. In a previous incantation of this changed I had actually paved the way for this so I left that code in just in case we need it. See DelegatingBlobResourceLoader and AbstractBblobResource. Feel free to use it if you feel that you need to. However, if we don't end up using it then we can decide later on whether to leave it in, or rip it out. This can of plug-in capability can often useful and can get someone of out trouble in a pinch. But maybe it is more hassle than it is worth.

So I think you can use this to test against SQLServer by adding a the relevant .sql files and adding the DataSourceInitializer bean to your project then set, updating and unsettting content. feel free to post back here with anything that you do find.

In the meantime, I will clean this all up, go find various free hosting options to perform accepting testing against and add the spring boot starter auto-configuration.

@paulcwarren
Copy link
Owner

Hi @brunnels I just committed changes to support sql server on wip/jpa-resource. Our integration tests work but aren't committed yet). Please feel free to give this branch a go but I will try and wrap this up and merge it across to master soonest whereby it will be available in the latest snapshot.

@brunnels
Copy link
Author

brunnels commented Mar 16, 2018 via email

@paulcwarren
Copy link
Owner

No problem. I know how it is. When you get a chance let me know how it goes.

@paulcwarren
Copy link
Owner

I rebased this work onto master this evening so it is available on the latest 0.0.10-SNAPSHOT. Might make it a bit easier to consume for you.

@paulcwarren
Copy link
Owner

This fix went out in 0.0.10 (released today). I am going to close this but feel free to re-open or open any new issues should you run into anything. We are running our standard acceptance tests against SQL Server so I think the basics should be there for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants