我在 oracle 数据库中有一个包含客户数据的表。这是一个简化的定义:
CUSTOMER (CUSTOMER_ID NUMBER NOT NULL,
SOURCE_SYSTEM VARCHAR2(30),
FULL_NAME VARCHAR2(360),
PHONE_NUMBER VARCHAR2(240)
)
此表的主键是(CUSTOMER_ID, SOURCE_SYSTEM)
.
该表有许多SOURCE_SYSTEM
为空的行。在数据库级别,没有问题,但是当我尝试通过 JPA Entity 访问这些行中的任何一个时,它会导致一些问题:
1:使用em.find()
获取空行SOURCE_SYSTEM
总是导致返回空。
2:如果表中不存在该记录,则em.merge()
用于更新具有空值的行成功,但在后续更新时失败,因为合并总是导致插入运行。SOURCE_SYSTEM
3:使用em.createQuery()
显式查询空行会导致以下异常:
Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.3.1.v20111018-r10243):
org.eclipse.persistence.exceptions.QueryException
Exception Description: The primary key read from the row [ArrayRecord(
CUSTOMER.CUSTOMER_ID => 1
CUSTOMER.FULL_NAME => GUY PERSON
CUSTOMER.PHONE_NUMBER => 555-555-1234
CUSTOMER.SOURCE_SYSTEM => null)] during the execution of the query was detected to be null.
Primary keys must not contain null.
Query: ReadAllQuery(referenceClass=Customer sql="SELECT CUSTOMER_ID, FULL_NAME, PHONE_NUMBER, SOURCE_SYSTEM FROM CUSTOMER WHERE ((CUSTOMER_ID = ?) AND (SOURCE_SYSTEM IS NULL))")
不幸的是,“主键不能包含空值”似乎是最终的。我无法找到有关此错误解决方法的太多信息,这使得似乎没有解决方案。
问题:我想知道是否有人有任何不涉及更改数据库的基于 Java 代码的解决方案。我目前的解决方法是ROW_ID
用作@Id
表的 ,但这意味着我不能再使用em.merge()
or em.find()
。
这是我的Java类:
客户.java:
@Entity
@Table(name = "CUSTOMER")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private Customer_Id key;
@Column(name = "CUSTOMER_ID", nullable = false, insertable = false, updatable = false)
private Long customerId;
@Column(name = "SOURCE_SYSTEM", length = 30, insertable = false, updatable = false)
private String sourceSystem;
@Column(name = "FULL_NAME", length = 360)
private String fullName;
@Column(name = "PHONE_NUMBER", length = 240)
private String phoneNumber;
//Setters, Getters, etc
...
}
Customer_Id.java
@Embeddable
public class Customer_Id implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "CUSTOMER_ID", nullable = false)
private Long customerId;
@Column(name = "SOURCE_SYSTEM", length = 30)
private String sourceSystem;
//Setters, Getters, etc
...
}