Hello,
I am unsure if this is a Spring issue or a JPA issue. I am working on
a fairly simple component of an App. I am trying to create users and
roles. The user object has a many-to-many relationship to the roles.
->
======
@Entity
@Table(name="htt_users", schema = "htt")
public class HttUser {
...
@ManyToMany(cascade=CascadeType.ALL)
private List<HttRole> roles;
======
@Entity
@Table(name="htt_roles", schema = "htt")
public class HttRole {
...
@ManyToMany(mappedBy="roles")
private List<HttUser> members;
I have created these classes with simple unit tests including the following -
List<HttRole> roles = new ArrayList<HttRole>();
roles.add(role);
user.setRoles(roles);
...
assertTrue(foundUser.getRoles() != null );
The unit test runs without complaining. I have also checked that the
schema is created correctly and there is also legitimate data within
the three tables (htt_users, htt_roles, htt_users_htt_roles).
I am trying to wire in some quick DAO classes using Spring. Here is my
applicationContext.xml -
<?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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
/>
<bean id="manageUsers" scope="prototype"
class="local.httlima.maint.admin.ManageUsers" >
<property name="httUserDao" ref="httUserDao" />
<property name="httRoleDao" ref="httRoleDao" />
</bean>
<bean id="manageRoles" scope="prototype"
class="local.httlima.maint.admin.ManageRoles" >
<property name="httRoleDao" ref="httRoleDao" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/htt" />
<property name="username" value="..." />
<property name="password" value="..." />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="httUserDao"
class="com.wantii.htt.px.dao.impl.HttUserDAOImplJPA" />
<bean id="httRoleDao"
class="com.wantii.htt.px.dao.impl.HttRoleDAOImplJPA" />
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
Everything appears to be working correctly until I get to the
following section in another part of the code -
HttUser user = userDao.find(username);
...
List<HttRole> selectedRoles = user.getRoles();
List<String> selectedRoleNames = new ArrayList<String>();
Iterator<HttRole> iter = selectedRoles.iterator();
while (iter.hasNext() ) {
selectedRoleNames.add(iter.next().getRoleName());
}
this.setRoleNames(selectedRoleNames);
I get a NullPointerException each time it gets to user.getRoles(). I
tried creating a getRoles(username) within the DAO in case the call
needed to be within a transaction, but I got an NPE there as well. Am
I missing something that would allow the roles to be passed back?
Thanks for your help!
-Wes
--
Wesley Wannemacher
President, Head Engineer/Consultant
WanTii, Inc.
http://www.wantii.com