I am trying to use a @Lob
column with a Java String
type to map its content to TEXT
inside Postgres. Here is the relevant entity:
@Entity(name="metadata")
public class Metadata {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "created_on")
@ColumnDefault(value="CURRENT_TIMESTAMP")
@Generated(GenerationTime.INSERT)
private LocalDateTime createdOn;
@Lob
@Column(name = "content")
private String content;
@Column(name = "draft")
private Boolean draft;
@OneToMany(cascade = javax.persistence.CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "metadata")
private List<Attachment> attachments;
public void addAttachment(Attachment attachment) {
if (attachments == null) {
attachments = new ArrayList<>();
}
attachments.add(attachment);
attachment.setMetadata(this);
}
// getters and setters
}
I have code which creates a new Metadata
entity based on use input. I verify manually in IntelliJ debug mode that this entity has the content
set to its intended value (which happens to be a JSON string). However, when I check Postgres after running the code, I see this data:
my_db=> select * from metadata;
id | content | created_on | draft
----+---------+-------------------------+-------
1 | 49289 | 2021-04-26 14:21:25.733 | t
(1 row)
Note carefully that the strange values 49289
is appearing where we would expect to see a JSON string. Note that I also verified from the command line that the correct table is what was created:
CREATE TABLE scarfon_attachment (
id bigint NOT NULL,
contents text,
filename character varying(255),
scarfon_id bigint NOT NULL
);
All the other columns in the entity/table are working as expected. What could be the problem with the @Lob
annotation. For reference, I am running a fairly old version of Postgres (9.2), but it is not that ancient.
text
column is not a "Lob" – Bulldog@String
tell the driver to instruct Postgres to useTEXT
? Forgive me, you certainly know more about this than I do. – Szegedtext
column can be used in exactly the same way as avarchar
column. So yes, JPA should treat it correctly – Bulldog