Please I just had issue with JPA sample application with Netbeans
IDE 8.1, the following is the error message after I run the app:
Jun 08, 2017 11:41:04 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.1.2.Final
[EL Info]: 2017-06-08 23:41:10.481--ServerSession(2504638)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Info]: connection: 2017-06-08 23:41:43.366--ServerSession(2504638)--file:/D:/New E-Library/360View Apps/JPA tutor 8 June 2017/build/web/WEB-INF/classes/_JPATest2_PU login successful
[EL Warning]: metamodel: 2017-06-08 23:41:43.444--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
java.lang.IllegalArgumentException: Object: entity.Person[ id=null ] 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 JPATest2.persist(JPATest2.java:57)
at JPATest2.main(JPATest2.java:48)
BUILD SUCCESSFUL (total time: 1 minute 11 seconds)
All the files are:
1. persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="JPATest2_PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
<property name="javax.persistence.jdbc.password" value="app"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
2. Person.java
package entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id ;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private
String address;
/**
* Get the value of address
*
* @return the value of address
*/
public String getAddress() {
return address;
}
/**
* Set the value of address
*
* @param address new value of address
*/
public void setAddress(String address) {
this.address = address;
}
private String phoneNumber;
/**
* Get the value of phoneNumber
*
* @return the value of phoneNumber
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* Set the value of phoneNumber
*
* @param phoneNumber new value of phoneNumber
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@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 Person)) {
return false;
}
Person other = (Person) 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 "entity.Person[ id=" + id + " ]";
}
}
3. JPATest.java
import entity.Person;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
public class JPATest2 {
public static void main(String[] args) {
Person p = new Person();
p.setName("Hendro Steven");
p.setAddress("Salatiga, Indonesia");
p.setPhoneNumber("+6281390989669");
JPATest2
test = new JPATest2();
test.persist(p);
}
public void persist(Object object) {
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("JPATest2_PU");
EntityManager em = null;
em = emf.createEntityManager();
em.getTransaction().begin();
try {
em.persist(object);
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
} finally {
em.close();
}
}
}
Please, what did I need to put in place for this JPA app to auto create table and work as intended? Thanks for your response.