有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java@EmbeddedId和@Id异常

我有两个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映射有关。基本上,我不想成为一个实体(拥有id)。然而,当我尝试使用@Embeddeble annotation时,我得到了以下错误

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

我的课程分别是:

LostTravelerLocation:

@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;
    }
}

LostTraveler:

@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不是。提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    在实体上使用@Embeddable时,不能声明@Table注释,因为它会导致冲突。一方面你说它可以嵌入任何一个表,另一方面你说它有一个独立的表,JPA会抱怨。我注意到的另一件事是,您没有嵌入可嵌入的(我不知道@Embedded),而是使用@EmbeddedId,它主要用于复合密钥Id

    也许@Embeddable/@Embedded不适合您想要做的事情,尤其是因为您想要创建两个具有一对一映射的表。要么使用一对一映射,要么将LostTravelerLocation正确嵌入LostTraveler