• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA application issue using netbeans 8.1  RSS feed

 
Julius Adek
Greenhorn
Posts: 23
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 20968
84
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julius Adek wrote:<exclude-unlisted-classes>true</exclude-unlisted-classes>

You've excluded unlisted classes. Where do you include any of them? From the persistence.xml XML schema:
When set to true then only listed classes and jars will
be scanned for persistent classes, otherwise the
enclosing jar or directory will also be scanned.
Not applicable to Java SE persistence units.

So either set it to false (or omit it) if you're in a JEE container, or add a <class> element for each entity and mapped super class.
 
Julius Adek
Greenhorn
Posts: 23
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob spoor, God bless you more. I have set it to "false" as you have instructed and it works.
 
Julius Adek
Greenhorn
Posts: 23
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, when ever i made changes to any of the file like "persistence.xml" or "JPATest.java" file. For example, to change " p.setName("Hendro Steven");" to  " p.setName("James Paul");" save the file and then run the program. It will give the following error:




HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

org.jboss.weld.exceptions.IllegalStateException: WELD-000227: Bean identifier index inconsistency detected - the distributed container probably does not work with identical applications
Expected hash: -1910493119
Current index: BeanIdentifierIndex [hash=2147418287, indexed=12]:
   0: WELD%AbstractBuiltInBean%com.ibm.jbatch.container.cdi.BatchCDIInjectionExtension%HttpSession
   1: WELD%AbstractBuiltInBean%com.sun.faces.application.view.ViewScopeExtension%HttpSession
   2: WELD%AbstractBuiltInBean%org.glassfish.cdi.transaction.TransactionalExtension%HttpSession
   3: WELD%AbstractBuiltInBean%org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider%HttpSession
   4: WELD%AbstractBuiltInBean%org.glassfish.jersey.ext.cdi1x.servlet.internal.CdiExternalRequestScopeExtension%HttpSession
   5: WELD%AbstractBuiltInBean%org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionInterceptorProvider%HttpSession
   6: WELD%AbstractBuiltInBean%org.glassfish.jms.injection.JMSCDIExtension%HttpSession
   7: WELD%AbstractBuiltInBean%org.glassfish.osgicdi.impl.OSGiServiceExtension%HttpSession
   8: WELD%AbstractBuiltInBean%org.glassfish.sse.impl.ServerSentEventCdiExtension%HttpSession
   9: WELD%AbstractBuiltInBean%org.hibernate.validator.internal.cdi.ValidationExtension%HttpSession
  10: WELD%AbstractBuiltInBean%root_web%HttpSession
  11: WELD%AbstractBuiltInBean%web%HttpSession

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.
GlassFish Server Open Source Edition 4.1.1





Moreover, if I try to close the IDE "Netbeans 8.1" to restart it back. This time if I run the program it will work fine but all the data that have been stored in the database before i restart it will not be found again, only the current new data will be retained and if I made any change to any file, the above error will be printed on the screen.

Please, what can I do to this? Thanks for your response.
 
Julius Adek
Greenhorn
Posts: 23
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks it is working fine. I just changed " <property name="eclipselink.ddl-generation"
value="drop-and- <property name="eclipselink.ddl-generation"
value="drop-and-create-tables"/> -tables"/> "  to " <property name="eclipselink.ddl-generation"
value="create-tables"/>".
 
Rob Spoor
Sheriff
Posts: 20968
84
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing the solution
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!