Given the following JPA Entity:
@Table(name = "T_BOOK")
@Entity
@NamedQueries({
@NamedQuery(name = Book.FIND_BY_ISBN_QUERY.QUERY_NAME, query = Book.FIND_BY_ISBN_QUERY.QUERY_STRING)
})
public class Book extends BaseEntity {
public static class FIND_BY_ISBN_QUERY {
public static final String QUERY_NAME = "Book.findByISBN";
public static final String QUERY_STRING = "select new ch.bfh.eadj.dto.BookInfo(b.isbn, b.authors, b.title, b.price) from Book b where b.isbn = :isbn";
}
@Column(nullable = false)
private String isbn;
private String authors;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private BigDecimal price;
The schema generation creates the following create statement:
CREATE TABLE T_BOOK (NR BIGINT IDENTITY NOT NULL, AUTHORS VARCHAR, BINDING VARCHAR,ISBN VARCHAR NOT NULL, PRICE NUMERIC(38) NOT NULL, TITLE VARCHAR NOT NULL, VERSION INTEGER, PRIMARY KEY (NR))
So the price field looks like this: PRICE NUMERIC(38) NOT NULL
Why does EclipseLink
create a non-decimal nummeric value?
As soon as I add @Column(precision = 8, scale = 2)
to the price
field, it works as expected and creates PRICE NUMERIC(8,2) NOT NULL
.
Are the precision
and scale
attributes on BigDecimal
columns required when auto generating a schema?
The reason is that according to the JDBC Specification BigDecimal maps to numeric. Without specifying the scale the default scale is 0.
NUMERIC(38) (or in databases like oracle NUMBER(38)) is the max value for this data type in the database:
38 significant digits in the range of -(10**125) to +(10**125).