I have an entity B
which contains a map of entities <VC, P>
in which some of the fields in P
, e.g. A
is not being linked with my join table and gives the error:
PSQLException: ERROR: column pricing1_.pricing_a does not exist
I am trying to make it such that when I persist my main entity, B
, that all of the entities in my map will also be persisted as well (if possible) all in one go.
This error occurs both when I do
bRepo.save(b);
and
pRepo.saveAll(b.getPricing().values()); // by here the values at least exists in its own table (p)
bRepo.save(b);
here is what I have
Main entity B
@Setter
@Getter
@Entity
@Table(name = "b")
public class B implements Serializable {
@Id
@Column(nullable = false)
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "b_p",
joinColumns = @JoinColumn(name = "b_name", referencedColumnName = "name"))
@MapKeyJoinColumns({
@MapKeyJoinColumn(name = "p_c"),
@MapKeyJoinColumn(name = "c_id")
})
private Map<VC, P> pricing = new LinkedHashMap<>();
...
}
The pricing
maps key
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "v_c")
public class VC implements Serializable {
@EmbeddedId private VCId vcId;
}
and its (VC) composite key
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class VCId implements Serializable {
@Enumerated(EnumType.STRING)
@Column(name = "p_c")
private PC pC;
@Column(name = "c_id")
private String cId;
}
the pricing map's value
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
@Entity
@Table(name = "p")
public class P implements Serializable {
@EmbeddedId private PId pId;
}
and its (P) key
@Setter
@Getter
@NoArgsConstructor
@Embeddable
public class PId implements Serializable {
@Column(name = "a")
private BigDecimal a; // complains about this field
@Column(name = "d_a")
private BigDecimal dA; // and will probably complain about this one too
}
My tables
CREATE TABLE b
(
name VARCHAR(100) NOT NULL PRIMARY KEY,
...
);
CREATE TABLE v_c
(
p_c TEXT NOT NULL,
c_id VARCHAR(50) NOT NULL,
PRIMARY KEY (p_c, c_id)
);
CREATE TABLE p
(
a NUMERIC NOT NULL,
d_a NUMERIC NOT NULL DEFAULT 0.0,
PRIMARY KEY (a, d_a)
);
CREATE TABLE b_p
(
b_name VARCHAR(100) NOT NULL,
p_c TEXT NOT NULL,
c_id VARCHAR(50) NOT NULL,
a NUMERIC NOT NULL,
d_a NUMERIC NOT NULL DEFAULT 0.0,
PRIMARY KEY (b_name, p_c, c_id),
FOREIGN KEY (b_name) REFERENCES b (name) ON DELETE CASCADE,
FOREIGN KEY (p_c, c_id) REFERENCES v_c (p_c, c_id) ON DELETE CASCADE,
FOREIGN KEY (a, d_a) REFERENCES p (a, d_a) ON DELETE CASCADE
);
What am I doing wrong?
CodePudding user response:
In the end I made the following changes and it worked:
Replaced the composite ids for p
and v_c
with auto increment ids AND added another new, auto increment field called pricing_p_id
in my b_p
table:
CREATE TABLE v_c
(
vc_id BIGSERIAL NOT NULL PRIMARY KEY,
p_c TEXT NOT NULL,
coin_id VARCHAR(50) NOT NULL
);
CREATE TABLE p
(
p_id BIGSERIAL NOT NULL PRIMARY KEY,
a NUMERIC NOT NULL,
d_a NUMERIC NOT NULL DEFAULT 0.0,
vc_id BIGSERIAL,
FOREIGN KEY (vc_id) REFERENCES v_c(vc_id) ON DELETE CASCADE
);
CREATE TABLE b_p
(
b_name VARCHAR(100) NOT NULL,
vc_id BIGSERIAL NOT NULL,
pricing_p_id BIGSERIAL NOT NULL,
PRIMARY KEY (b_name, vc_id, pricing_p_id),
FOREIGN KEY (b_name) REFERENCES b (name) ON DELETE CASCADE,
FOREIGN KEY (vc_id) REFERENCES v_c (vc_id) ON DELETE CASCADE,
FOREIGN KEY (pricing_p_id) REFERENCES p (p_id) ON DELETE CASCADE
);
and then updated the mapping for the pricing field to only look like this:
@OneToMany(cascade = CascadeType.ALL)
@MapKeyJoinColumn(name = "vc_id") // this
private Map<VC, P> pricing = new LinkedHashMap<>();