关系的 PSQLException 列不存在
PSQLException column of relation does not exists
我尝试使用 Postman 将 json 等价于 class Album
保存到 Postgres。我在 Postgres 中为 Album
class 和 Album
class - Duration
的嵌入对象创建了两个实体。当我 post json 使用 Postman 时,IDE 抱怨:org.postgresql.util.PSQLException: ERROR: column "hours" of relation "album" does not exist
,这是真的,但是 hours
应该属于 duration
的字段 Album
不是 Album
本身。下面是我的代码。
Album
class:
package com.musesite.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonFormat;
@Entity
@Table(name="album")
public class Album {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="title")
private String title;
@Embedded
@Column(name="duration")
private Duration duration;
@Column(name="dateofrelease")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy", timezone="CET")
private Date dateOfRelease;
@Column(name="coverpath")
private String coverPath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public Date getDateOfRelease() {
return dateOfRelease;
}
public void setDateOfRelease(Date dateOfRelease) {
this.dateOfRelease = dateOfRelease;
}
public String getCoverPath() {
return coverPath;
}
public void setCoverPath(String coverPath) {
this.coverPath = coverPath;
}
}
Duration
class:
package com.musesite.model;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Table;
@Embeddable
@Table(name="duration")
public class Duration {
@Column(name="hours")
private long hours;
@Column(name="minutes")
private long minutes;
@Column(name="seconds")
private long seconds;
public long getHours() {
return hours;
}
public void setHours(long hours) {
this.hours = hours;
}
public long getMinutes() {
return minutes;
}
public void setMinutes(long minutes) {
this.minutes = minutes;
}
public long getSeconds() {
return seconds;
}
public void setSeconds(long seconds) {
this.seconds = seconds;
}
}
我如何在 Postgres 中创建 duration
table:
create table duration(
hours int,
minutes int,
seconds int
);
我如何在 Postgres 中创建 album
table:
create table album(
title text,
duration duration,
dateofrelease date,
coverpath text
);
JSON 我从 Postman 发送:
{
"title": "Deftones",
"duration": {"hours": 0, "minutes": 47, "seconds": 14 },
"dateOfRelease": "20/05/2003",
"coverPath": "https://upload.wikimedia.org/wikipedia/en/9/91/Deftones-selftitled_albumcover.jpg"
}
你实际上只需要 1 album
table.
@Embedded
注释会将专辑 table 的 hours, minutes, seconds
转换为 java 一侧的 Duration
class,但持续时间不必存在在数据库中
create table album (
id bigint primary key, -- this field was missing
title text,
dateofrelease date,
coverpath text,
hours int,
minutes int,
seconds int
);
我尝试使用 Postman 将 json 等价于 class Album
保存到 Postgres。我在 Postgres 中为 Album
class 和 Album
class - Duration
的嵌入对象创建了两个实体。当我 post json 使用 Postman 时,IDE 抱怨:org.postgresql.util.PSQLException: ERROR: column "hours" of relation "album" does not exist
,这是真的,但是 hours
应该属于 duration
的字段 Album
不是 Album
本身。下面是我的代码。
Album
class:
package com.musesite.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonFormat;
@Entity
@Table(name="album")
public class Album {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="title")
private String title;
@Embedded
@Column(name="duration")
private Duration duration;
@Column(name="dateofrelease")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy", timezone="CET")
private Date dateOfRelease;
@Column(name="coverpath")
private String coverPath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public Date getDateOfRelease() {
return dateOfRelease;
}
public void setDateOfRelease(Date dateOfRelease) {
this.dateOfRelease = dateOfRelease;
}
public String getCoverPath() {
return coverPath;
}
public void setCoverPath(String coverPath) {
this.coverPath = coverPath;
}
}
Duration
class:
package com.musesite.model;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Table;
@Embeddable
@Table(name="duration")
public class Duration {
@Column(name="hours")
private long hours;
@Column(name="minutes")
private long minutes;
@Column(name="seconds")
private long seconds;
public long getHours() {
return hours;
}
public void setHours(long hours) {
this.hours = hours;
}
public long getMinutes() {
return minutes;
}
public void setMinutes(long minutes) {
this.minutes = minutes;
}
public long getSeconds() {
return seconds;
}
public void setSeconds(long seconds) {
this.seconds = seconds;
}
}
我如何在 Postgres 中创建 duration
table:
create table duration(
hours int,
minutes int,
seconds int
);
我如何在 Postgres 中创建 album
table:
create table album(
title text,
duration duration,
dateofrelease date,
coverpath text
);
JSON 我从 Postman 发送:
{
"title": "Deftones",
"duration": {"hours": 0, "minutes": 47, "seconds": 14 },
"dateOfRelease": "20/05/2003",
"coverPath": "https://upload.wikimedia.org/wikipedia/en/9/91/Deftones-selftitled_albumcover.jpg"
}
你实际上只需要 1 album
table.
@Embedded
注释会将专辑 table 的 hours, minutes, seconds
转换为 java 一侧的 Duration
class,但持续时间不必存在在数据库中
create table album (
id bigint primary key, -- this field was missing
title text,
dateofrelease date,
coverpath text,
hours int,
minutes int,
seconds int
);