gpt4 book ai didi

java - @EmbeddedId 和 @Id 异常

转载 作者:行者123 更新时间:2023-12-02 03:24:38 24 4
gpt4 key购买 nike

我有两个 SQL 表,如下:

CREATE TABLE lost_travelers
(
id BIGINT PRIMARY KEY DEFAULT nextval('global_seq'),
/* a lot of other columns */
);

CREATE TABLE lost_travelers_locations
(
lost_traveler_id BIGINT NOT NULL,
latitude REAL NOT NULL,
longitude REAL NOT NULL,
location_type VARCHAR NOT NULL,

FOREIGN KEY (lost_traveler_id) REFERENCES travelers (id) ON DELETE CASCADE
);

我希望它位于单独的表中的原因是因为 lost_travelers 表确实有很多属性。

我遇到的问题与 JPA/Hibernate 映射有关。基本上,我不希望 lost_travelers_locations 成为一个实体(有 id)。但是,当我尝试使用 @Embeddable 注释时,出现以下错误。

Caused by: org.hibernate.AnnotationException: model.location.LostTravelerLocation must not have @Id properties when used as an @EmbeddedId: model.traveler.LostTraveler.lostTravelerLocation

我的类(class)分别是:

迷失旅行者位置:

@Embeddable
@Table(name = "lost_travelers_locations")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class LostTravelerLocation extends Location
{
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "lost_traveler_id")
private LostTraveler lostTraveler;

@Enumerated(EnumType.STRING)
@Column(name = "location_type")
private LocationType locationType;

public LostTraveler getLostTraveler()
{
return lostTraveler;
}

public void setLostTraveler(LostTraveler lostTraveler)
{
this.lostTraveler = lostTraveler;
}

public LocationType getLocationType()
{
return locationType;
}

public void setLocationType(LocationType locationType)
{
this.locationType = locationType;
}
}

位置类别:

@MappedSuperclass
public abstract class Location
{
@Column(name = "latitude")
@NotNull
private float longitude;

@Column(name = "longitude")
@NotNull
private float latitude;

public float getLongitude()
{
return longitude;
}

public void setLongitude(float longitude)
{
this.longitude = longitude;
}

public float getLatitude()
{
return latitude;
}

public void setLatitude(float latitude)
{
this.latitude = latitude;
}
}

迷失的旅行者:

@Entity
@Table(name = "lost_travelers")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class LostTraveler extends Traveler
{
@EmbeddedId
private LostTravelerLocation lostTravelerLocation;

/* A lot of other properties */

public LostTravelerLocation getLostTravelerLocation()
{
return lostTravelerLocation;
}

public void setLostTravelerLocation(LostTravelerLocation lostTravelerLocation)
{
this.lostTravelerLocation = lostTravelerLocation;
}

}

抽象类旅行者:

@MappedSuperclass
public abstract class Traveler extends EntityWithId
{
/* A lot of properties as well */
}

EntityWithId:

@MappedSuperclass
public class EntityWithId
{
@Id
@SequenceGenerator(name = "global_seq", sequenceName = "global_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "global_seq")
private Long id;

public Long getId()
{
return id;
}

public void setId(Long id)
{
this.id = id;
}
}

我现在不知道问题是什么。我只是坚持 LostTraveler 是一个实体,而 LostTravelerLocation 不是。提前致谢。

最佳答案

在实体上使用 @Embeddable 时,不能声明 @Table 注解,因为它会导致冲突。一方面你说这可以嵌入到任何表中,另一方面你说它有一个独立的表,JPA 会提示。我注意到的另一件事是,您没有嵌入可嵌入的(我没有看到 @Embedded ),而是使用 @EmbeddedId ,这主要是用于复合键 ID。

也许 @Embeddable/@Embedded 不是适合您想要执行的方法,特别是因为您想要创建两个具有一对一关系的表一个映射。使用一对一映射或将 LostTravelerLocation 正确嵌入到 LostTraveler

关于java - @EmbeddedId 和 @Id 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56912854/

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