Jpa Unknown entity:
JPa , 。 , 。 。
java.lang.IllegalArgumentException: Unknown entity: com.entity.TestEntity
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:671)
at com.test.Case.test(Case.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
この誤りを排除する
1)entityの中で、空の属性ではなく、set()があるかどうかを見てみましょう.
2)META-INFpersistence.xml、entityを宣言しましたか?
最後に自分を見つけたxmlは次の文を加えていませんJpa Unknown entityを招きました.
これは私の最後のテストに成功した例です.
entity
package com.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
public class TestEntity {
private Integer no1;
private String str1;
public TestEntity() {
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "myid")
@SequenceGenerator(name = "myid", sequenceName = "myid")
@Column(nullable = false)
public Integer getNo1() {
return no1;
}
public void setNo1(Integer no1) {
this.no1 = no1;
}
@Column(nullable = false, length = 12)
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
}
Test
@Test
public void test() {
// TODO: handle exception
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("JpaTest");
EntityManager entityManager = factory.createEntityManager();
TestEntity testEntity = new TestEntity();
testEntity.setStr1("ningnian");
entityManager.getTransaction().begin();
entityManager.persist(testEntity);
entityManager.getTransaction().commit();
}
persistence.xml
xml version="1.0" encoding="UTF-8"?>
properties>
persistence-unit>
persistence>
リンク先:http://ningnian169.blog.51cto.com/2417825/450042