-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Accessing collections with OneToMany Bidirectional Mapping
PostPosted: Tue Oct 04, 2011 11:54 am 
Newbie

Joined: Tue Oct 04, 2011 11:06 am
Posts: 1
Please forgive me for posting this 'beaten to death' problem. I literally tried tens of solutions posted here as well as the Spring/Hibernate forums to no avail. I have a feeling that I am not wiring the beans properly as none of the solutions I tried worked.

My problem is, after loading the NFSExports class (which defines a collection NFSClient and is on the One side of the association), I am not able to get to the collection. I almost always get the "org.hibernate.collection.PersistentBag cannot be cast to java.util.ArrayList" exception or the "Failed to lazyly initialize collection" exception. I have a test class that calls the getAllNFSExports method on the NFSExportsDaoImpl class. Please note that I retained a few of the 'tricks' I tried in this method within comments. I put the line(s) of code where I get the exception in red

This is only half of my problem - but the important half. The other half, though not relevant in this forum is to create object graphs (without circular references) for JSON. Any advise is greatly appreciated.

Thank you very much.

- Sharma
I am using the following versions of relevant jars
hibernate-annotations-3.5.1-Final.jar
hibernate-commons-annotations-3.2.0.Final.jar
hibernate-core-3.5.1-Final.jar
hibernate-ehcache-3.5.1-Final.jar
hibernate-entitymanager-3.5.1-Final.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
spring-aop-3.1.0.M2.jar
spring-expression-3.1.0.M2.jar
spring-asm-3.1.0.M2.jar
spring-jdbc-3.1.0.M2.jar
spring-beans-3.1.0.M2.jar
spring-orm-3.1.0.M2.jar
spring-context-3.1.0.M2.jar
spring-tx-3.1.0.M2.jar
spring-context-support-3.1.0.M2.jar
spring-web-3.1.0.M2.jar
spring-core-3.1.0.M2.jar



I have two simple classes NFSExport and NFSClient. Here are the code snippets.


@javax.persistence.Entity
@Table(name = "NFSEXPORTS")
public class NFSExport implements Serializable {
private Integer id; // integer
private String nfsVolName; // varchar 256
private String exportPath; // varchar 256
private List<NFSClient> nfsClients = new ArrayList<NFSClient>();

@Id
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

@Column(name = "nfsvolname", nullable = false)
public String getNfsVolName() {
return nfsVolName;
}

public void setNfsVolName(String nfsVolName) {
this.nfsVolName = nfsVolName;
}

@Column(name = "exportpath", nullable = false)
public String getExportPath() {
return exportPath;
}

public void setExportPath(String exportPath) {
this.exportPath = exportPath;
}

@OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="nfsexports_id")
public List<NFSClient> getNFSClients() {
return this.nfsClients;
}

public void setNFSClients(List<NFSClient> nfscs) { this.nfsClients = nfscs;}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@javax.persistence.Entity
@Table(name = "NFSCLIENTS")
public class NFSClient implements Serializable {
private Integer id;
private String clientName;
private NFSExport nfsExport;


public NFSClient() { }

@Id
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}

@Column(name = "clientname", nullable = false)
public String getClientName() {return clientName;}
public void setClientName(String clientName) {this.clientName = clientName;}


