• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I store data to a List<> without loosing the data of the List after my first data insertion?  RSS feed

 
Stavros Vrakas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I have created a service ArticlesService and in which I have created a property where I store
a list of orders (private List<Order> cartList;)

I created addOrderToList method, which I call from my UI in order to add a new order in my
Cart.

You can see an excerpt of  ArticlesService below:



When I run the code I manage to add an Order in my cartList by calling the addOrderToList method
and I also get the data from the list on a <p:dataTable> and I present them on my UI.

The problem begins, when I try to add a second order to the cartList. For some reason, the list turns up to
be empty!

The questions are:
1. how can I preserve my cartList so that I can add as many orders as I need to?
2. what should I type in my code in order to prevent new instances of my cartList to be created?
3. Is there an Annotation I could use?

In case someone is aware of a solution, or has found a workaround for such cases, please let me know.

P.S.: I tried the @ViewScoped and @Autowired annotations, but none of them worked.

Thank you in advance
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have JSF confused with ORM. Your samples and explanations seem to be mostly addressing data concerns.

JSF will display whatever data it is directed to select from its display Models. For simple data items (such as inputText), it will also replace (via the model's "set" method) the current model value with the value received from the client control.

For complex objects such as a dataTable, there are intermediary (fa├žade) model objects that maintain internal JSF information such as cursor position. When you use an array/collection directly as a "value=" model on an h:dataTable control, an anonymous DataModel object is created and bound between the dataTable and your value object. You can also explicitly construct a DataModel (ListDataModel, ArrayDataModel, etc.) which has the advantage of being directly addressable by your application code so that an action method can tell what row of a table the user selected (via the DataModel's "getRowData()" and "getRowIndex()" methods.)

The important thing to note is that an h:dataTable control is a read-only (display) control. You cannot insert, delete, or update rows using it. The table is not read out to the client and back to the server (which would be both a lot of network overhead and a security risk). You can attach functionality to the rows in the table, bit the dataTable itself can only display. So when you add a row (or rows) to a List in a JSF data model, you cannot use a simple "set" method on the target directly, you must first obtain the original List, then append/insert/replace/delete your new element(s) from that list. The rules are the same as they are for updating a one-to-many object relationship in an ORM.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!