Composite key on table where primary key is also foreign key?

Viewed 23

I have table item_image:

create table item_image
(
    item_id bigint        not null
        constraint item_image_pk
            primary key
        constraint item_image_item_fk
            references item,
    location  varchar(1000) not null,
    path      varchar(1000) not null,
    file_name varchar(200)  not null
);

And mapped it to entity:

@Entity
@Table(name = "item_image", schema = "public")
public class ItemImageEntity implements Serializable {

    @Id
    @OneToOne
    @JoinColumn(name = "item_id")
    private ItemEntity item;

    private String location;
    private String path;
    private String fileName;
    
    // getters and setters
}

My item entity has:

@OneToOne(mappedBy = "item")
private ItemImageEntity profileImage;

in it, the postgress table does not contain refference to this item_image, only vice versa.

When i try to run the app i get:

@EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: This class [ItemImageEntity] does not define an IdClass

Why do i need to define IdClass and How? Since i have only 1 primary key.

Thanks for help!

1 Answers
  1. It's issue that you want to add primary key of entity without real primary key, hibernate can't identify itemEntity. (Do you have item_id field?)
  2. About @IdClass - Hibernate can't identify class as field and think that you can use inner fields as primary key.
Related