suraj kiran suraj kiran - 1 year ago 251
Java Question

entity has no primary key attribute defined : JPA

i am using Eclipse neon.i am new to JPA. i always getting this error.
please help to fix this.

below is my POJO class

package myJPA;

import javax.persistence.*;
import java.util.Date;

* The persistent class for the user_online database table.
@NamedQuery(name="UserOnline.findAll", query="SELECT u FROM UserOnline u")

public class UserOnline implements Serializable {
private static final long serialVersionUID = 1L;

private int memId;

private Date time;

public UserOnline() {

public int getMemId() {
return this.memId;

public void setMemId(int memId) {
this.memId = memId;

public Date getTime() {
return this.time;

public void setTime(Date time) {
this.time = time;


my persistence.xml contents are below

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<persistence-unit name="myJPA">

when i run my main class with this error i get these errors

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named Eclipselink_JPA
at javax.persistence.Persistence.createEntityManagerFactory(
at javax.persistence.Persistence.createEntityManagerFactory(
at myJPA.Main.main(

package myJPA;

import java.sql.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" );

EntityManager entitymanager = emfactory.createEntityManager( );
entitymanager.getTransaction( ).begin( );
UserOnline u = new UserOnline();
u.setTime(new Date(0));
entitymanager.persist( u );
entitymanager.getTransaction( ).commit( );

entitymanager.close( );
emfactory.close( );



Answer Source

EntityManagerFactory initialization should be changed to below :

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "myJPA" );

You have to provide name of your defined persistence unit to create factory from it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download