Home > EclipseLink, JPA > EclipseLink error : Null primary key encountered in unit of work clone

EclipseLink error : Null primary key encountered in unit of work clone

With EclipseLink used as the JPA provider, the following error message “Null primary key encountered in unit of work clone” may appear if the value 0 is used for a field which is a primary key.

To resolve it, a few solutions :

1) start your primary key values from 1 instead of 0, which in my opinion is the best solution.

2) add the following annotation to the entity :

import javax.persistence.Entity;
import javax.persistence.Table;

import org.eclipse.persistence.annotations.IdValidation;
import org.eclipse.persistence.annotations.PrimaryKey;

 * The persistent class for the HOUSE database table.
@Table(name = "HOUSE")
public class House implements Serializable {



3) add the following property to the persistence.xml file :

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
	xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

	<persistence-unit name="myUnit">

                    <property name="eclipselink.allow-zero-id" value="true"/> 



Links :

Categories: EclipseLink, JPA Tags:
  1. No comments yet.
You must be logged in to post a comment.