You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by "Vermeulen (JIRA)" <ji...@apache.org> on 2011/02/23 10:04:38 UTC

[jira] Commented: (OPENJPA-1949) inserting new entities to the front of an existing List fails when using GenerationType.IDENTITY

    [ https://issues.apache.org/jira/browse/OPENJPA-1949?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12998253#comment-12998253 ] 

Vermeulen commented on OPENJPA-1949:
------------------------------------

I ran the same test with GenerationType.TABLE and an @TableGenerator with an allocationSize of 1 (not recommended for performance!) and the test passes, so the problem does not occur here.

> inserting new entities to the front of an existing List fails when using GenerationType.IDENTITY
> ------------------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-1949
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-1949
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: kernel
>    Affects Versions: 2.1.0
>         Environment: ms sql server
>            Reporter: Vermeulen
>         Attachments: OPENJPA-1949-TestCase.zip, OPENJPA-1949.ut.patch
>
>
> This problem occurs when I try to insert new entities to an existing list and I use a generated identity with GenerationType.IDENTITY.
> I start with a fresh database and let OpenJPA create the schema. I have a ProductOrder entity that contains a List of ProductOrderLines, annotated as such:
> @Entity
> public class ProductOrder {
> ...
>             @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
>             private List<ProductOrderLine> products = new ArrayList<ProductOrderLine>();
> ...
> }
> The entity in the List (ProductOrderLine) has a generated id with GenerationType.IDENTITY.
> @Entity
> public class ProductOrderLine {
>             @Id
>             @GeneratedValue(strategy = GenerationType.IDENTITY)
>             private Long id;
> ...
> }
> I start with a ProductOrder that has these products:
> 1 - orange
> 2 - apple
> I insert two new products into the front of the list so that I get:
> null - banana
> null - pear
> 1 - orange
> 2 - apple
> Then I merge the entity (I work with attach/detach, not sure if this matters).
> OpenJPA merge correctly returns a ProductOrder with this list:
> 3 - banana
> 4 - pear
> 1 - orange
> 2 - apple
> However OpenJPA generates the wrong SQL so that the database contains something completely different and indeed selecting the ProductOrder by it's id gives:
> 3 - banana
> 4 - pear
> 4 - pear
> 4 - pear
> I tested this with ms sql server 2008 express. (I tried hsqldb but this also suffers from bug OPENJPA-1066).
> This problem does not occur when I use GenerationType.TABLE for ProductOrderLine. My example uses a join table, but foreign key columns seem to have the same problem.
> Different workarounds I found:
> - Use another GenerationType for the id column
> - add the new entities to the end of the List
> - (somehow only works in the toy example not in our application) replace the entire list reference to a new ArrayList containing the same elements in the same order

-- 
This message is automatically generated by JIRA.
-
For more information on JIRA, see: http://www.atlassian.com/software/jira