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

Sometimes JPA Stream doesn't load properties with relationships #182

Closed
doardin opened this issue Sep 28, 2021 · 5 comments
Closed

Sometimes JPA Stream doesn't load properties with relationships #182

doardin opened this issue Sep 28, 2021 · 5 comments
Assignees
Labels
bug Something isn't working
Projects
Milestone

Comments

@doardin
Copy link

doardin commented Sep 28, 2021

My Account class:

public class Account {
    @Id
    private String id;
    
    private String owner;

    @OneToOne
	@JoinColumn(name = "statusId")
    private Status status;
}

My AccountStatus class:

public class Status {   		
	@Id
	private String id = UUID.randomUUID().toString().toUpperCase();

	private String name;

	public Status(String name){
		this.name = name;
	}
}

My query with JPA

public Account findAccount(String owner){
        Account account = this.streamer.stream(Account.class).filter(
            Account$.owner.equal(owner)
        ).findFirst().orElseThrow(AccountNotFound::new);
        if (account.getStatus().getName().equalsIgnoreCase("CLOSED"))
            throw new AccountNotFound();

        return account;
}

Sometimes the AccountStatus wasn't load and returning error. I fixed this bug changing JPAStream query to JPQL

@minborg
Copy link
Contributor

minborg commented Sep 28, 2021

Could you provide more information? For example, under which conditions the Status is not loaded. If it is not loaded, is null returned instead? Can you provide the logs of SQL. Did you also try joining the tables (e.g. using StreamConfiguration?

@doardin
Copy link
Author

doardin commented Sep 30, 2021

SQL:
select account0_.id as account_3_0_, account0_.status_id as account_9_0_, account0_.owner as owner6_0_ from account account0_ where account0_.owner=?

It was hard to debug because it's rare for the account's status object to return null, but sometimes it did return. I did not try joining the tables

And the account status returned null for the same account, and when I went to check the database, the status was filled.

@julgus julgus added the bug Something isn't working label May 31, 2023
@julgus julgus added this to the 3.0.2 milestone May 31, 2023
@julgus julgus added this to To do in 3.0.4 May 31, 2023
@julgus
Copy link
Member

julgus commented Jun 5, 2023

Hi, I would love to take a closer look at this issue. However, it isn't easy to reproduce without your database and code example. Are you able to provide any more information?

Generally, I recommend performing a join when in need of information from another table. In this case that would look like this:

StreamConfiguration sc = StreamConfiguration.of(Account.class).joining(Account$.status); 
Account account = this.streamer.stream(sc)
       .filter(Account$.owner.equal(owner))
       .findFirst().orElseThrow(AccountNotFound::new);

@julgus julgus moved this from To do to In Progress in 3.0.4 Jun 5, 2023
@julgus julgus self-assigned this Jun 5, 2023
@doardin
Copy link
Author

doardin commented Jun 16, 2023

I tried to reproduce the error here and was unsuccessful, but what I noticed was that even marking the property with fetch type lazy, the property was still loaded.

@julgus julgus modified the milestones: 3.0.2, 3.0.3 Jun 19, 2023
@julgus
Copy link
Member

julgus commented Jun 26, 2023

Thanks for the update, as both of us failed to reproduce the issue I will close it for now.

Regarding the fetching, that is a question for the underlying JPA provider. JPAStreamer does not interact with the database directly (only via the JPA provider) and thus cannot control how the data is fetched.

@julgus julgus closed this as completed Jun 26, 2023
@julgus julgus moved this from In Progress to Done in 3.0.4 Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
3.0.4
Done
Development

No branches or pull requests

3 participants