JPA单向@OnetoMany失败 [英] JPA Unidirectional @OnetoMany fails

查看:93
本文介绍了JPA单向@OnetoMany失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些针对单向JPA2 @OnetoMany关系的失败案例 下面是代码段

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]

当我从@JoinColumn注释中删除referencedColumnName属性时,服务器启动一切都很好

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

但是当我尝试保留该实体时,它在下面失败了,这是Hibernate为失败生成的跟踪记录(CUSTOMER_ID is the name of the identity generated PK column in CUSTOMER table and FK in the CUSTOMER_ADDRESS table)

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.

推荐答案

我遇到了同样的问题.添加@JoinColumn(nullable = false, ...)修复了该问题.

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

这篇关于JPA单向@OnetoMany失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