pbuchheit
pbuchheit

Reputation: 1607

Hibernate Validation Causing Wrong Column Type Error

I'm having a bit of an issue with hibernate validating during application deployment. I have two classes, Frame and FrameReleasePlan that are associated in a OneToOne relationship. On the database side, the relationship is unidirectional. The frame_release_planss table has a 'frame_id' column of type NUMERIC(19,0) that is a foreign key pointing to the 'id' column of the frames table. When I try to deploy, the schema validation fails with the error:

Unable to build Hibernate SessionFactory. Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [frame_id] in table [frame_release_plans]; found [numeric (Types#NUMERIC)], but expecting [bigint (Types#BIGINT)]"}

I have tried manually specifying the column type using the 'columnDefinition' argument of the @JoinColumn annotation, but it made no difference. Is there any way to fix this with having to change the database column to 'bigint'?

persistence.xml

<persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="ProductPersistenceUnit"
        transaction-type="JTA">
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.id.new_generator_mappings" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

Code Snippets:

@Entity(name = "FrameReleasePlan")
@Table(name = "frame_release_plans")
@Audited
@Access(AccessType.FIELD)
public class FrameReleasePlan extends AbstractFrameReleasePlan
{
    @OneToOne
    @JoinColumn(name = "frame_id")
    protected Frame frame;
}

@Entity(name = "Frame")
@DiscriminatorValue("FRAME")
public class Frame extends Product
{
    @OneToOne(mappedBy = "frame", cascade = { CascadeType.ALL }, orphanRemoval = true)
    protected FrameReleasePlan releasePlan;
}

@javax.persistence.Entity(name = "Product")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "products")
public abstract class Product extends Entity
{
    @Id
    @DocumentId
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(columnDefinition = "NUMERIC(19,0)")
    protected Long id;
}

Upvotes: 2

Views: 19923

Answers (2)

Marco Tizzano
Marco Tizzano

Reputation: 1896

The issue should be in the type you have chosen to map the field "id" of Product class with the related database column.
Hibernate is able to map each database column type to specific Java type. In particular you can have:

NUMERIC <---> BigInteger or BigDecimal
BIGINT <---> long or Long

So, to fix the issue, I suggest that you change the Product's "id" field type from Long to BigInteger or, in alternative, the database column type from NUMERIC to BIGINT.

Upvotes: 12

SternK
SternK

Reputation: 13041

According to the standard hibernate basic types table mapping the jdbc NUMERIC type is mapped to the java.math.BigInteger and java.math.BigDecimal.

So, try to correct your mapping like this:

@Id
@DocumentId
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(columnDefinition = "NUMERIC(19,0)")
protected BigInteger id;

Upvotes: 2

Related Questions