我尝试在Java EE 7中创建实体.Hibernate版本-5.1.0,jta -4.0.4,jpa -2.1。
这是我的persistence.xml
<persistence-unit name="bookUnit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>src/BookEntity.xml</mapping-file>
<class>src.BookEntity</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test_db"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="admin"/>
<property name="hibernate.connection.password" value="admin"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
我尝试创建实体管理器工厂的JUnit文件:
@Test
public void Test(){
BookEntity book = new BookEntity();
book.setTitle("What is love?");
book.setDescription("What is Love? Answer: Read this book");
EntityManager em = Persistence.createEntityManagerFactory("bookUnit").createEntityManager();
em.getTransaction().begin();
em.persist(em);
em.getTransaction().commit();
em.flush();
assert em.contains(book);
em.close();
}
}
@Entity
public class BookEntity {
private long id;
private String title;
private Double price;
private String description;
@Id
@Column(name = "id", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "title", nullable = true, length = 255)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Basic
@Column(name = "description", nullable = true, length = 255)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BookEntity that = (BookEntity) o;
if (id != that.id) return false;
if (title != null ? !title.equals(that.title) : that.title != null) return false;
if (price != null ? !price.equals(that.price) : that.price != null) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (price != null ? price.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
return result;
}
BookEntity.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity class="src.BookEntity">
<table name="book" schema="public" catalog="test_db"/>
<attributes>
<id name="id">
<column name="id" precision="0"/>
</id>
<basic name="title">
<column name="title" nullable="false"/>
</basic>
<basic name="price">
<column name="price" nullable="false" precision="-1"/>
</basic>
<basic name="description">
<column name="description" nullable="false"/>
</basic>
</attributes>
</entity>
</entity-mappings>
我已经接受了这个例外:
javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:66)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:78)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at test.test.Test(test.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.RuntimeException: Trying to instanciate interface
javax.persistence.Table with unknown elements at org.hibernate.annotations.common.annotationfactory.AnnotationProxy.getAnnotationValues(AnnotationProxy.java:88)
at org.hibernate.annotations.common.annotationfactory.AnnotationProxy.<init>(AnnotationProxy.java:69)
at org.hibernate.annotations.common.annotationfactory.AnnotationFactory.create(AnnotationFactory.java:80)
at org.hibernate.annotations.common.annotationfactory.AnnotationFactory.create(AnnotationFactory.java:53)
at org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader.getTable(JPAOverriddenAnnotationReader.java:2780)
at org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader.initAnnotations(JPAOverriddenAnnotationReader.java:345)
at org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader.isAnnotationPresent(JPAOverriddenAnnotationReader.java:314)
at org.hibernate.annotations.common.reflection.java.JavaXAnnotatedElement.isAnnotationPresent(JavaXAnnotatedElement.java:60)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.categorizeAnnotatedClass(AnnotationMetadataSourceProcessorImpl.java:115)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.<init>(AnnotationMetadataSourceProcessorImpl.java:104)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.<init>(MetadataBuildingProcess.java:147)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:876)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
... 30 more
我已经搜索了此异常的解决方案,但解决方案并不能帮助我