1
votes

I'm new to spring so I don't know if everything is configured correctly. Though I use the spring IDE and a unittest provided to be correct.

I'm having a User class

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

I want to keep this class persistent with a DB. I use hsqldb to send queries to my db and so on.

Now I have a DAO layer for my user which looks like this :

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>
1
Can you post the error you get when you launch the Unit Test?. Do you have any other config file (like persistence.xml) in your project? - Benoit Wickramarachi
I have no error, it just seems that never something is inserted in the db - Jeremy Knees
Ok, can you add the definition of your dataSource bean. Thx - Benoit Wickramarachi
I don't have a dataSource bean. Where should I define it? - Jeremy Knees
sorry, found it. I edited my original post - Jeremy Knees

1 Answers

1
votes

You have to define a DataSource bean like this:

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

(Or a JNDI lookup)

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

You can follow this tutorial to help you setting everything up.