0

I have a question for my project in Spring Data REST.

My model includes two tables with EmbeddedIds. The first table (name=B) consist of two integers. The second table (name=A) consist of a simple FK and the model of B (includes the EmbeddedId).

Now, if I make a request for table B, I'll get the two IDs. However, if I make a request for table A, I wont get the IDs..

So I overrid the toString()-method in my EmbeddedId-class, to return at least the IDs right in the URI-link.

I read about BackendIdConverter or Spring core.converter and tried to convert the IDs right, but I wasn't able to reach my goal (got errors). So now, I need your help!

To fully understand my problem, here's my structure (as demo):

@Embeddable
public class IDsFromA implements Serializable {

    @ManyToOne
    @JoinColumn(name="cID")
    private C c;

    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="b_1", referencedColumnName="b_1"),
        @JoinColumn(name="b_2", referencedColumnName="b_2")
    })
    private B b;
}

@Embeddable
public class IDsFromB implements Serializable {

    private int b_1;
    private int b_2;
}

@Entity
public class A {

    @EmbeddedId
    private IDsFromA idsFromA;

    // ...
}

@Entity
public class B {

    @EmbeddedId
    private IDsFromA idsFromA;

    // ...
}

@Entity
public class c {

    @Id
    private int cID;

    // ...
}
Phil
  • 87
  • 6

1 Answers1

0

The Jackson JSON serializer will by default also serialize any public get..() methods. So you can simply add a couple of methods to return the relevant data and the values should be in the response:

e.g.

@Entity
public class A {

    @EmbeddedId
    private IDsFromA idsFromA;

    public int getValueOne(){
        return idsFromA.getB().getIdsFromB().getB_1();
    }

     public int getValueTwo(){
        return idsFromA.getB().getIdsFromB().getB_2();
    }
}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • 1
    I solved the problem a few hours ago by setting the BackendIdConverter right. But after hiding the converters and trying with your option, I noticed, that the IDs already are shown so I can't test your answer right now, but will do it in another project. But thanks for your answer! – Phil Dec 20 '16 at 07:54
  • @Phil, can you add your answer? – Rafael Gomes Francisco Apr 12 '17 at 12:29
  • I will push it to a github-project in approx. 5-6 hours. That's ok? – Phil Apr 13 '17 at 09:03