簡體   English   中英

如何在Hibernate3-maven-plugin生成的DAO中注入@PersistenceContext

[英]How to inject @PersistenceContext in DAO generated by Hibernate3-maven-plugin

我想基於Hibernate-3Spring Framework創建一個Java應用程序。 為了簡化該過程,我找到了hibernate3-maven-plugin ,它可以對現有數據庫進行反向工程。
這是POM的示例:

<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;  

...而且這個領域還沒有二傳手! 最終,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>

問題是您缺少配置的一部分。 您需要告訴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>

將代碼部署到功能完善的應用服務器時,代碼可能會出現問題,並且Spring和EJB容器爭奪Bean控制權時可能會遇到問題。 休眠插件生成@Stateless會話bean,通常將由應用程序拾取。 服務器(取決於您使用的服務器)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM