JPA实体未知

时间:2013-11-16 17:06:44

标签: eclipse persistence eclipselink persistence.xml

我想把我的学生对象插入mydb数据库中的Students表,但总是得到异常。但是,使用Connection连接到表可正常工作。我正在使用Eclipse和EclipseLink。

实体类

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;
    }

}
我班上的

方法

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 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)

1 个答案:

答案 0 :(得分:2)

将实体的全名放在持久性xml中。我的意思是完整的包名

package com.mycom.api.data;

@entity
public class student{

...
..
..
}

您应该将 com.mycom.api.data.student 放在persistence.xml中。