@ManyToOne
@JoinColumn(name="nfsexports_id", nullable=false,insertable=false, updatable=false)
public NFSExport getNFSExport() {return nfsExport;}
public void setNFSExport(NFSExport nfsExport) {this.nfsExport = nfsExport;}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class NFSExportDaoImpl extends HibernateDaoSupport implements NFSExportDao {
private final String GET_ALL_NFSEXPORTS = "SELECT DISTINCT A.*, B.* FROM NFSEXPORTS A, NFSCLIENTS B WHERE B.NFSEXPORTS_ID = A.ID ORDER BY A.NFSVOLNAME, B.CLIENTNAME";

@Override
public Class<NFSExport> getEntityClass() {
return NFSExport.class;
}

public ArrayList<NFSExport> getAllNFSExports() {
long startTime = Calendar.getInstance().getTimeInMillis();
ArrayList<NFSExport> nfsvs = null;
List<NFSExport> exps = new ArrayList<NFSExport>();

/*
NFSExport exp;
List<Entity> entities = super.getAllEntities();
if (entities != null && entities.size() > 0) {
if (nfsvs == null) {
nfsvs = new ArrayList<NFSExport>();
}
for (Entity e : entities) {
exp = (NFSExport) e;
nfsvs.add(exp);
}
}
*/
ArrayList<NFSClient> cli;
SessionFactory sf = getSessionFactory();
Session s = sf.getCurrentSession();
Query q = s.createSQLQuery(GET_ALL_NFSEXPORTS).addEntity(NFSExport.class);
if(q.list().size() > 0){
exps.addAll(q.list());
}

boolean isOpen = s.isOpen();
boolean isDirty = s.isDirty();
boolean isConnected = s.isConnected();

if(exps != null && exps.size() > 0){
for(NFSExport exp : exps){
s.buildLockRequest(LockOptions.NONE).lock(exp);
try{
exp.getNFSClients().size();
}catch (Exception e){
e.printStackTrace();
}
cli = (ArrayList<NFSClient>) exp.getNFSClients();

if(cli != null){
cli.size();
exp.setNFSClients(cli);
}
nfsvs.add(exp);
}
}
/*
Session sess = getSession();
ArrayList<NFSClient> cli;
Query query = sess.createSQLQuery(GET_ALL_NFSEXPORTS).addEntity(NFSExport.class);
if(query.list().size() > 0){
exps.addAll(query.list());
}

boolean isOpen = sess.isOpen();
boolean isDirty = sess.isDirty();
boolean isConnected = sess.isConnected();

if(exps != null && exps.size() > 0){
for(NFSExport exp : exps){
sess.buildLockRequest(LockOptions.NONE).lock(exp);
try{
exp.getNFSClients().size();
}catch (Exception e){
e.printStackTrace();
}
cli = (ArrayList<NFSClient>) exp.getNFSClients();

if(cli != null){
cli.size();
exp.setNFSClients(cli);
}
nfsvs.add(exp);
}
}
*/

/*
List<Entity> entities = getHibernateTemplate().loadAll(NFSExport.class);
if(entities != null && entities.size() > 0){
if (nfsvs == null) {
nfsvs = new ArrayList<NFSExport>();
}
for(Entity e : entities){
exp = (NFSExport) e;
sess.buildLockRequest(LockOptions.READ).lock(exp);
cli = (ArrayList<NFSClient>) exp.getNFSClients();

if(cli != null){
cli.size();
exp.setNFSClients(cli);
}
ret.add(exp);
}
}
*/
return nfsvs;
}

}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Here is my ApplicationContext-dao.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/b ... ns-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/t ... tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/a ... op-3.1.xsd">

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="primaryDB"/>
</property>
<property name="configLocation">
<value>WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
</props>
</property>

</bean>

<!-- enable the configuration of transactional behavior based on annotations-->
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="txAttributeSource"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="send*">PROPAGATION_REQUIRED, readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
</props>
</property>
</bean>

<bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>

<property name="transactionAttributeSource">
<ref local="txAttributeSource"/>
</property>
</bean>

<bean id="autoProxyCreator"
class="com.sep.sms.common.SepAutoProxyCreator">
<property name="interceptorNames">
<list>
<idref local="txInterceptor"/>
</list>
</property>
<property name="beanNames">
<list>
<value>*Dao</value>
<value>*DAO</value>
</list>
</property>
</bean>

<bean id="daoTmpl" abstract="true">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>


<!-- custom beans -->
<bean id="GDVDao" class="com.sep.sms.domain.dao.impl.GDVDaoImpl"
parent="daoTmpl" lazy-init="default" autowire="default">
</bean>
<bean id="NFSExportDao" class="com.sep.sms.domain.dao.impl.NFSExportDaoImpl"
parent="daoTmpl" lazy-init="default" autowire="default">
</bean>
<bean id="NFSClientDao" class="com.sep.sms.domain.dao.impl.NFSClientDaoImpl"
parent="daoTmpl" lazy-init="default" autowire="default">
</bean>
</beans>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Here is my web.xml file

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>RESTful API</display-name>
<context-param>
<param-name>resteasy.media.type.mappings</param-name>
<param-value>json : application/json, xml : application/atom+xml, atom : application/atom+xml</param-value>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-dao.xml
/WEB-INF/applicationContext-controllers.xml
</param-value>
</context-param>



<!-- binds a JPA EntityManager to the thread for the entire processing of the request
<filter>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

Map the EntityManager Filter to all requests
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/v1/*</url-pattern>
</filter-mapping>
-->

<!--
- Loads the root application context of this web app at startup.
- The application context is then available via
- WebApplicationContextUtils.getWebApplicationContext(servletContext).
-->

<!-- replacing this with the SpringContextLoaderListener provided by JBOSS. - Sharma 09/27/2011
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
-->

<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap </listener-class>
</listener>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.sepaton.common.SMSApplication</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>

</web-app>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.