I am getting the following exception over and over trying to use JPA in NetBeans 11.2 on MacOS.
java.lang.IllegalArgumentException: Object: javaapplication4.Id[ id=5 ] is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4228)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
at javaapplication4.JavaApplication4.main(JavaApplication4.java:30)
My main class:
package javaapplication4;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
/**
*
* @author Informatica
*/
public class JavaApplication4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication4PU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try{
Id id = new Id();
em.persist(id);
tx.commit();
}catch(Exception e ){
e.printStackTrace();
tx.rollback();
}
em.close();
emf.close();
}
}
My Entity Class:
package javaapplication4;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
* @author Informatica
*/
@Entity
@Table(name = "ID")
@NamedQueries({
@NamedQuery(name = "Id.findAll", query = "SELECT i FROM Id i"),
@NamedQuery(name = "Id.findById", query = "SELECT i FROM Id i WHERE i.id = :id")})
public class Id implements Serializable {
private static final long serialVersionUID = 1L;
@javax.persistence.Id
@Column(name = "ID")
private Long id;
public Id() {
this.id = new Long(5);
}
public Id(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Id)) {
return false;
}
Id other = (Id) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "javaapplication4.Id[ id=" + id + " ]";
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JavaApplication4PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>javaapplication4.Id</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/s"/>
<property name="javax.persistence.jdbc.user" value="s"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.password" value="s"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
My libraries:
I am using Derby DB.
Let me know if you can find what's wrong with this. Thank you in advance.
Problem soved, for some reason it only worked with jdk 8, switching jkd fixed the problem