问题描述
错误信息javax.persistence.PersistenceException:[PersistenceUnit:jpa]UnabletoconfigureEntityManagerFactoryatorg.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:374)atorg.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)atjavax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)atjavax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)atcom.yl.junitTest.JunitTest.save(JunitTest.java:18)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)atjava.lang.reflect.Method.invoke(Method.java:585)atorg.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)atorg.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)atorg.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)atorg.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)atorg.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)atorg.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)atorg.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)atorg.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)atorg.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)atorg.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)Causedby:org.hibernate.AnnotationException:Useof@OneToManyor@ManyToManytargetinganunmappedclass:com.yl.jpa.Orders.items[com.yl.jpa.OrderItems]atorg.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1168)atorg.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:693)atorg.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:628)atorg.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)atorg.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1686)atorg.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393)atorg.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)atorg.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)atorg.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)atorg.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)atorg.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:278)atorg.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:362)...24more实体类ORDERSpackagecom.yl.jpa;importjava.util.HashSet;importjava.util.Set;importjavax.persistence.CascadeType;importjavax.persistence.Entity;importjavax.persistence.FetchType;importjavax.persistence.Id;importjavax.persistence.OneToMany;@EntitypublicclassOrders{privateStringorderId;privatefloatamount=0f;privateSet<OrderItems>items=newHashSet<OrderItems>();@IdpublicStringgetOrderId(){returnorderId;}publicvoidsetOrderId(StringorderId){this.orderId=orderId;}publicfloatgetAmount(){returnamount;}publicvoidsetAmount(floatamount){this.amount=amount;}@OneToMany(cascade={CascadeType.REFRESH,CascadeType.MERGE,CascadeType.REMOVE,CascadeType.PERSIST},fetch=FetchType.LAZY,mappedBy="order")publicSet<OrderItems>getItems(){returnitems;}publicvoidsetItems(Set<OrderItems>items){this.items=items;}}实体类2ORDRSITEMSpackagecom.yl.jpa;importjavax.persistence.CascadeType;importjavax.persistence.Column;importjavax.persistence.FetchType;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.ManyToOne;publicclassOrderItems{privateIntegerid;privateStringprodouctName;privatefloatsellPrice=0f;privateOrdersorder;@Id@GeneratedValuepublicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}@Column(nullable=false,length=40)publicStringgetProdouctName(){returnprodouctName;}publicvoidsetProdouctName(StringprodouctName){this.prodouctName=prodouctName;}publicfloatgetSellPrice(){returnsellPrice;}publicvoidsetSellPrice(floatsellPrice){this.sellPrice=sellPrice;}@ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},fetch=FetchType.EAGER,optional=false)@JoinColumn(name="order_id")publicOrdersgetOrder(){returnorder;}publicvoidsetOrder(Ordersorder){this.order=order;}}persistence.xml配置文件<?xmlversion="1.0"?><persistencexmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"version="1.0"><persistence-unitname="jpa"transaction-type="RESOURCE_LOCAL"><provider>org.hibernate.ejb.HibernatePersistence</provider><properties><propertyname="hibernate.dialect"value="org.hibernate.dialect.MySQL5Dialect"/><propertyname="hibernate.connection.driver_class"value="org.gjt.mm.mysql.Driver"/><propertyname="hibernate.connection.username"value="root"/><propertyname="hibernate.connection.password"value="sa"/><propertyname="hibernate.connection.url"value="jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8"/><propertyname="hibernate.max_fetch_depth"value="3"/><propertyname="hibernate.hbm2ddl.auto"value="update"/><propertyname="hibernate.jdbc.fetch_size"value="18"/><propertyname="hibernate.jdbc.batch_size"value="10"/><propertyname="hibernate.show_sql"value="true"/><propertyname="hibernate.format_sql"value="false"/></properties></persistence-unit></persistence>测试生成表importjavax.persistence.EntityManagerFactory;importjavax.persistence.Persistence;importorg.junit.BeforeClass;importorg.junit.Test;publicclassJunitTest{@BeforeClasspublicstaticvoidsetUpBeforeClass()throwsException{}//测试@Testpublicvoidsave(){EntityManagerFactoryemf=Persistence.createEntityManagerFactory("jpa");emf.close();}}各位大侠小弟菜鸟刚接触JPA请大虾们多多指点小弟不胜感激谢谢了