Vermeulen created OPENJPA-2311:
----------------------------------

             Summary: JPA cannot handle @OneToMany with @OrderColumn and 
non-nullable @JoinColumn with foreign key constraint
                 Key: OPENJPA-2311
                 URL: https://issues.apache.org/jira/browse/OPENJPA-2311
             Project: OpenJPA
          Issue Type: Bug
          Components: jdbc
    Affects Versions: 2.2.0, 2.3.0
            Reporter: Vermeulen
            Priority: Minor


Situation: an ordered List whose entities are completely owned by the parent 
entity but do not have a reference to the parent entity AND using foreign key 
constraints using the option:

<property name="openjpa.jdbc.MappingDefaults" 
value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict" /> 

This situation can be mapped by using a @OneToMany with the @JoinColumn on the 
One side (JPA2 only). I would like to have this join column non-nullable.

@Entity
public class BattingOrder {
    ...

    @JoinColumn(name="BATTING_ORDER_PARENT_ID", nullable=false)
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, 
orphanRemoval=true)
    @OrderColumn
    private List<Player> batters;
        ...
        
When inserting a new Player into the batters list and merging BattingOrder, the 
following exception is thrown:

org.apache.openjpa.persistence.InvalidStateException: Attempt to set column 
"Player.batters_ORDER" to two different values: (class java.lang.Integer)"0", 
(class java.lang.Integer)"1" This can occur when you fail to set both sides of 
a two-sided relation between objects, or when you map different fields to the 
same column, but you do not keep the values of these fields in synch.
        at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:344)
        
Can be reproduced by adjusting the unit test 
org.apache.openjpa.persistence.jdbc.order.TestOrderColumn by changing 
BattingOrder as above, adding the MappingDefaults property, adjusting it to use 
JPA version 2 and using cascade persist with BattingOrder instead of persisting 
the players first.

Then testOneToManyElementRemoval and testOneToManyElementInsert fail. This 
problem goes away when making the join column nullable or when not setting the 
MappingDefaults property.

(Some other of the TestOrderColumn tests fail as well, but this is because 
Players are made that are not in the list of batters and it's logical that this 
is impossible with the non-nullable join column. It's also logical that we need 
orphan removal otherwise because the join column cannot be set to null on 
removal from the list).

I marked this bug as minor because there are so many combinations until this 
occurs.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to