我正在使用 OpenJPA 和 Eclipse 来持久化对象。我创建了一个简单的一对一单向应用程序。但它给出了外键空错误。
学生实体
@Entity
public class Student implements Serializable {
@Id
private int id;
private String name;
private static final long serialVersionUID = 1L;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "dept_id", unique = true, nullable = true, insertable = true, updatable = true, referencedColumnName = "id")
private Department department;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n" + department;
}
}
部门实体
@Entity
@Table(name = "department")
public class Department implements Serializable {
@Id
private int id;
private String name;
private static final long serialVersionUID = 1L;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String deptName) {
this.name = deptName;
}
public String toString() {
return "Department id: " + getId() + ", name: " + getName();
}
}
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="IBMJPADemo" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.ibm.jpa.onetoone.model.Department</class>
<class>com.ibm.jpa.onetoone.model.Student</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/test" />
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="root" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
<!-- property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)" /-->
<!-- property name="openjpa.jdbc.MappingDefaults"
value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict" /-->
</properties>
</persistence-unit>
</persistence>
客户计划
public class OneToOneClient {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("IBMJPADemo");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Student student = new Student();
student.setId(2537);
student.setName("K.Senthuran");
Department dept = new Department();
dept.setId(100);
dept.setName("IT");
student.setDepartment(dept);
em.persist(student);
em.flush();
tx.commit();
em.close();
}
}
错误
线程“主”org.apache.openjpa.persistence.PersistenceException 中的异常:在 org.apache.openjpa.kernel.DelegatingBroker 的 org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1817) 中不允许空键。 flush(DelegatingBroker.java:1037) at org.apache.openjpa.persistence.EntityManagerImpl.flush(EntityManagerImpl.java:652) at com.ibm.jpa.onetoone.client.OneToOneClient.main(OneToOneClient.java:32) : java.lang.NullPointerException: 在 org.apache.openjpa.kernel.ManagedCache.assignObjectId(ManagedCache.java:189) 的 org.apache.commons.collections.map.AbstractReferenceMap.put(AbstractReferenceMap.java:248) 不允许空键) 在 org.apache.openjpa.kernel.BrokerImpl.setStateManager(BrokerImpl.java:4046) 在 org.apache.openjpa.kernel.BrokerImpl.assignObjectId(BrokerImpl.java:4949) 在 org.org.apache.openjpa.kernel.StateManagerImpl.afterFlush(StateManagerImpl.java:1084) 上的 apache.openjpa.kernel.StateManagerImpl.assertObjectIdAssigned(StateManagerImpl.java:636) org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl. java:2162) 在 org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:2037) 在 org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1808) ... 还有 3 个
请帮助解决这个问题。
谢谢和问候,
K.Senthuran