我定义了一个实体,而hibernate是我的提供者。这是persistence.xml:
<persistence-unit name="ordersUnit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>com/jj/entityZone/OrdersViewEntity.xml</mapping-file>
<class>com.jj.entityZone.OrdersViewEntity</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:sqlserver://localhost\MSSQLSERVER;databaseName=JbDB"/>
<property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="hibernate.connection.username" value="xxx"/>
<property name="hibernate.connection.password" value="xxx"/>
<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="update"/>
</properties>
</persistence-unit>
和hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost\MSSQLSERVER;databaseName=JbDB</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.username">uuu</property>
<property name="hibernate.connection.password">xxx</property>
</session-factory>
这是java代码(在单例bean中使用):
SessionFactory sessionFactory =
new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("ordersUnit");
EntityManager entitymanager = emfactory.createEntityManager();
entitymanager.getTransaction().begin();
OrdersViewEntity ov = new OrdersViewEntity();
Query query = entitymanager.createNativeQuery("SELECT * FROM ORDERS_VIEW ", OrdersViewEntity.class);
List<OrdersViewEntity> list = query.getResultList();
for (OrdersViewEntity n : list) {
System.out.println("OV:" + n.getIdQueue());
System.out.println("OV:" + n.getIdCnt());
}
entitymanager.getTransaction().commit();
System.out.println("Size : " + list.size());
entitymanager.close();
emfactory.close();
但我得到错误:
javax.ejb.EJBException
at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:750)
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:700)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:505)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4542)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2021)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1992)
at com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:4029)
at com.sun.ejb.containers.EJBTimerService.deliverTimeout(EJBTimerService.java:1224)
at com.sun.ejb.containers.EJBTimerService.access$000(EJBTimerService.java:92)
at com.sun.ejb.containers.EJBTimerService$TaskExpiredWork.run(EJBTimerService.java:1954)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: ordersUnit] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:967)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.jabolsa.jbeans.PlayBoardBean.startReadOrders(PlayBoardBean.java:30)
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.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4762)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:834)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:73)
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
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.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:895)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:834)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundTimeout(SystemInterceptorProxy.java:145)
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.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:895)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:834)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:374)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4734)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4722)
at com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:4000)
... 8 more
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.jabolsa.entityZone.LocationsQuarterEntity column: id_province (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:835)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:853)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:875)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:607)
at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:451)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
... 43 more
]]
QUIZ 1:可以吗?
测验2:如果我想使用JNDI我该怎么办?如何定义?
QUIZ 3:为什么会出现此异常&#34;引起:javax.persistence.PersistenceException:[PersistenceUnit:ordersUnit]无法构建Hibernate SessionFactory&#34;抛出?
我正在使用intellij和glassfish(Payara)。如何制作SessionFactoy并与实体管理器一起使用?