TTS
TTS

Reputation: 67

Cannot find the declaration of element 'persistence' JPA and hibernate provider

I am new in jpa and I am tryingg to write and execute a sample founded here:Sample JPA

This is my table created: enter image description here

Here is my persistance.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_0.xsd">
<persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ir.ac.sbu.testsimplesql1.Employee</class>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/library?zeroDateTimeBehavior=convertToNull"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.password" value="pass"/>
  <property name="hibernate.archive.autodetection" value="class"/>
  <property name="hibernate.show_sql" value="true"/>
  <property name="hibernate.format_sql" value="true"/>
  <property name="hbm2ddl.auto" value="update"/>
</properties>

I have auto generate model class from database:

package ir.ac.sbu.testsimplesql1;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "Employee")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e"),
@NamedQuery(name = "Employee.findById", query = "SELECT e FROM Employee e WHERE e.id = :id"),
@NamedQuery(name = "Employee.findByFistName", query = "SELECT e FROM Employee e WHERE e.fistName = :fistName"),
@NamedQuery(name = "Employee.findByLastName", query = "SELECT e FROM Employee e WHERE e.lastName = :lastName"),
@NamedQuery(name = "Employee.findByDept", query = "SELECT e FROM    Employee e WHERE e.dept = :dept")})
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "fistName")
private String fistName;
@Column(name = "lastName")
private String lastName;
@Column(name = "dept")
private String dept;

public Employee() {
}

public Employee(Integer id) {
    this.id = id;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getFistName() {
    return fistName;
}

public void setFistName(String fistName) {
    this.fistName = fistName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getDept() {
    return dept;
}

public void setDept(String dept) {
    this.dept = dept;
}

@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 Employee)) {
        return false;
    }
    Employee other = (Employee) 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 "ir.ac.sbu.testsimplesql1.Employee[ id=" + id + " ]";
}
}

This is my poem.xml: (Dependencies Tag)

    <dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.8.Final</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.27</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
    </dependency>
</dependencies>

And the last thing is my main function:

public class EmployeeTest {



private static EntityManager em;

public static void main(String[] args) {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService");
    em = emf.createEntityManager();

    createEmployee(1, "Saint", "Peter", "Engineering");
    createEmployee(2, "Jack", " Dorsey", "Imaginea");
    createEmployee(3, "Sam", "Fox", "Imaginea");

}

private static void createEmployee(int id, String firstName, String lastName, String dept) {
    em.getTransaction().begin();
    Employee emp = new Employee();
    emp.setId(id);
    emp.setFistName(firstName);
    emp.setLastName(lastName);
    emp.setDept(dept);

    em.persist(emp);
    em.getTransaction().commit();
}
}

But when I run this code I face with this error: enter image description here

I have searched this error bud couldn't understand what should I do. I've also visited this link: Can not find the declaration of element 'persistence'

javax.persistence.PersistenceException - JPA+Hibernate and alot more similar questions ....

Can anyone please help me to solve this error?

Thanks in advance for your attention.

Upvotes: 1

Views: 1682

Answers (1)

J.Mengelle
J.Mengelle

Reputation: 341

You can check your XML against the XSD here : http://www.freeformatter.com/xml-validator-xsd.html Very useful.

Replace the beginning of your xml with:

<persistence 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"
version="2.0">

I don't know why, but yours is not validated.

Also, you miss the end of the xml, but i guess it's just a copy/paste error.

Upvotes: 3

Related Questions