5 Replies Latest reply on Nov 2, 2012 5:41 AM by bmajsak

    Arquillian Persistency Extension: yaml and backreferences

    mjaskowski

      How to persist two entities that have references to each other?

       

      More formaly i would like to have such a setup in the database after applying a YAML file:

       

      A:

      - id: 2

      - b_id: 1

       

      B:

      - id: 1

         a_id: 2

       

      both A and B have foreign key constraints on b_id and a_id columns (those are nullable, though), so the above yaml file would be rejected by the database or am I missing something?

      I read that Yaml itself understands the notion of "reference" and one uses "&" and "*" characters to make it work but how should I use it in the case of Arquilllian Persistency?

       

      Great thanks for any help!

        • 1. Re: Arquillian Persistency Extension: yaml and backreferences
          bmajsak

          Hi Maciej,

           

          YAML files used by APE are plain DBunit DataSets meaning, they are db / row oriented (rdbms entities, not Java objects). You need to refer to other entries using foreign keys, as you would do in plain SQL. I hope I got your concern right

           

          Cheers,

          Bartosz

          • 2. Re: Arquillian Persistency Extension: yaml and backreferences
            mjaskowski

            Nope you didn't

             

            in plain sql i would:

             

            insert into A(id) values(1);

            insert into B(id, a_id) values(2,1);

            update table A where id = 1 set b_id = 2;

             

            given there are foreign keys between a_id and A and b_id and B that's the only way to this (unless you consider turning off the foreign keys alltogether)

             

            how can I do that using yml in APE?

            • 3. Re: Arquillian Persistency Extension: yaml and backreferences
              bmajsak

              Ok, got me

               

              Not sure if it's even possible with pure YAML, because it's more something like chicken-egg problem (not that it's not doable, only to describe how YAML parsers are working)

              I'm afraid you need to define the alias first before you reference to it, so something like this won't work

               

              A:

              - id: 2

                b_id: *b_id

               

              B:

              - id: &b_id 1

                a_id: 2

               

              On the other hand I can't really see how dbunit would process such data set, when it's anyway going row-by-row. Conclusion: turning off integrity check might be the only option if you want to use single data set.

              However, you can provide two data sets, one with raw data and second one with updated relationships. For this purpose you should define insert strategy as @SeedDataUsing(REFRESH) - this will update existing rows and insert those which are not existing yet (if any)

               

              HTH

              • 4. Re: Arquillian Persistency Extension: yaml and backreferences
                mjaskowski
                However, you can provide two data sets, one with raw data and second one with updated relationships.

                 

                That sounds like a solution.

                For this purpose you should define insert strategy as @SeedDataUsing(REFRESH) - this will update existing rows and insert those which are not existing yet (if any)

                Mmm... an internet search didn't help me find @SeedDataUsing annotation.

                 

                I had a look at the source code but I still wonder how to make it work? I mean: I need to have e.g.

                 

                @UsingDataSet("datasets/data.yml")

                @SeedDataUsing(DataSeedStrategy.DEFAULT)

                 

                @UsingDataSet("datasets/references.yml")

                @SeedDataUsing(DataSeedStrategy.REFRESH)

                 

                I can't do it on one test method, can I? It wouldn't look good to split it between class annotation and method annotation too, right? How to handle it then?

                • 5. Re: Arquillian Persistency Extension: yaml and backreferences
                  bmajsak

                  How about having such a construct?

                   

                  @UsingDataSet({ "datasets/data.yml", "datasets/references.yml" })

                  @SeedDataUsing(DataSeedStrategy.REFRESH)

                   

                  the order of data sets should be preserved.

                   

                  You can also put @SeedDataUsing on the class levelso it will become the default if not defined otherwise on the method level.

                   

                  HTH