Announcement Announcement Module
Collapse
No announcement yet.
Spring one to many association not working - nested loop throws exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring one to many association not working - nested loop throws exception

    Hi guys!

    This is gonna to be my first post on this forum - so welcome.

    In my project I use Spring 3.0.0.RELEASE and Hibernate 3.6.10. I also use JDK7 and PostgreSQL 9.1.9.

    Heres goes the dependencies from pom.xml file

    Code:
     <dependencies>
    
          <dependency>
              <groupId>postgresql</groupId>
              <artifactId>postgresql</artifactId>
              <version>9.1-901.jdbc4</version>
          </dependency>
     
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-core</artifactId>
    			<version>3.6.10.Final</version>
    		</dependency>
    		
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.8.2</version>
    			<scope>test</scope>
    		</dependency>
    		
    		<dependency>
    			<groupId>javassist</groupId>
    			<artifactId>javassist</artifactId>
    			<version>3.12.1.GA</version>
    		</dependency>
    		
    		<!--  spring dependencies -->
    		
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    			<version>3.0.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>3.0.0.RELEASE</version>
    		</dependency>
    		
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-orm</artifactId>
    			<version>3.0.0.RELEASE</version>
    		</dependency>
    		
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>3.0.0.RELEASE</version>
    		</dependency>
    		
    		<!-- Basic datasource in servlet-context -->
    		<dependency>
    			<groupId>commons-dbcp</groupId>
    			<artifactId>commons-dbcp</artifactId>
    			<version>20030825.184428</version>
    		</dependency>
    
    		<dependency>
    			<groupId>commons-pool</groupId>
    			<artifactId>commons-pool</artifactId>
    			<version>20030825.183949</version>
    		</dependency>
    		
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>servlet-api</artifactId>
    			<version>2.5</version>
                <scope>provided</scope>
    		</dependency>
    		
    		<dependency>
    			<groupId>taglibs</groupId>
    			<artifactId>standard</artifactId>
    			<version>1.1.2</version>
    		</dependency>
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>jstl</artifactId>
    			<version>1.1.2</version>
    		</dependency>
      </dependencies>

    I have following DB schema:

    Code:
    CREATE TABLE applications (
        id serial PRIMARY KEY,
        name character varying(255),
        ip character varying(255),
        port integer,
        alive boolean
    );
    
    CREATE TABLE application_states (
        id serial PRIMARY KEY,
        cpu_percentage_use integer,
        disk_write_kb integer,
        disk_read_kb integer,
        alive boolean,
        thread_count integer,
        memory_percentage_use integer,
        created_at timestamp without time zone NOT NULL,
        name character varying(255),
        application_id integer REFERENCES applications(id)
    );

    When I want to use nested forEach loop in JSP view I get exception.

    JSP:

    Code:
        <c:forEach items="${applications}" var="application">
        {
            "id":${application.id},
            "name":"${application.name}",
                "ip":"${application.ip}",
                "port":${application.port},
            "alive":${application.alive}
            "other":{
            <c:forEach var="applicationState" items="${application.applicationStates}">
            "id":"${applicationState.id}"
            </c:forEach>
        }
        },
        </c:forEach>
    Last edited by Hitsu; Jun 8th, 2013, 11:09 AM.

  • #2
    Now I will paste code from my project, so anyone can help me with debug (sorry for double-post but I want to post more info about that exception, right know I have limit to 10000 chars in one post)

    MODELS / ENTITIES

    ApplicationEntity.java
    Code:
    package com.codeyard.model;
    
    import javax.persistence.*;
    import java.util.HashSet;
    import java.util.Set;
    
    @Entity
    @Table(name = "applications", schema = "public")
    @javax.persistence.SequenceGenerator(
            name="SEQ_STORE",
            allocationSize=1,
            sequenceName="applications_id_seq"
    )
    public class ApplicationEntity {
        private int id;
    
        @Column(name = "id")
        @Id
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        private String name;
    
        @Column(name = "name")
        @Basic
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        private String ip;
    
        @Column(name = "ip")
        @Basic
        public String getIp() {
            return ip;
        }
    
        public void setIp(String ip) {
            this.ip = ip;
        }
    
        private Integer port;
    
        @Column(name = "port")
        @Basic
        public Integer getPort() {
            return port;
        }
    
        public void setPort(Integer port) {
            this.port = port;
        }
    
        private Boolean alive;
    
        @Column(name = "alive")
        @Basic
        public Boolean getAlive() {
            return alive;
        }
    
        public void setAlive(Boolean alive) {
            this.alive = alive;
        }
    
    
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "applications", fetch = FetchType.EAGER,
                targetEntity = ApplicationStateEntity.class)
        private Set<ApplicationStateEntity> applicationStates;
    
        public Set<ApplicationStateEntity> getApplicationStates() {
            return this.applicationStates;
        }
    
        public void setApplicationStates(Set<ApplicationStateEntity> applicationStates) {
            this.applicationStates = applicationStates;
        }
    
    
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            ApplicationEntity that = (ApplicationEntity) o;
    
            if (id != that.id) return false;
            if (alive != null ? !alive.equals(that.alive) : that.alive != null) return false;
            if (ip != null ? !ip.equals(that.ip) : that.ip != null) return false;
            if (name != null ? !name.equals(that.name) : that.name != null) return false;
            if (port != null ? !port.equals(that.port) : that.port != null) return false;
    
            return true;
        }
    
        @Override
        public int hashCode() {
            int result = id;
            result = 31 * result + (name != null ? name.hashCode() : 0);
            result = 31 * result + (ip != null ? ip.hashCode() : 0);
            result = 31 * result + (port != null ? port.hashCode() : 0);
            result = 31 * result + (alive != null ? alive.hashCode() : 0);
            return result;
        }
    }
    ApplicationStateEntity.java

    Code:
    package com.codeyard.model;
    
    import javax.persistence.*;
    import java.sql.Timestamp;
    
    
    @Entity
    @Table(name = "application_states", schema = "public")
    public class ApplicationStateEntity {
        private int id;
    
        @Column(name = "id")
        @Id
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        private Integer cpuPercentageUse;
    
        @Column(name = "cpu_percentage_use")
        @Basic
        public Integer getCpuPercentageUse() {
            return cpuPercentageUse;
        }
    
        public void setCpuPercentageUse(Integer cpuPercentageUse) {
            this.cpuPercentageUse = cpuPercentageUse;
        }
    
        private Integer diskWriteKb;
    
        @Column(name = "disk_write_kb")
        @Basic
        public Integer getDiskWriteKb() {
            return diskWriteKb;
        }
    
        public void setDiskWriteKb(Integer diskWriteKb) {
            this.diskWriteKb = diskWriteKb;
        }
    
        private Integer diskReadKb;
    
        @Column(name = "disk_read_kb")
        @Basic
        public Integer getDiskReadKb() {
            return diskReadKb;
        }
    
        public void setDiskReadKb(Integer diskReadKb) {
            this.diskReadKb = diskReadKb;
        }
    
        private Boolean alive;
    
        @Column(name = "alive")
        @Basic
        public Boolean getAlive() {
            return alive;
        }
    
        public void setAlive(Boolean alive) {
            this.alive = alive;
        }
    
        private Integer threadCount;
    
        @Column(name = "thread_count")
        @Basic
        public Integer getThreadCount() {
            return threadCount;
        }
    
        public void setThreadCount(Integer threadCount) {
            this.threadCount = threadCount;
        }
    
        private Integer memoryPercentageUse;
    
        @Column(name = "memory_percentage_use")
        @Basic
        public Integer getMemoryPercentageUse() {
            return memoryPercentageUse;
        }
    
        public void setMemoryPercentageUse(Integer memoryPercentageUse) {
            this.memoryPercentageUse = memoryPercentageUse;
        }
    
        private Timestamp createdAt;
    
        @Column(name = "created_at")
        @Basic
        public Timestamp getCreatedAt() {
            return createdAt;
        }
    
        public void setCreatedAt(Timestamp createdAt) {
            this.createdAt = createdAt;
        }
    
        private String name;
    
        @Column(name = "name")
        @Basic
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @ManyToOne
        @JoinColumn(name="application_id", insertable=false, updatable=false, nullable=false)
        private ApplicationEntity application;
    
        public ApplicationEntity getApplication() {
            return application;
        }
    
        public void setApplication(ApplicationEntity application) {
            this.application = application;
        }
    
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            ApplicationStateEntity that = (ApplicationStateEntity) o;
    
            if (id != that.id) return false;
            if (alive != null ? !alive.equals(that.alive) : that.alive != null) return false;
            if (cpuPercentageUse != null ? !cpuPercentageUse.equals(that.cpuPercentageUse) : that.cpuPercentageUse != null)
                return false;
            if (createdAt != null ? !createdAt.equals(that.createdAt) : that.createdAt != null) return false;
            if (diskReadKb != null ? !diskReadKb.equals(that.diskReadKb) : that.diskReadKb != null) return false;
            if (diskWriteKb != null ? !diskWriteKb.equals(that.diskWriteKb) : that.diskWriteKb != null) return false;
            if (memoryPercentageUse != null ? !memoryPercentageUse.equals(that.memoryPercentageUse) : that.memoryPercentageUse != null)
                return false;
            if (name != null ? !name.equals(that.name) : that.name != null) return false;
            if (threadCount != null ? !threadCount.equals(that.threadCount) : that.threadCount != null) return false;
    
            return true;
        }
    
        @Override
        public int hashCode() {
            int result = id;
            result = 31 * result + (cpuPercentageUse != null ? cpuPercentageUse.hashCode() : 0);
            result = 31 * result + (diskWriteKb != null ? diskWriteKb.hashCode() : 0);
            result = 31 * result + (diskReadKb != null ? diskReadKb.hashCode() : 0);
            result = 31 * result + (alive != null ? alive.hashCode() : 0);
            result = 31 * result + (threadCount != null ? threadCount.hashCode() : 0);
            result = 31 * result + (memoryPercentageUse != null ? memoryPercentageUse.hashCode() : 0);
            result = 31 * result + (createdAt != null ? createdAt.hashCode() : 0);
            result = 31 * result + (name != null ? name.hashCode() : 0);
            return result;
        }
    
    
    }

    DAO

    ApplicationDao.java
    Code:
    package com.codeyard.dao;
    
    import java.util.List;
    import com.codeyard.model.ApplicationEntity;
    
    public interface ApplicationDao {
    
        public void createApplication(ApplicationEntity application);
        public List<ApplicationEntity> getAllApplications();
        public void deleteApplication(Integer id);
    
    }
    ApplicationDaoImpl.java

    Code:
    package com.codeyard.dao;
    
    import java.util.List;
    
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    import com.codeyard.model.ApplicationEntity;
    
    @Repository
    public class ApplicationDaoImpl implements ApplicationDao {
    
        @Autowired
        private SessionFactory sessionFactory;
    
        public void createApplication(ApplicationEntity application) {
            sessionFactory.getCurrentSession().save(application);
        }
    
        public List<ApplicationEntity> getAllApplications() {
            return sessionFactory.getCurrentSession().
                    createQuery("FROM ApplicationEntity application LEFT OUTER JOIN application.applicationStates as applicationStates").list();    //").list(); //"
            // LEFT JOIN FETCH  p.creditCards WHERE p.id="+id
        }
    
        public void deleteApplication(Integer id) {
            ApplicationEntity applicationEntity = (ApplicationEntity) sessionFactory.getCurrentSession()
                    .load(ApplicationEntity.class, id);
            if (null != applicationEntity) {
                sessionFactory.getCurrentSession().delete(applicationEntity);
            }
    
        }
    
    }

    Comment


    • #3
      And last part...



      SERVICE

      ApplicationService.java

      Code:
      package com.codeyard.service;
      
      import com.codeyard.model.ApplicationEntity;
      import java.util.List;
      
      public interface ApplicationService {
      
          public void createApplication(ApplicationEntity application);
          public List<ApplicationEntity> getAllApplications();
          public void deleteApplication(Integer id);
      
      }
      ApplicationServiceImpl.java

      Code:
      package com.codeyard.service;
      
      import com.codeyard.dao.ApplicationDao;
      import com.codeyard.model.ApplicationEntity;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      import org.springframework.transaction.annotation.Transactional;
      
      import java.util.List;
      
      @Service
      public class ApplicationServiceImpl implements ApplicationService {
      
          @Autowired
          private ApplicationDao applicationDAO;
      
          @Transactional
          public void createApplication(ApplicationEntity application) {
              applicationDAO.createApplication(application);
          }
      
          @Transactional
          public List<ApplicationEntity> getAllApplications() {
              return applicationDAO.getAllApplications();
          }
      
          @Transactional
          public void deleteApplication(Integer id) {
              applicationDAO.deleteApplication(id);
          }
      }

      CONTROLLER
      Code:
      package com.codeyard.controller;
      
      import java.util.Map;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.validation.BindingResult;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import com.codeyard.model.ApplicationEntity;
      import com.codeyard.service.ApplicationService;
      
      @Controller
      @RequestMapping("/")
      public class ApplicationsController {
          @Autowired
          private ApplicationService applicationService;
      
          @RequestMapping(value = "/", method = RequestMethod.GET)
          public String listApplications(Map<String, Object> map) {
              map.put("applications", applicationService.getAllApplications());
              return "application";
          }
      }

      Comment

      Working...
      X