英文:
Spring Boot JPA returns empty list for findAll() of entity with composite key
问题
I'm having a hard time getting data from the Postgres DB schema which looks like:
CREATE TABLE public.mm_enterprise (
systemcode varchar(64) NOT NULL,
lid varchar(255) NOT NULL,
euid int8 NOT NULL,
CONSTRAINT pk_enterprise PRIMARY KEY (systemcode, lid, euid)
);
CREATE INDEX mm_enterprise1 ON public.mm_enterprise USING btree (euid);
and entity for that:
@Entity
@Table(name = "mm_enterprise")
public class EUID {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "lid", nullable = false)
private String lid;
@Column(name = "systemcode", nullable = false)
private String systemcode;
@Column(name = "euid", nullable = false)
private Integer euid;
public EUID() {
}
public String getSystemcode() {
return systemcode;
}
public void setSystemcode(String systemcode) {
this.systemcode = systemcode;
}
public Integer getEuid() {
return euid;
}
public void setEuid(Integer euid) {
this.euid = euid;
}
public String getLid() {
return lid;
}
public void setLid(String lid) {
this.lid = lid;
}
}
Repository for findAll()
looks like
@Repository
public interface EUIDRepository extends JpaRepository<EUID, String> {
List<EUID> findAll();
}
but when I will run it the list will come empty. Hibernate is set to validate as well output SQL, which looks like
Hibernate:
select
euid0_.lid as lid1_0_,
euid0_.euid as euid2_0_,
euid0_.systemcode as systemco3_0_
from
mm_enterprise euid0_
and that query works fine when I will execute it directly on the DB.
Any thoughts on what I have skipped? Postgres version is 13.8
PS.
It looks like that lid
can't be annotated as @Id
for some reason. DB is a 3rd-party DB so I'm not able to add id SERIAL
to that table.
英文:
I'm having a hard time getting data from the Postgres DB schema which looks like:
CREATE TABLE public.mm_enterprise (
systemcode varchar(64) NOT NULL,
lid varchar(255) NOT NULL,
euid int8 NOT NULL,
CONSTRAINT pk_enterprise PRIMARY KEY (systemcode, lid, euid)
);
CREATE INDEX mm_enterprise1 ON public.mm_enterprise USING btree (euid);
and entity for that:
@Entity
@Table(name = "mm_enterprise")
public class EUID {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "lid", nullable = false)
private String lid;
@Column(name = "systemcode", nullable = false)
private String systemcode;
@Column(name = "euid", nullable = false)
private Integer euid;
public EUID() {
}
public String getSystemcode() {
return systemcode;
}
public void setSystemcode(String systemcode) {
this.systemcode = systemcode;
}
public Integer getEuid() {
return euid;
}
public void setEuid(Integer euid) {
this.euid = euid;
}
public String getLid() {
return lid;
}
public void setLid(String lid) {
this.lid = lid;
}
}
Repository for findAll()
looks like
@Repository
public interface EUIDRepository extends JpaRepository<EUID, String> {
List<EUID> findAll();
}
but when I will run it the list will come empty. Hibernate is set to validate as well output SQL, which looks like
Hibernate:
select
euid0_.lid as lid1_0_,
euid0_.euid as euid2_0_,
euid0_.systemcode as systemco3_0_
from
mm_enterprise euid0_
and that query works fine when I will execute it directly on the DB.
Any thoughts on what I have skipped? Postgres version is 13.8
PS.
It looks like that lid
can't be annotated as @Id
for some reason. DB is a 3rd-party DB so I'm not able to add id SERIAL
to that table.
答案1
得分: 3
如@Mar-Z在评论中提到的,有两种方法可以正确获取它。
#1 使用@IdClass
(首选)
创建ID类如下:
@Data
@NoArgsConstructor
@EqualsAndHashCode
public class SystemcodeLidEuidId implements Serializable {
private static final long serialVersionUID = -909206262878526790L;
private String lid;
private String systemcode;
private Long euid;
}
然后创建实体类如下:
@Entity
@Table(name = "mm_enterprise")
@AllArgsConstructor
@NoArgsConstructor
@IdClass(SystemcodeLidEuidId.class)
public class EUID implements Serializable {
private static final long serialVersionUID = -909206262878526790L;
@Id
@Column(name = "lid")
private String lid;
@Id
@Column(name = "systemcode")
private String systemcode;
@Id
@Column(name = "euid")
private Long euid;
}
#2 使用@Embeddable
和@EmbeddedId
创建嵌入式ID类如下:
@Data
@Embeddable
public class OrderEntryPK implements Serializable {
private String lid;
private String systemcode;
}
然后创建实体类如下:
@Data
@Entity
@Table(name = "mm_enterprise")
public class EUID {
@EmbeddedId
private OrderEntryPK id;
@Column(name = "euid", nullable = false)
private Long euid;
}
英文:
As @Mar-Z mentioned in the comments there are 2 ways to get it the right way.
#1 Using @IdClass
(preferred)
Create ID class as
@Data
@NoArgsConstructor
@EqualsAndHashCode
public class SystemcodeLidEuidId implements Serializable {
private static final long serialVersionUID = -909206262878526790L;
private String lid;
private String systemcode;
private Long euid;
}
and then entity as
@Entity
@Table(name = "mm_enterprise")
@AllArgsConstructor
@NoArgsConstructor
@IdClass(SystemcodeLidEuidId.class)
public class EUID implements Serializable {
private static final long serialVersionUID = -909206262878526790L;
@Id
@Column(name = "lid")
private String lid;
@Id
@Column(name = "systemcode")
private String systemcode;
@Id
@Column(name = "euid")
private Long euid;
}
#2 Using @@Embeddable
and @EmbeddedId
@Data
@Embeddable
public class OrderEntryPK implements Serializable {
private String lid;
private String systemcode;
}
and an entity
@Data
@Entity
@Table(name = "mm_enterprise")
public class EUID {
@EmbeddedId
private OrderEntryPK id;
@Column(name = "euid", nullable = false)
private Long euid;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论