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

Problem reading Shadows with EmbeddedId #886

Closed
tkuprevich opened this issue Sep 24, 2019 · 12 comments
Closed

Problem reading Shadows with EmbeddedId #886

tkuprevich opened this issue Sep 24, 2019 · 12 comments

Comments

@tkuprevich
Copy link

@tkuprevich tkuprevich commented Sep 24, 2019

I'm using javers 5.7.2 and have a problem reading nested collection of AgreementMember objects in Agreement. It's a regilar OneToMany relation except AgreementMember has a composite primary key. Here are the classes:

Agreement.java:

@TypeName("Agreement")
public class Agreement
{
    @Id
    private UUID agreementId;

    private UUID locationId;

    @OneToMany(mappedBy = "agreement", cascade = CascadeType.ALL, orphanRemoval = true)
    @ShallowReference
    private List<AgreementMember> agreementMembers;

    //other fields ...
}

AgreementMember.java: with AgreementMemberId as primary key:

@TypeName("AgreementMember")
public class AgreementMember
{
    public AgreementMemberId getId()
    {
        return agreementMemberId;
    }


    @Embeddable
    @TypeName("AgreementMemberId")
    public static class AgreementMemberId implements Serializable
    {
        private UUID agreementId;
        private UUID memberId;
    }

    @EmbeddedId
    private AgreementMemberId agreementMemberId;

    //other fields ...
}

Here is a snapshot

[
   Snapshot   {
      commit:1.0,
      id:Agreement/a3ffda86-ba8b-4640-bd03-4ad945aa2546,
      version:1,
      (agreementId:a3ffda86-ba8b-4640-bd03-4ad945aa2546,
      agreementMembers:[
         AgreementMember/a3ffda86-ba8b-4640-bd03-4ad945aa2546,
         50527728-0f56-4403-8651-7f9eff2dda93
      ],
      locationId:1b1e5b47-ba10-4bb7-9f04-6b81d1c66422)
   }
]

And here is an exception when I'm trying to read shadows:

org.javers.shadow.ShadowGraphBuilder#assembleShallowReferenceShadow java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at path $.agreementMemberId

And here is my test:

@Test
void testAgreement()
{
    //given
    Javers javers = JaversBuilder.javers()
            .withListCompareAlgorithm(ListCompareAlgorithm.SIMPLE)
            .build()

    Agreement agreement = new Agreement()
    agreement.agreementId = UUID.randomUUID()
    agreement.locationId = UUID.randomUUID()

    AgreementMember.AgreementMemberId agreementMemberId = new AgreementMember.AgreementMemberId()
    agreementMemberId.agreementId = agreement.agreementId
    agreementMemberId.memberId = UUID.randomUUID()

    AgreementMember agreementMember = new AgreementMember()
    agreementMember.agreementMemberId = agreementMemberId

    List<AgreementMember> agreementMemberList = new ArrayList<>()
    agreementMemberList.add(agreementMember)
    agreement.agreementMembers = agreementMemberList

    javers.commit("Agreement", agreement)

    JqlQuery query = QueryBuilder.byInstanceId(agreement.agreementId, Agreement.class).build()
    List<Shadow<Agreement>> shadows = javers.findShadows(query)
    println shadows
}

Any help is greatly appreciated. This might me misconfiguration or something, please advice.

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Sep 24, 2019

please push your case to github as a failing test case (see CONTRIBUTING.md)

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Sep 25, 2019

@itisha
Copy link
Contributor

@itisha itisha commented Sep 25, 2019

I removed my post at stackoverflow and added failing case here
#889

Is that what I was supposed to do?

@itisha
Copy link
Contributor

@itisha itisha commented Sep 25, 2019

@bartoszwalacik should I reopen the case or do anything else? I'm still having the issue(
The PR failed with exact same exception that we get in our app

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Sep 25, 2019

ok, now I can look on this test case, stay tuned

@vsafronau
Copy link

@vsafronau vsafronau commented Sep 27, 2019

any progress on this? We had the same issue. thx

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Sep 27, 2019

nope, this is an OSS project, if you want to speed up things, contribute a PR with the fix

@vsafronau
Copy link

@vsafronau vsafronau commented Sep 27, 2019

thx

@tkuprevich
Copy link
Author

@tkuprevich tkuprevich commented Sep 30, 2019

This has been resolved by registering a custom toString() method

    @Data
    @Embeddable
    @TypeName( "AgreementMemberId" )
    public static class AgreementMemberId implements Serializable, JaversToString
    {
        @Column private UUID agreementId;

        @Column private UUID memberId;

        @Override
        public String javersToString()
        {
            return agreementId.toString() + ',' + memberId.toString();
        }
    }

and registering piece:

javers().registerValueWithCustomToString( JaversToString.class, JaversToString::javersToString )

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Oct 23, 2019

bug report confirmed

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Oct 23, 2019

as a quick workaround, you can remove @ShallowReference or map Id class (AgreementMemberId) as Value and not as ValueObject.

bartoszwalacik added a commit that referenced this issue Oct 26, 2019
bartoszwalacik added a commit that referenced this issue Nov 2, 2019
@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Nov 2, 2019

fixed in 5.8.3

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

Successfully merging a pull request may close this issue.

None yet
4 participants