felipeek
felipeek

Reputation: 1213

Incomplete @JoinColumns

I'm having a problem with JPA when trying to create some models to my database.

I have these three classes (I'll just put part of the code here):

GuideVersionLang

@Entity
public class GuideVersionLang implements LangItem {
     ...
     @ManyToOne
     @JoinColumns({
          @JoinColumn(name="GUIDE_VERSION_NUMBER", referencedColumnName="VERSION_NUMBER"),
          @JoinColumn(name="GUIDE_ID", referencedColumnName="GUIDE_ID")
     })
     @JsonIgnore
     private GuideVersion guideVersion;
     ...
}

GuideVersion

@Entity
@IdClass(value=GuideVersionKey.class)
public class GuideVersion {
     ...
     @OneToMany(mappedBy="guideVersion", orphanRemoval=true, cascade=CascadeType.PERSIST)
     private LangsCollection<GuideVersionLang> guideVersionLangs;

     @Id
     @ManyToOne
     @JoinColumn(nullable = false, name="GUIDE_ID")
     @JsonIgnore
     private Guides guide;

     @Id
     @Column(name = "VERSION_NUMBER")
     private long versionNumber;
     ...
}

And GuideVersionKey

@Embeddable
public class GuideVersionKey {
    private long versionNumber;
    private long guide;
    ...
}

So, I have a GuideVersion class and this class has a composite key. Its composite key is composed by the id of a Guide and a versionNumber, both long numbers.

I just want to make a relation between GuideVersion and GuideVersionLang, as you can see in the code. However, I'm having problems on the @JoinColumns annotation:

@JoinColumns({
        @JoinColumn(name="GUIDE_VERSION_NUMBER", referencedColumnName="VERSION_NUMBER"),
        @JoinColumn(name="GUIDE_ID", referencedColumnName="GUIDE_ID")
})

I don't know why but the @JoinColumns is not working. I'm getting this error:

The @JoinColumns on the annotated element [field guideVersion] from the entity class [class com.model.GuideVersionLang] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.

As you can see in the code, I am specifying both @Id columns inside the @JoinColumns annotation. What am I missing here?

Thank you VERY much!

Upvotes: 1

Views: 1311

Answers (1)

void
void

Reputation: 7890

There is some tips about the code in question:

1.as you have an embedded id for GuideVersion (GuideVersionKey ) so you really don't need to specify Ids for it (just use @EmbeddedId annoation).

2.you can map the Guid_id with @MapsId.

@Entity
public class GuideVersion {
     ...

     @EmbeddedId
     GuideVersionKey IdKey;

     @ManyToOne
     @MapsId("guide")
     @JoinColumn(name = "GUIDE_ID")
     private Guides guide;

     ...
}

Upvotes: 1

Related Questions