1

Postgres supports xml as is datatype for a column. Hence I have a model that goes like this

@Entity
class StorageOfXml {
    @Id
    String id;

    @Lob
    @Column(columnDefinition="xml")
    String myXml;
}

When I try to persist the model using entity manager, it gives me an error

StorageOfXml s = new StorageOfXml()
s.setId("sample");
s.setMyXml("<foo><bar></bar></foo>");

entityManager.persist(s)

Error is

Internal Exception: org.postgresql.util.PSQLException: ERROR: column "myXml" is of type xml but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.

I notice that I need to cast the string to an xml format. However, I don't know where and how to cast it. How should I be able to persist and fetch this?

2
  • hey, I'm not using Hibernate and Oracle, so this is a pure JPA and Postgres thing Commented May 30, 2019 at 15:09
  • Did you ever find a good solution for this? Commented Sep 1, 2019 at 15:15

1 Answer 1

0

Without using hibernate or jaxb and if you really really want to save "myXml" as pg xml type, one solution would be to use XMLPARSE on a statement as the accepted answer in this post suggests: How can I insert an XML document in PostgreSQL in Java?

Sign up to request clarification or add additional context in comments.

2 Comments

Is there something with JAXB that can do this? I am open to JAXB solutions. anyway I used and saw that link already but I'm trying to keep away from creating prepared statements
@bumblebeen did you find a way to do this in the end?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.