Skip to content

[BUG] - OneToOne as primiary key #1070

Description

@const

Jimmer Version

0.9.90

JDK Version

JDK 21

Database

PostgreSQL

OS

Windows

Expected behavior

I have OneToOne relationship in SQL:

CREATE TABLE IF NOT EXISTS public.company (
	company_id uuid NOT NULL,
	description varchar(255) NULL,
	industry varchar(255) NULL,
	"name" varchar(255) NOT NULL,
	url varchar(255) NULL,
	CONSTRAINT company_pkey PRIMARY KEY (company_id)
);
CREATE INDEX IF NOT EXISTS company_by_name_idx ON public.company USING btree (name);
CREATE TABLE IF NOT EXISTS public.verification_info (
	company_id uuid NOT NULL,
	"comment" varchar(255) NULL,
	status varchar(255) NOT NULL,
	"timestamp" timestamptz(6) NOT NULL,
	username varchar(255) NOT NULL,
	CONSTRAINT verification_info_pkey PRIMARY KEY (company_id),
	CONSTRAINT verification_info_status_check CHECK (((status)::text = ANY ((ARRAY['VERIFIED'::character varying, 'INVALID'::character varying])::text[]))),
	CONSTRAINT fk2631d1desupjf5fo8mtgd7srv FOREIGN KEY (company_id) REFERENCES public.company(company_id)
);
CREATE INDEX IF NOT EXISTS verification_info_by_company_id_idx ON public.verification_info USING btree (company_id);

So, company might have at most one verification_info in related table. This is a sample, but reproduces situation in the real project. In the table verification_info the column company_id is both foreign and primary key.

The expected behavior is that something like the following will work:

@Entity
@Table(name = "company", schema = "public")
public interface CompanyEntity {
    @Id
    @Column(name = "company_id")
    UUID id();
    @Column(name = "name")
    String name();
    // skipped
    @Nullable
    String url();
    @Nullable
    String industry();
    @Nullable
    String description();

    @OneToOne(mappedBy = "company")
    @Nullable
    VerificationInfoEntity verificationInfo();
}
@Entity
@Table(name = "verification_info")
public interface VerificationInfoEntity {
    @Id
    @IdView
    @Column(name = "company_id")
    UUID companyId();
    @OneToOne
    @JoinColumn(name = "company_id")
    CompanyEntity company();
    @Column(name = "timestamp")
    Instant timestamp();
    @Column(name = "status")
    VerificationStatus status();
    @Column(name = "username")
    String username();
    @Nullable
    String comment();
}

Actual behavior

I receive the following error instead.

Illegal property "sample.model.jimmer.VerificationInfoEntity.companyId", it cannot be decorated by both 
@org.babyfish.jimmer.sql.Id and @org.babyfish.jimmer.sql.IdView

I actually tried several variants of modelling it, but none has worked.

Description

The situation where some column is both primary and foreign key is often used to model one-to-at-most-one relationships. We have quite a lot of such relationships in exiting databases. I have not found how to model such relationships using jimmer. If there is a way to do it, it should be added to the documentation.

Reproduction steps

Add classes above to the proejct and try to compile. It fails during compilation.

Generated SQL

None.

Relation Model

Specified above.

Screenshots

No response

Logs

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpendingPostpone plans because of more important tasks

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions