2014-02-09 28 views
0

我想創建一個基於的Java應用程序Hibernate-3Spring Framework。爲了簡化流程,我發現hibernate3-maven-plugin能夠對現有數據庫執行反向工程。
這裏有例如POM的:如何在由Hibernate生成的DAO中插入@PersistenceContext 3-maven-plugin

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>hibernate3-maven-plugin</artifactId> 
    <version>2.2</version> 
    <configuration> 
     <components> 
      <component> 
       <name>hbm2java</name> 
       <outputDirectory>src/main/java</outputDirectory> 
       <implementation>jdbcconfiguration</implementation> 
      </component> 
      <component> 
       <name>hbm2dao</name> 
       <outputDirectory>src/main/java</outputDirectory> 
       <implementation>jdbcconfiguration</implementation> 
      </component> 
     </components> 
     <componentProperties> 
      <revengfile>/src/main/resources/model.reveng.xml</revengfile> 
      <propertyfile>/src/main/resources/hibernate.properties</propertyfile> 
      <jdk5>true</jdk5> 
      <ejb3>true</ejb3> 
     </componentProperties> 
    </configuration> 
    <dependencies> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.18</version> 
     </dependency> 
     <dependency> 
      <groupId>cglib</groupId> 
      <artifactId>cglib-nodep</artifactId> 
      <version>2.1_3</version> 
     </dependency> 
    </dependencies> 
    <executions> 
     <execution> 
      <phase>compile</phase> 
      <goals> 
       <goal>hbm2java</goal> 
       <goal>hbm2dao</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

然後,我建立在Spring上下文:

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
      <property name="persistenceUnitName" value="gomer" /> 
     </bean> 

     <bean id="entityManager" factory-bean="entityManagerFactory" factory-method="createEntityManager"/> 

     <bean id="user" class="ru.tomtrix.first.db.UserHome"> 
      <property name="entityManager" ref="entityManager"/> 
     </bean> 
    </beans> 

它完美地生成一個實體文件,除了以下一個DAO文件。在DAO文件中有一個EntityManager:

@Stateless 
public class UserHome { 

    private static final Log log = LogFactory.getLog(UserHome.class); 

    @PersistenceContext private EntityManager entityManager; 

...並且該字段沒有setter!最終Spring拋出異常:

Invalid property 'entityManager' of bean class [ru.tomtrix.first.db.UserHome]: Bean property 'entityManager' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 

當然,手動編寫setter並不是一個好習慣。我認爲有一種方法可以正確地注入經理。 那麼如何做到這一點,而不重寫生成的文件?

通訊信息:
1)我想創建一個獨立的應用程序(也可能是如Tomcat應用服務器運行)
2)model.reveng.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://www.hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd"> 
<hibernate-reverse-engineering> 
    <table-filter match-name=".*" package="ru.tomtrix.first.db"/> 
</hibernate-reverse-engineering> 

3)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="gomer" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
     <class>User</class> 
     <properties> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> 
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
      <property name="hibernate.show_sql" value="true"/> 
      <property name="hibernate.connection.username" value="root"/> 
      <property name="hibernate.connection.password" value="1234"/> 
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/users"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

回答

0

問題是您缺少一部分配置。你需要告訴Spring你想(也)使用註解來進行配置。爲此,將<context:annotation-config />添加到您的配置中,並刪除entityManager

的設置旁邊的那個刪除工廠方法的調用spring將處理所有爲你。

添加提示使用的

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:annotation-config /> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="gomer" /> 
    </bean> 

    <bean id="user" class="ru.tomtrix.first.db.UserHome" /> 

</beans> 

你的代碼的版本少架構INSEAD可能是有問題的,當你把它部署到茂盛的應用服務器,你可能會遇到的問題與Spring和EJB容器在控制競爭的豆子。休眠插件生成@Stateless會話bean,通常會被應用程序拾取。服務器(取決於你使用哪一個)。

+0

該配置看起來好多了,可以正常工作,但應用程序會拋出異常:「**由...引起:java.lang.NoSuchMethodError:org.springframework.beans.factory.annotation.InjectionMetadata:方法()V未找到** 「在該行:'BeanFactory工廠=新的ClassPathXmlApplicationContext(」trix.xml「);'我的方法得到一個上下文錯誤? –

+0

哦......原來,** Spring.context **和** Spring.orm **在POM中有不同的版本。所以問題幾乎解決了。你能解釋一下如何處理交易嗎?據我瞭解,DAO的方法必須與@Transactional一起提供,但它們仍然是自動生成的! –

+0

我終於在文檔[14.7交易管理]中找到答案(http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch14s07.html)謝謝M. Deinum –