Use PostgreSQL types and types in Java

After creating an instance of EntityManagerFactory, an error message appears:

... Exception Description: Predeployment of PersistenceUnit [aPU] failed. Internal Exception: Exception [EclipseLink-7157] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.ValidationException Exception Description: Entity class [class Table] must use a @JoinColumn instead of @Column to map its relationship attribute [Price]. ... 

The column price is the domain type (example: CREATE TYPE MONEY AS NUMERIC(10,2) FINAL ).

How to use Domain or Struct PostgreSQL types? Is this possible with JPA?

+4
source share
1 answer

The problem is not that the column type is at least not what JPA is complaining about right now, the problem is that the JPA does not know how to map the Price type, which is not one of the main supported type or object.

2.1.1 Constant fields and properties

Permanent fields or properties an organization can have the following types: Java primitive types; java.lang.String ; other Java serializable types (including wrappers of primitive types, java.math.BigInteger java.math.BigDecimal java.util.Date java.util.Calendar java.math.BigInteger , java.math.BigInteger , java.math.BigInteger , user-defined serializable types, byte[] , byte[] , char[] and Character[] ; enumerations; organization types and / or collections of entity types; and inline classes (see section 2.1.5).

With standard JPA, try using the Embeddable and Embedded annotations:

 @Embeddable public class Price { private BigDecimal amount; ... } 

and then in your organization:

 @Embedded @AttributeOverrides({ @AttributeOverride(name="amount", column=@Column (name="AMOUNT")) }) public Price getPrice() { ... } 

Another option is to use TransformationMapping (specific to EclipseLink).

References

  • JPA 1.0 Specification
    • 2.1.5 Inline classes
    • 2.1.6 Display default values ​​for fields or properties without binding
    • 9.1.34 Embed annotation
    • 9.1.35 Built-in annotation
+2
source

All Articles