在做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-INF \persistence.xml,有没有声明你的entity
最后发现自己,在persistence.xml没有加下面这句导致了Jpa Unknown entity:
- <class>com.entity.TestEntity</class>
这是我的最后测试成功的例子
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"?>
- <persistence version="1.0" 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_1_0.xsd">
- <persistence-unit name="JpaTest" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <class>com.entity.TestEntity</class>
- <exclude-unlisted-classes>true</exclude-unlisted-classes>
- <properties>
- <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
- <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
- <property name="hibernate.connection.username" value="ningnian"/>
- <property name="hibernate.connection.password" value="ningningx"/>
- <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
- <property name="hibernate.show_sql" value="true" />
- <property name="hibernate.hbm2ddl.auto" value="update"/>
- </properties>
- </persistence-unit>
- </persistence>
- 链接地址:http://ningnian169.blog.51cto.com/2417825/450042