struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cesar Arevalo <cesararev...@yahoo.com>
Subject Re: Problem assiging elements in to list
Date Fri, 14 Mar 2008 17:17:10 GMT
HI Vamsi,

by the time you get to the for loop I don't see the list initialized, unless you are doing
this from your jsp I don't think it will work as how you have it, a better way would be with
a normal for like this :

            for (int index = 0; index < someLimit; index++) {
               Person person = new Person();
               persons.add(index, person);
                _logger.error("Person ::::::::::::::::::::::::::" + person);
                
                e.persist(person);
            }

hope this helps.

Cesar

----- Original Message ----
From: Vamsi Gmail! <vamseemandava@gmail.com>
To: Struts Users Mailing List <user@struts.apache.org>
Sent: Thursday, March 13, 2008 11:14:53 PM
Subject: Problem assiging elements in to list

Hi all,
 
         I have a requirement where I need to assign a Objects to a list.
I tried the example given in showcase there is working (in the site).
But It is not assigning to that list
 
I am also attaching code for your refernce.
 
---Vamsi


-----Inline Attachment Follows-----

/*
 * $Id: PersonAction.java 471756 2006-11-06 15:01:43Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0

 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package test.actions;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.exception.ConstraintViolationException;

import test.examples.Person;

import com.opensymphony.xwork2.ActionSupport;

    
@SuppressWarnings("serial")

public class PersonAction extends ActionSupport {

    //private Person person;
    private List persons = new ArrayList();
    
    private Log _logger = LogFactory.getLog(PersonAction.class);
    
    
    
    
    public List getPersons() {
        return persons;
    }

    public void setPersons(List persons) {
        this.persons = persons;
    }

    @SuppressWarnings("unchecked")
    
    public String save()  {
        EntityManager e = SfmsEntityManagerFactory.getEntityManager();
        EntityTransaction txn  =e.getTransaction();
        txn.begin();
        try{
            _logger.error("Person ::::::::::::::::::::::::::");
            _logger.error("Person ::::::::::::::::::::::::::");
            for (Object object :persons) {
                Person person  = (Person) object;
                _logger.error("Person ::::::::::::::::::::::::::" + person);
                
                e.persist(person);
            }
            _logger.error("Person ::::::::::::::::::::::::::");
            _logger.error("Person ::::::::::::::::::::::::::");
            txn.commit();
            addActionMessage("Record Added");
            read(e);
        }catch(ConstraintViolationException e1) {
            addActionError("Record Already There");
        }catch(IllegalStateException e1){
                        
        }catch(Exception e1) {
            addActionError("Error Inserting");
            txn.rollback();
        }
        
        return SUCCESS;
    }
    
    private void read(EntityManager e)  {                
        persons = e.createQuery("From Person").getResultList();
    
    }





   

}


-----Inline Attachment Follows-----

package test.examples;

import static javax.persistence.FetchType.LAZY;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Person implements Persister{

    
    @Id
    private String name;    
    
    @Basic(fetch=LAZY)
    private int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
    
        return name + "::::" + age;
    }
    
}

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message