這是第二天,我正在努力解決這個問題。 我們有一個使用spring和hibernate的web應用程序。現在我們正試圖向命令行公開一些功能。所以我們應該能夠從命令行運行一個類(沒有web應用程序),所以它應該可以訪問spring和hibernate。 Spring Entitymanager java.lang.ClassFormatError
<context:annotation-config/>
<context:component-scan base-package="com.x.y.z"/>
<jpa:repositories base-package="com.x.y" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="jpaProperties">
<props>
<prop key="javax.persistence.validation.mode">NONE</prop>
<prop key="hibernate.validator.apply_to_ddl">false</prop>
<prop key="hibernate.validator.autoregister_listeners">false</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="SQL_SERVER" />
<property name="showSql" value="false" />
</bean>
</property>
</bean>
這裏我使用LocalEntityManagerFactoryBean但似乎沒有辦法來設置數據源吧。它從persistance.xml
如果我使用LocalContainerEntityManagerFactoryBean我可以設置數據源(它有DataSource屬性)
persistance.xml:
<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="oms-jpa">
<class>com.domain.User</class>
</persistence-unit>
在persistance.xml我不是定義數據源的任何數據庫信息,所以LocalEntityManagerFactoryBean不適用於此。
什麼我得到:
Error creating bean with name 'entityManagerFactory' defined in file [C:\Users\ekamoliddinov\IdeaProjects\web\conf\spring-config.xml]: Invocation of init method failed; nested exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
一派則有些說:
- 版本的jar搞砸
- JEE-api.jar文件丟失
但是我有JEE-api.jar文件。
我的問題:
- 什麼導致上述異常?
- 我可以在沒有網頁 容器的情況下使用
LocalContainerEntityManagerFactoryBean
嗎? Spec對此沒有任何說明。 - 如果我不能使用
LocalContainerEntityManagerFactoryBean
那麼我該如何設置 LocalEntityManagerFactoryBean因此它應該使用我的數據源而不是 persistance.xml(我無法在persistance.xml中定義任何東西)。
我使用Spring版本3.1.3休眠4.1.8.Final,我在命令行(我也沒有有JEE環境)
對不起不止一個問題,但所有問題都來自我認爲的同一根。
任何幫助將不勝感激。 謝謝。