I am using hibernate3-maven-plugin to query the Oracle10g database and create java classes using JPA annotations using the hbm2java target. Then I use spring to set up a factory session that scans annotated code.
After a long struggle with hbm2java I managed to get my classes, but now I have another problem: I get the exception "Mixing with zero and non-currency columns in the property is not allowed" when annotated classes are loaded.
The database defines the FOO and BAR tables as follows:
CREATE TABLE FOO ( STATUS_CODE ... NOT NULL, REASON_CODE ...); ALTER TABLE FOO ADD ( CONSTRAINT FK_BAR FOREIGN KEY (REASON_CODE, STATUS_CODE) REFERENCES BAR(REASON_CODE, STATUS_CODE)); CREATE TABLE BAR ( STATUS_CODE ... NOT NULL, REASON_CODE ... NOT NULL); ALTER TABLE BAR ( PRIMARY KEY (REASON_CODE, STATUS_CODE));
Thus, the FOO table has two columns, which are foreign keys in the BAR table. The column FOO.STATUS_CODE must not be null, but the column FOO.REASON_CODE may be null. The logic here is that FOO needs a status, but not every condition needs a reason.
The BAR table has columns BAR.REASON_CODE and BAR.STATUS_CODE, both of which are not equal to zero. The logic here links the causes for the various (but not all) status codes. So, for example, if the status is "canceled", then the cause may be "fraud", "incompetence", etc.
Note that a status, such as “active”, has no associated reason, therefore it does not exist in the BAR table, but can occur as a status code in the FOO table (without the corresponding reason code). But if a row in FOO has a status code "canceled", then it must also have one of the reason codes defined in the BAR table for this state.
Therefore, the definitions of the tables seem fine to me (although I am not a database expert).
Now the hbm2java target in maven generates the following code for the FOO table:
private Bar bar; @ManyToOne(fetch=FetchType.LAZY) @JoinColumns( { @JoinColumn(name="REASON_CODE", referencedColumnName="REASON_CODE"), @JoinColumn(name="STATUS_CODE", referencedColumnName="STATUS_CODE", nullable=false) } ) public Bar getBar() { return this.bar; }
Note that only the STATUS_CODE column is not NULL.
But when a factory bean hibernation session is created and scans annotated classes, an “exception and zero column offset in the property is invalid” exception is thrown.
Caused by: org.hibernate.AnnotationException: Mixing nullable and non nullable columns in a property is not allowed: com.whatever.domain.LnrPermissionlnrPermStatusReason at org.hibernate.cfg.Ejb3Column.checkPropertyConsistency(Ejb3Column.java:514) at org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2568) at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1527) at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:769) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:733) at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:636)
Question
Is this a valid error caused by the sleep annotation processing code (in this case, you need to change the table definitions in the database), or is this invalid? And if the latter, can I configure the factory session code to ignore such errors?
Configuration
Maven Dependencies:
- org.hibernate / hibernation-kernel / 3.5.6-Final
- org.hibernate / sleep-annotations / 3.5.6-Final
- org.springframework / spring -orm / 3.1.2-RELEASE
Spring application context:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.whatever.domain" /> </bean>