0

我想创建一个基于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>

它完美地生成了一个Entity文件和一个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)持久性.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>
4

1 回答 1

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>

当您将代码部署到成熟的应用服务器时,您的代码可能会出现问题,并且您可能会遇到 Spring 和 EJB 容器争夺 bean 控制权的问题。hibernate 插件生成@Stateless会话 bean,通常会被应用程序拾取。服务器(取决于您使用哪一个)。

于 2014-02-10T07:19:04.880 回答