*
The moose likes Beginning Java and the fly likes How to set list value to entity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to set list value to entity?" Watch "How to set list value to entity?" New topic
Author

How to set list value to entity?

Sally william
Greenhorn

Joined: Mar 03, 2012
Posts: 15
I have 2 DTOs & 2 Entities, 1 controller & 1 service implementation over here.

Now, my problem is at the service implementation setStates. I dont know how to set states to address entity from addressDTO's getStatesList. because address entity includes state entity while addressDTO using stateDTO .

My Address Entity:



My Address DTO:



My StateDTO:



My State entity:



My controller:



My service implementation:



Please guide..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4341
    
    7

Hi Sally. Welcome to The Ranch!

For your DTOs to be useful, somewhere you're going to need methods for translating from entities to DTOs and vice-versa. If you haven't already got them, now is the time to write them. Firstly, you'll need a method that will convert a StateDTO to a State. Once you've got that, I'd suggest writing a second method that makes use of it that can convert a List of StateDTOs to a List of States (that will involve a loop, calling your translation method within it).

Then write a method that does a similar translation from AddressDTO to Address - this will make use of the previous method to convert the List of StateDTOs it contains.

Finally, once you've got that, you've got something you can easily call from the createAddress object to do most of the work.
Sally william
Greenhorn

Joined: Mar 03, 2012
Posts: 15
Matthew Brown wrote:Hi Sally. Welcome to The Ranch!

For your DTOs to be useful, somewhere you're going to need methods for translating from entities to DTOs and vice-versa. If you haven't already got them, now is the time to write them. Firstly, you'll need a method that will convert a StateDTO to a State. Once you've got that, I'd suggest writing a second method that makes use of it that can convert a List of StateDTOs to a List of States (that will involve a loop, calling your translation method within it).

Then write a method that does a similar translation from AddressDTO to Address - this will make use of the previous method to convert the List of StateDTOs it contains.

Finally, once you've got that, you've got something you can easily call from the createAddress object to do most of the work.


hi..can you please give an example on how to convert the list StateDTO to State ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4341
    
    7

Sally william wrote:hi..can you please give an example on how to convert the list StateDTO to State ?

Well, start first with just creating a single State object from a single StateDTO. You'd need to do this:
- Create a new State object
- Copy the field values from the StateDTO to the State
Do you know how to do those steps?

Once you've got that, then you can move on to converting a list:
- Create an empty list
- Iterate through the list of StateDTOs. For each one, create the corresponding State (i.e., apply what we did in the first part), and add it to the list
Do those both make sense?

It's a more effective learning process if we help you solve it yourself, rather than giving you a solution, which is why I don't want to just hand out an answer. The to a task you think you can't do is to break it down into simpler steps that you can.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7502
    
  18

Sally william wrote:Now, my problem is at the service implementation setStates. I dont know how to set states to address entity from addressDTO's getStatesList. because address entity includes state entity while addressDTO using stateDTO .

I'm no great expert on DTOs or their Java implementations, but I was a DBA for quite a while, and I see a few things about your model that worry me:

1. You have an Address object that contains a List of states, and an AddressDTO object that contains a List of Addresses. That suggests to me that you have a List of state lists, which sounds like overkill; especially since you already have a State object with an associated DTO.

2. You have a Map of "attributes" in your State object, which is totally un-normalized and will almost certainly rely on code to keep it validated. While such a construct may be reasonable for a DTO, I think you'd be much better off having named attributes in the State object itself (eg, 'area', 'population' etc). This would also ensure that they are the correct type (Strings make poor substitutes for proper values).

3. There are several standards for things like state and country codes, and if you use them, you'll be in a much better position to expand them if you need to. I'd start with ISO 3166 (3166-2 covers US states, but the whole standard also includes country codes); otherwise, there's also one published by the US Postal service which covers Canadian provinces as well.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sally william
Greenhorn

Joined: Mar 03, 2012
Posts: 15
Matthew Brown wrote:
Sally william wrote:hi..can you please give an example on how to convert the list StateDTO to State ?

Well, start first with just creating a single State object from a single StateDTO. You'd need to do this:
- Create a new State object
- Copy the field values from the StateDTO to the State


hi... i can only do until create new object. after that i dont know how to copy the value.

State stateobj = new State();
Sally william
Greenhorn

Joined: Mar 03, 2012
Posts: 15
Matthew Brown wrote:
Sally william wrote:hi..can you please give an example on how to convert the list StateDTO to State ?

Well, start first with just creating a single State object from a single StateDTO. You'd need to do this:
- Create a new State object
- Copy the field values from the StateDTO to the State
Do you know how to do those steps?

Once you've got that, then you can move on to converting a list:
- Create an empty list
- Iterate through the list of StateDTOs. For each one, create the corresponding State (i.e., apply what we did in the first part), and add it to the list
Do those both make sense?

It's a more effective learning process if we help you solve it yourself, rather than giving you a solution, which is why I don't want to just hand out an answer. The to a task you think you can't do is to break it down into simpler steps that you can.


Hi Matthew,

I've done this...



but now, im having another error...
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: entity.State; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: entity.State
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set list value to entity?
 
Similar Threads
S2: autocompleter - how to populate a list based on user's input?
AbstractWizardFormController: teasing command object
When submiting driop down value its shwoing null.
Need help !!!! Facing problem with display of data fetched from Database
AbstractWizardFormController: java.lang.NullPointerException at processFinish(...)