Skip to content

Declare the dependency between shared session creator and entity instantiator. #2099

New issue

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

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

Already on GitHub? # to your account

Closed
steffen-harbich-cognitum opened this issue Jan 5, 2021 · 8 comments
Assignees
Labels
type: bug A general bug

Comments

@steffen-harbich-cognitum
Copy link

steffen-harbich-cognitum commented Jan 5, 2021

I have a database migration script component as in the following code:

@Component
public class MigrationLogic {

    // + autowired repositories

    @PostConstruct
    @Transactional
    public void run() {
        // running migration logic, storing entities that rely on converter registered to ConversionService
    }
}

Produces the NullPointerException:

Caused by: java.lang.NullPointerException: null
at org.neo4j.ogm.typeconversion.ProxyAttributeConverter.toGraphProperty(ProxyAttributeConverter.java:56) ~[neo4j-ogm-core-3.2.18.jar:3.2.18]
at org.neo4j.ogm.metadata.FieldInfo.readProperty(FieldInfo.java:492) ~[neo4j-ogm-core-3.2.18.jar:3.2.18]

This is because Neo4jOgmEntityInstantiatorConfigurationBean has not been initialized at that point. I would expect it to be initialized once the repositories are available as spring bean. I need to run the migration script before tomcat embedded is opening REST API ports, so I cannot use an event like "context refreshed" to do the migration.

Workaround: Add

    @Autowired()
    @Qualifier("org.springframework.data.neo4j.repository.config.Neo4jOgmEntityInstantiatorConfigurationBean#0")
    private Object workaround;

which is of course an unwanted hack.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 5, 2021
@michael-simons michael-simons self-assigned this Jan 5, 2021
@michael-simons
Copy link
Collaborator

You removed the order, right? And the issue still persists… I'll have a look.

@steffen-harbich-cognitum
Copy link
Author

Yes, I am struggling with the order of initialization of spring beans. @order was not working.

@michael-simons
Copy link
Collaborator

Could you please post an example of your converter logic?

@steffen-harbich-cognitum
Copy link
Author

See this example reproduction: repro_converion_npe.zip

Start the test, then it fails. Uncomment the workaround lines in MyMigration component it and runs green. Hope that helps.

@michael-simons
Copy link
Collaborator

Yeah, I can work with that.

Btw, I hope you don't have that base class in production:

@NodeEntity
public abstract class EntityBase {

    @Id
    @GeneratedValue
    private Long graphId;

    @Id
    @GeneratedValue(strategy = UuidStrategy.class)
    private String uuid;
}

This is 100% guaranteed to cause pain somewhere. You are basically defining two ids here.

@michael-simons
Copy link
Collaborator

What that work for you?

@Component
public class MigrationLogic implements ApplicationListener<ContextRefreshedEvent> {

    private @Autowired ThingRepository userRepository;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        // Do your migration
    }
}

Otherwise I would like to pull in @mp911de here. I have no idea why the thing is picked up too late.

@michael-simons michael-simons added bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 6, 2021
michael-simons added a commit that referenced this issue Jan 6, 2021
…ntity instantiator.

The conversion service is picked up to late as the the dependency
between the shared session creator and the bean that does the post
processing of the underlying session factory is undefined for the Spring
context.

By explicit stating this in the repository extension, the
`Neo4jOgmEntityInstantiatorConfigurationBean` is correctly initialized
upfront without messing around with an actual post processor or
orderings.

This closes #2099.
@steffen-harbich-cognitum
Copy link
Author

As stated in the initial comment, I cannot use ContextRefreshedEvent because it's too late.

Regarding the two IDs: I need the UUID as usual ID. The graph ID is needed because I do some manual wiring of configured (not known at compile time) relations when quering and the query is returning org.neo4j.ogm.model.Edge which is based on graph IDs but not on user-defined IDs. For now, there were no troubles with this but if you have a better solution in mind, please tell me :)

Thanks for looking into the issue.

@michael-simons michael-simons added this to the 5.2.13 (Moore SR13) milestone Jan 12, 2021
michael-simons added a commit that referenced this issue Jan 14, 2021
…ntity instantiator.

The conversion service is picked up to late as the the dependency
between the shared session creator and the bean that does the post
processing of the underlying session factory is undefined for the Spring
context.

By explicit stating this in the repository extension, the
`Neo4jOgmEntityInstantiatorConfigurationBean` is correctly initialized
upfront without messing around with an actual post processor or
orderings.

This closes #2099.
@michael-simons michael-simons changed the title Conversion service picked up too late Declare the dependency between shared session creator and entity instantiator. Jan 14, 2021
@michael-simons
Copy link
Collaborator

This is fixed and will be included in the next patch releases in February. Thank you for your contribution, @steffen-harbich-cognitum

michael-simons added a commit that referenced this issue Jan 14, 2021
…ntity instantiator.

The conversion service is picked up to late as the the dependency
between the shared session creator and the bean that does the post
processing of the underlying session factory is undefined for the Spring
context.

By explicit stating this in the repository extension, the
`Neo4jOgmEntityInstantiatorConfigurationBean` is correctly initialized
upfront without messing around with an actual post processor or
orderings.

This closes #2099.
@michael-simons michael-simons added type: bug A general bug and removed bug labels Apr 20, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants