Question

I'm tring to insert my student object to Students table in mydb database, but always get exception. However connecting to table using Connection works properly. I'm using Eclipse with EclipseLink.

entity class

package student;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "STUDENTS")
public class Student {
    @Id
    @Column(name = "studentID")
    @GeneratedValue
    private int id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "GROUPNUM")
    private String groupNum;
    @Column(name="AVGMARK")
    private double avgMark;


    public Student(){}

    public Student(String name, String groupNum, double avgMark) {
        this.name = name;
        this.groupNum = groupNum;
        this.avgMark = avgMark;

    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGroupNum() {
        return groupNum;
    }

    public void setGroupNum(String groupNum) {
        this.groupNum = groupNum;

    }

    public double getAvgMark() {
        return avgMark;
    }

    public void setAvgMark(double avgMark) {
        this.avgMark = avgMark;
    }

}

method in my class

package connector;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
public void insertIntoStudents(Student st) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(
                "students");
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        em.persist(st);
        em.getTransaction().commit();
        em.close();


    }

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="students" transaction-type="RESOURCE_LOCAL">
    <class>student.Student</class>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url"
        value="jdbc:mysql://localhost/mydb" />
      <property name="javax.persistence.jdbc.user" value="sqluser" />
      <property name="javax.persistence.jdbc.password" value="sqluserpw" />
    </property></properties>

  </persistence-unit>
</persistence> 

Exception

Exception in thread "main" java.lang.IllegalArgumentException: Object: student.Student@62d7ef8f 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 connector.DBconnector.insertIntoStudents(DBconnector.java:60)
    at Main.main(Main.java:10)
Was it helpful?

Solution

Put full name of entity in persistence xml. I mean full package name

package com.mycom.api.data;

@entity
public class student{

...
..
..
}

You should put com.mycom.api.data.student in persistence.xml.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top