Reputation: 45
I try to save json equivalent to class Album
to Postgres using Postman. I have created two entities for Album
class and embedded object of Album
class - Duration
in Postgres. When I post json using Postman, IDE complains: org.postgresql.util.PSQLException: ERROR: column "hours" of relation "album" does not exist
, which is true, but hours
should belong to field duration
of Album
not Album
itself. Below is my code.
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;
}
}
How I created duration
table in Postgres:
create table duration(
hours int,
minutes int,
seconds int
);
How I created album
table in Postgres:
create table album(
title text,
duration duration,
dateofrelease date,
coverpath text
);
JSON I send from 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"
}
Upvotes: 1
Views: 1019
Reputation: 1625
You actually need only 1 album
table.
The @Embedded
annotation will convert the hours, minutes, seconds
of the album table to a Duration
class on java side, but duration does not have to exist in the database
create table album (
id bigint primary key, -- this field was missing
title text,
dateofrelease date,
coverpath text,
hours int,
minutes int,
seconds int
);
Upvotes: 2