0

I am trying to nest classes with @Idclass and i am getting an error on the 3rd level.

so what I have:

@Entity 
public class Foo{

@Id
private Integer FooNumber;

private boolean available;
public Foo(){}
}

first level - BarRepository works

@Entity
@IdClass(Bar.BarId.class)
public class Bar{

@Id
private Integer barNumber;

@Id
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
private Foo foo;

public Bar(){}

//removed getters setters

static class BarId  implements Serializable {

        private Integer barNumber;
        private Integer foo;

        public BarId() {
           //JPA
        } //removed getters setters
    }}

second level @IdClass - first level nested @IdClass - BarRepo works fine

@Entity
@IdClass(WorksId.class)
public class Works{

@Id
private Integer worksNumber;

@Id
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
private Bar bar;

private boolean available;

public Works() {
    //JPA
}

static class WorksId implements Serializable {

        private Integer worksNumber;

        private Bar.BarId bar;

        public WorksId() {
            //JPA
        }}

third - second level nested @IdClass - fails ;(

@Entity
@IdClass(Nope.NopeId.class)
public class Nope{

@Id
private Integer nopeNumber;

@Id
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
private Works works;

private int weight;

public Nope() {
    //JPA
}
static class NopeId implements Serializable {

    private Integer nopeNumber;

    private Works.WorksId level;

    public NopeId() {
        //JPA
    }}

Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Integer' to required type 'com.tvh.parts.warehouse.wb3.storing.domain.Bar$BarId' for property 'bar': no matching editors or conversion strategy found

Sven Dhaens
  • 1,916
  • 1
  • 15
  • 10
  • Why would you even nest `@IdClass` (so deeply)? – XtremeBaumer Apr 30 '18 at 14:35
  • Because I want to use business keys, and normalising my entitys i came up with this structure.... I can do a workaround if I add @GeneratedValue keys to the tables, but then I am not using my business keys.. and JPA needs equals and hashcode so they would be hijacking equals and hashcode methods. ? – Sven Dhaens Apr 30 '18 at 15:18
  • Why do so many people revert to @GeneratedValue so much...? it feels like the easy thing to do but that doesnt mean it is the best way no? – Sven Dhaens Apr 30 '18 at 15:29
  • Its easier and for more info you can [read this question](https://stackoverflow.com/questions/63090/surrogate-vs-natural-business-keys). Regarding your concern for `hashCode` and `equals`, you don't have to take every property into them. You can still only use the parts that you consider relevant and skip the generated id completly. Also you can still "use" your business keys with a simple `unique` constraint. From my point of view, it would be much easier to use generated values in your case, because 3 levels are too much – XtremeBaumer Apr 30 '18 at 18:49
  • 1
    Ok, so I am working with generated id's now. I am implementing equals and hashcode.... for example Bar.class....now my next problem... this means I need to take into account the Foo.class into the equals aswell. since it is part of the business key....this means each equals will need to retrieve Foo.class aswell ?? n way round that I guess? – Sven Dhaens May 02 '18 at 16:02

0 Answers0