4

I have couple of failure cases for Unidirectional JPA2 @OnetoMany relationship below is the code snippet

@Entity
@Table(name="CUSTOMER")
@Access(AccessType.FIELD)
public class Customer {

       @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
       @JoinColumn(name="CUSTOMER_ID", referencedColumnName="CUSTOMER_ID")
       private List<Address> customerAddresses;

....
}

In this case it fails to create Entity manager factory during server startup with the following error

DEBUG - Second pass for collection: xx.xxx.xxxxxxx.core.domainmodel.customerinfo.Customer.customerAddresses
 DEBUG - Binding a OneToMany: xx.xxx.xxxxxxx.core.domainmodel.customerinfo.Customer.customerAddresses through a foreign key
 DEBUG - Mapping collection: xx.xxx.xxxxxxx.core.domainmodel.customerinfo.Customer.customerAddresses -> CUSTOMER_ADDRESS
 DEBUG - Unable to build entity manager factory
java.lang.NullPointerException: null
 at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1456) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]

The server startup is all good when I remove the referencedColumnName attribute from the @JoinColumn annotation

But when I try to persist the entity it fails below are the Hibernate generated traces for the failure(CUSTOMER_ID is the name of the identity generated PK column in CUSTOMER table and FK in the CUSTOMER_ADDRESS table)

DEBUG - Executing identity-insert immediately
DEBUG - insert into CUSTOMER (ESTABLISHMENT_DATE, ESTABLISHMENT_PLACE, MAJOR_PRODUCT, PAID_UP_CAPITAL, TYPE_OF_BUSINESS, COMPANY_REGISTRATION_NUMBER, CUSTOMER_TYPE, FAX_NUMBER, ID_EXPIRY_DATE, ID_ISSUE_DATE, ID_ISSUE_PLACE, ID_NUMBER, ID_TYPE, TELEPHONE_NO, ENGLISH_NAME, RACE, NATIONALITY, MALAY_NAME) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
DEBUG - Natively generated identity: 6
DEBUG - Executing identity-insert immediately
DEBUG - insert into CUSTOMER_ADDRESS (COUNTRY, ADDRESS_LINE1, ADDRESS_LINE2, ADDRESS_LINE3, ADDRESS_LINE4, PINCODE, STATE, ADDRESS_TYPE) values (?, ?, ?, ?, ?, ?, ?, ?)
DEBUG - could not execute statement [n/a]
com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'CUSTOMER_ID', table 'xxxxxxx.xxx.CUSTOMER_ADDRESS'; column does not allow nulls. INSERT fails.

What is the reason for failure in the first case, and how to get it to work either way, any help is much appreciated.

Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85
  • Can you confirm that your annotation @JoinColumn with "name" and "referencedColumnName" is fine? Example: name: If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy, the foreign key is in the table of the target entity. referencedColumnName: When used with a unidirectional OneToMany foreign key mapping, the referenced column is in the table of the source entity. – Mário Kapusta Aug 04 '15 at 14:37
  • If you really want to have a _unidirectional_ `OneToMany` mapping, you shoud check [One-To-Many Relationship with Join Table](http://stackoverflow.com/questions/1378248/one-to-many-relationship-with-join-table). – Tobias Liefke Aug 04 '15 at 15:49
  • @Mário Kapusta: The foreignkey is in the target entity while I placed the Joincolumn in the source entity here. Since I want to retreive address only from Customer entity not and as a stand alone data, in which case it may not have any value as a data. So as per your comment JoinColumn(name="CUSTOMER_ID", referencedColumnName="CUSTOMER_ID") private List
    customerAddresses; is supposed to cause an error in this case?
    – Somasundaram Sekar Aug 04 '15 at 16:57
  • hello(sorry that i am so late)...answer: yes...you should have "referencedColumnName" - column of source entity ,,,,, "name" - fk in target entity. In your case it seems like bug is here. – Mário Kapusta Aug 08 '15 at 23:12

2 Answers2

11

I had the same issue. Adding @JoinColumn(nullable = false, ...) fixed it.

Alex Vayda
  • 6,154
  • 5
  • 34
  • 50
  • Thank you. Worked for me :) – mdziob Apr 07 '17 at 12:31
  • Even this fixed for me, but what did it do to fix it? What is the meaning of this configuration. – ThinkGeek May 21 '18 at 14:17
  • 1
    @LokeshAgrawal - *(optional) Whether the foreign key column is nullable* - is sated in the documentation for the attribute `nullable`. if you constrain the foreign key to **not null**, you must specify `@JoinColumn(nullable = false)`. Otherwise it is not needed. – Erfan Ahmed Jul 03 '18 at 05:37
-2

Probably the links below could give you more information on oneToMany relationship.

It indicates to use mappedBy at parent side and use JoinColumn at childs.

JPA JoinColumn vs mappedBy

Community
  • 1
  • 1
Mihir
  • 270
  • 3
  • 9
  • He is talking about an _unidirectional_ link. In that case it makes no sense to use a `mappedBy` - because that one is used in _bidirectional_ links. – Tobias Liefke Aug 04 '15 at 15:43
  • My apologies; I just missed Unidirectional part. Thanks for pointing that out. – Mihir Aug 04 '15 at 16:09