gpt4 book ai didi

java - 关系的 PSQLException 列不存在

转载 作者:行者123 更新时间:2023-11-29 12:49:43 26 4
gpt4 key购买 nike

我尝试使用 Postman 将等同于类 Album 的 json 保存到 Postgres。我在 Postgres 中为 Album 类和 Album 类的嵌入对象创建了两个实体 - Duration。当我使用 Postman 发布 json 时,IDE 提示:org.postgresql.util.PSQLException: ERROR: column "hours"of relation "album"does not exist,这是真的,但是 hours 应该属于 Album 的字段 duration 而不是 Album 本身。下面是我的代码。

专辑类:

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

持续时间类:

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 表:

create table duration(
hours int,
minutes int,
seconds int
);

我如何在 Postgres 中创建 album 表:

create table album(
title text,
duration duration,
dateofrelease date,
coverpath text
);

我从 Postman 发送的 JSON:

{
"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 表。
@Embedded注解会将专辑表的时分秒转换为java端的Duration类,但是duration没有存在于数据库中

create table album (
id bigint primary key, -- this field was missing
title text,
dateofrelease date,
coverpath text,
hours int,
minutes int,
seconds int
);

关于java - 关系的 PSQLException 列不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57202284/

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