1

Here is my postgres table

CREATE TABLE bid (
  id          SERIAL    NOT NULL,
  price       MONEY     NOT NULL,
  date        TIMESTAMP NOT NULL,
  customer_id INT       NOT NULL,
  item_id     INT       NOT NULL,
  CONSTRAINT bid_pk PRIMARY KEY (id)
);

I use BigDecimal to store the price in Java. But when I run my application I get error

org.apache.openjpa.lib.jdbc.ReportingSQLException: ERROR: column t2.buyoutprice does not exist

What is the best way to store PostgreSQL Money Type in Java?

barbara
  • 3,111
  • 6
  • 30
  • 58
  • The error indicates that you used the wrong name. Did you mean `price`? – keyser Dec 22 '14 at 23:55
  • @barbara Please search StackOverflow before posting. I have already posted [a thorough answer](http://stackoverflow.com/a/18170030/642706) on this topic. – Basil Bourque Dec 23 '14 at 08:18
  • 1
    Short answer: Don't use the money datatype in the DB. Use numeric. – Joe Love Dec 23 '14 at 16:03
  • @Joe Love Why? What is wrong with `Money` type? – barbara Dec 23 '14 at 16:17
  • The scale is the same across the entire DB. (What happens if you're using multiple currencies?) Not SQL standard. Including formatting in output from the DB can potentially cause issues when doing math outside of the DB. The PostgreSQL docs talk about potential loss of precision when casting unless done in 2 stages like so "SELECT '52093.89'::money::numeric::float8;" I could go on and on, but google can give you more help on this subject, and you should read the articles listed in the comments above. – Joe Love Dec 23 '14 at 16:54
  • `Money` is awful. Seriously. Use `NUMERIC` and the Java `BigInteger` / `BigDecimal` types to map it. Do not use `Money`. If you use `Money` you will have endless problems. The type should really be removed, but can't be for backward compatibility and because there are a few corner cases where it's potentially useful. – Craig Ringer Dec 23 '14 at 23:06

0 Answers0