1 year ago

#376607

test-img

Chaitan Yadav

Any way to do one-to-one association by non primary key in spring data

I have a situation where two entities are to be joined on non-primary key column. I have following situation:

@Embeddable
Entity EmbeddedKey{
    private String apple;
    private String banana;
    private int cat;
}
@Entity
public class ParentEntity{

    @EmbeddedId
    private EmbeddedKey embeddedKey;

    @Column(name = "dog")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger dog;

    @OneToOne(mappedBy = "parentEntity")
    @JoinColumn(name = "dog", referencedColumnName = "dog")
    private ChildEntity childEntitty;
}
@Entity
public class ChildEntity{

    @OneToOne
    @PrimaryKeyJoinColumn(name = "dog", referencedColumnName = "dog")
    private ParentEntity parentEntity;

    @Id
    @Column(name = "dog")
    private BigInteger dog;
}

When I deploy code, i get

Caused by: org.hibernate.MappingException: broken column mapping for: ParentEntity.id of: x.y.z.ChildEntity. 

As per me, hibernate is trying to join primary key of parent table with primary-key of child table which my design doesn't want. column dog is unique but not primary key due to unavoidable reason.

jpa

spring-data-jpa

foreign-keys

one-to-one

0 Answers

Your Answer

Accepted video resources