You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by "Daniel Gajdos (JIRA)" <ji...@apache.org> on 2007/03/21 12:20:32 UTC

[jira] Created: (OPENJPA-177) When using NOT NULL constraint on foreign key it is not possible to use CascadeType.ALL and GeneratedId

When using NOT NULL constraint on foreign key it is not possible to use CascadeType.ALL and GeneratedId
-------------------------------------------------------------------------------------------------------

                 Key: OPENJPA-177
                 URL: https://issues.apache.org/jira/browse/OPENJPA-177
             Project: OpenJPA
          Issue Type: Bug
          Components: jpa
    Affects Versions: 0.9.6
         Environment: Windows XP, IBM 32-bit SDK 5.0, WebSphere 6.1
            Reporter: Daniel Gajdos


When using on entity Application
	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "C_PERSON_ID", nullable=false)
	@ForeignKey
	private Person person;
and on entity Person using generated ID, it fails on inserting Application because of performing db actions in this order:
INSERT INTO T_APPLICATION (C_ID, C_APPLICATION_NUMBER, C_APPLIED_DATE, C_STATUS) VALUES (:1, :2, :3, :4) - here it tries to insert null value as 		C_PERSON_ID, which is not permited because of nullable=false declaration. This declaration creates NOT NULL constraint on C_PERSON_ID.
INSERT INTO NSVISP.T_PERSON (C_ID, C_BIRTH_DATE, C_FIRST_NAME, C_LAST_NAME) VALUES (:1, :2, :3, :4)
UPDATE NSVISP.T_APPLICATION SET C_PERSON_ID = :1 WHERE C_ID = :2
Error reported from DB:
org.apache.openjpa.lib.jdbc.ReportingSQLException: ORA-01400: cannot insert NULL into ("T_APPLICATION"."C_PERSON_ID")

Is there any chance to change the order in which this operations are executed? On this type of constraint we have to insert Person before inserting Application and include the generated Person Id into insert statement for Application. Otherwise we have to deffer the NOT NULL constraint which can be dangerous and it is not defered initialy.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (OPENJPA-177) When using NOT NULL constraint on foreign key it is not possible to use CascadeType.ALL and GeneratedId

Posted by "Daniel Gajdos (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-177?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12483068 ] 

Daniel Gajdos commented on OPENJPA-177:
---------------------------------------

In official manual for OpenJPA, chapter 13.2. Schema Factory (http://people.apache.org/~mprudhom/openjpa/site/openjpa-project/manual/ref_guide_schema_info.html#ref_guide_schema_info_factory) I found:
When using this factory, it is important that your mapping metadata correctly represent your database's foreign key constraints so that OpenJPA can order its SQL statements to meet them.
And my question is how is OpenJPA ordering it;s statements when as you can see it is inserting Application first and then Person and it takes care about @ForeignKey constraint by inserting null value in Application and then updating this value to PersonID. Is it true that OpenJPA doesn't take care about non-nullable constraints? Another interesting thing is that after changing nullable to false it does not change the DB schema by adding not null constraint, it adds this constraint only when creating TABLE. Is this correct?

> When using NOT NULL constraint on foreign key it is not possible to use CascadeType.ALL and GeneratedId
> -------------------------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-177
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-177
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 0.9.6
>         Environment: Windows XP, IBM 32-bit SDK 5.0, WebSphere 6.1
>            Reporter: Daniel Gajdos
>
> When using on entity Application
> 	@OneToOne(cascade = CascadeType.ALL)
> 	@JoinColumn(name = "C_PERSON_ID", nullable=false)
> 	@ForeignKey
> 	private Person person;
> and on entity Person using generated ID, it fails on inserting Application because of performing db actions in this order:
> INSERT INTO T_APPLICATION (C_ID, C_APPLICATION_NUMBER, C_APPLIED_DATE, C_STATUS) VALUES (:1, :2, :3, :4) - here it tries to insert null value as 		C_PERSON_ID, which is not permited because of nullable=false declaration. This declaration creates NOT NULL constraint on C_PERSON_ID.
> INSERT INTO NSVISP.T_PERSON (C_ID, C_BIRTH_DATE, C_FIRST_NAME, C_LAST_NAME) VALUES (:1, :2, :3, :4)
> UPDATE NSVISP.T_APPLICATION SET C_PERSON_ID = :1 WHERE C_ID = :2
> Error reported from DB:
> org.apache.openjpa.lib.jdbc.ReportingSQLException: ORA-01400: cannot insert NULL into ("T_APPLICATION"."C_PERSON_ID")
> Is there any chance to change the order in which this operations are executed? On this type of constraint we have to insert Person before inserting Application and include the generated Person Id into insert statement for Application. Otherwise we have to deffer the NOT NULL constraint which can be dangerous and it is not defered initialy.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (OPENJPA-177) When using NOT NULL constraint on foreign key it is not possible to use CascadeType.ALL and GeneratedId

Posted by "Abe White (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-177?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12482815 ] 

Abe White commented on OPENJPA-177:
-----------------------------------

OpenJPA will insert records in the order that you persist the corresponding objects.  When you persist in an order that would violate foreign key constraints, OpenJPA attempts to insert null and then update the foreign key value in a separate statement.  If you use non-nullable constraints, though, you must persist your objects in the correct order.

> When using NOT NULL constraint on foreign key it is not possible to use CascadeType.ALL and GeneratedId
> -------------------------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-177
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-177
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 0.9.6
>         Environment: Windows XP, IBM 32-bit SDK 5.0, WebSphere 6.1
>            Reporter: Daniel Gajdos
>
> When using on entity Application
> 	@OneToOne(cascade = CascadeType.ALL)
> 	@JoinColumn(name = "C_PERSON_ID", nullable=false)
> 	@ForeignKey
> 	private Person person;
> and on entity Person using generated ID, it fails on inserting Application because of performing db actions in this order:
> INSERT INTO T_APPLICATION (C_ID, C_APPLICATION_NUMBER, C_APPLIED_DATE, C_STATUS) VALUES (:1, :2, :3, :4) - here it tries to insert null value as 		C_PERSON_ID, which is not permited because of nullable=false declaration. This declaration creates NOT NULL constraint on C_PERSON_ID.
> INSERT INTO NSVISP.T_PERSON (C_ID, C_BIRTH_DATE, C_FIRST_NAME, C_LAST_NAME) VALUES (:1, :2, :3, :4)
> UPDATE NSVISP.T_APPLICATION SET C_PERSON_ID = :1 WHERE C_ID = :2
> Error reported from DB:
> org.apache.openjpa.lib.jdbc.ReportingSQLException: ORA-01400: cannot insert NULL into ("T_APPLICATION"."C_PERSON_ID")
> Is there any chance to change the order in which this operations are executed? On this type of constraint we have to insert Person before inserting Application and include the generated Person Id into insert statement for Application. Otherwise we have to deffer the NOT NULL constraint which can be dangerous and it is not defered initialy.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.