gpt4 book ai didi

java - 在联结表中使用复合主键时,Hibernate 未正确映射对象 ("Bad value for type"异常)

转载 作者:行者123 更新时间:2023-12-01 14:31:37 26 4
gpt4 key购买 nike

我收到异常 o.h.e.j.s.SqlExceptionHelper | int 类型的错误值:9dac4fd2-a04c-4be7-976b-d880a43ea25a。这里好像是想把一个UUID放到一个Integer字段里。

我有以下表格,诚然它们在复合键方面有点复杂:

CREATE TABLE public.event (
id uuid NOT NULL,
...
CONSTRAINT event_pkey PRIMARY KEY (id)
);

CREATE TABLE public.condition_set (
api_id uuid NOT NULL,
version integer NOT NULL,
...,
CONSTRAINT condition_set_pkey PRIMARY KEY (api_id, version)
);

CREATE TABLE public.condition_set_event (
condition_set_api_id uuid NOT NULL,
condition_set_version integer NOT NULL,
event_id uuid NOT NULL,
CONSTRAINT condition_set_event_pkey PRIMARY KEY (condition_set_api_id, condition_set_version, event_id),
CONSTRAINT fk_condition_set FOREIGN KEY (condition_set_api_id, condition_set_version) REFERENCES public.condition_set(api_id, version) ON DELETE CASCADE,
CONSTRAINT fk_event FOREIGN KEY (event_id) REFERENCES public.event(id) ON DELETE CASCADE
);

在我的模型中,我有一个非常简单的 Event 类。 ConditionSet类有一个匹配数据库结构的复合主键,如下:

@Entity
public class ConditionSet {

@EmbeddedId
private ConditionSetId id;
}

看起来像:

@Embeddable
public class ConditionSetId implements Serializable {

private static final long serialVersionUID = 8110138933878596476L;

private UUID apiId;
private Integer version;

}

棘手的部分是 ConditionSetEvent 联结表,它也由一个复合键组成,其中一个是 ConditionSet 的复合键

@Entity
public class ConditionSetEvent {

@EmbeddedId
private ConditionSetEventId id;

@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@MapsId("conditionSetId")
@JoinColumns(foreignKey = @ForeignKey(name = "fk_condition_set"), value = {
@JoinColumn(nullable = false, name = "conditionSetApiId"),
@JoinColumn(nullable = false, name = "conditionSetVersion")
})
private ConditionSet conditionSet;

@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@MapsId("eventId")
@JoinColumn(foreignKey = @ForeignKey(name = "fk_event"))
private Event event;

public ConditionSetEvent(ConditionSet conditionSet, Event event) {
this.conditionSet = conditionSet;
this.event = event;
this.id = new ConditionSetEventId(conditionSet.getId(), event.getId());
}
}

及其 EmbeddedId:

@Embeddable
public class ConditionSetEventId implements Serializable {

private static final long serialVersionUID = -6269791751266804667L;

private ConditionSetId conditionSetId;
private UUID eventId;
}

但是,如果我尝试使用此存储库方法查询此联结表:

public interface ConditionSetEventRepository extends JpaRepository<ConditionSetEvent, ConditionSetEventId> {

@Query("select cse from ConditionSetEvent cse where cse.id.eventId = :eventId")
List<ConditionSetEvent> findByEventId(UUID eventId);
}

然后我得到上面提到的错误(异常中的 uuid 是一个有效的 ConditionSet.apiId,但不知何故似乎被重新使用了。

使用跟踪记录:

DEBUG | org.hibernate.SQL | select conditions0_.condition_set_api_id as conditio0_8_, conditions0_.event_id as event_id1_8_, conditions0_.condition_set_api_id as conditio2_8_, conditions0_.condition_set_version as conditio3_8_ from condition_set_event conditions0_ where conditions0_.event_id=?
TRACE | o.h.t.d.sql.BasicBinder | binding parameter [1] as [OTHER] - [be1ec45d-6533-4e77-98b7-f9a357cda052]
TRACE | o.h.t.d.s.BasicExtractor | extracted value ([conditio0_8_] : [OTHER]) - [9dac4fd2-a04c-4be7-976b-d880a43ea25a]
WARN | o.h.e.j.s.SqlExceptionHelper | SQL Error: 0, SQLState: 22003
ERROR | o.h.e.j.s.SqlExceptionHelper | Bad value for type int : 9dac4fd2-a04c-4be7-976b-d880a43ea25a

所以它最初确实设法提取了 UUID 值(最后一行跟踪),但在下一步(对于 Integer)它仍然尝试使用 UUID 而不是 Integer。

我是不是做错了什么?

最佳答案

我认为您不需要 @JoinColumns 并且它弄乱了哪个 id 列映射到 ConditionSetEventId 中的哪个 id 字段

    @Entity
public class ConditionSetEvent {

@EmbeddedId
private ConditionSetEventId id;

@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@MapsId("conditionSetId")
private ConditionSet conditionSet;
....

关于java - 在联结表中使用复合主键时,Hibernate 未正确映射对象 ("Bad value for type"异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63231507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com