1

我是 spring 新手,所以我不知道所有配置是否正确。虽然我使用了 spring IDE 和提供的单元测试是正确的。

我有一个用户类

@Entity
@NamedQuery(name="findUser4Email",
query="SELECT * " +
      "FROM User " +
      "WHERE email = :userEmail")
public class User extends AbstractNamedDomain {

private String name;
private String email;
private String password;
...

我想通过数据库保持这个类的持久性。我使用 hsqldb 将查询发送到我的数据库等等。

现在我的用户有一个 DAO 层,如下所示:

public class UserManager implements IUserManager {

    @PersistenceContext 
    private EntityManager em;

public User findUser4Email(String email) {
    return (User) em.createNamedQuery("findUser4Email").setParameter("userEmail",
            email).getSingleResult();
}

public User storeUser(User user) {
    //User u = em.merge(user);
    em.persist(user); 
    return user;
}
...

my beans look like this :

  <?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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="persistenceUnitName" value="BankingWeb" />
            <property name="jpaVendorAdapter">
                    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                            <property name="generateDdl" value="true" />
                            <property name="showSql" value="true" />
                            <property name="databasePlatform" value="${hibernate.dialect}" />
                    </bean>
            </property>
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean name="AccountManager" class="ssel.banking.dao.jpa.AccountManager" />

    <bean name="UserManager" class="ssel.banking.dao.jpa.UserManager" />


    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="txManager"/>

    </beans>

When I try to store a User in a unittest (just by calling the store(User) from the dao the test fails. Nor do I see any table created in my DB when I look at it with hsqldb. What is wrong my code ? Or what do I miss?

this is what my datasourcebean looks like

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url" value="${jdbc.url}"/>
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
4

1 回答 1

1

您必须像这样定义一个 DataSource bean:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://192.0.0.1:3306/yourDB"/>
    <property name="username" value=""/>
    <property name="password" value=""/>
</bean>

(或 JNDI 查找)

<jee:jndi-lookup jndi-name="jdbc/datasource" id="dataSource" />

您可以按照本教程来帮助您设置所有内容。

于 2012-12-11T19:49:11.423 回答