2014-02-09 24 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 –