I have an client-server application.
The client has a class: Item.java
public class Item
public string name;
public string size;
and the server has a class PersistableItem.java which lets me store it using JPA
public class PersistableItem extends Item
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long Id;
and sends it to the server which executes the addItemToServer method:
public void addItem( Item item){
//create PersistableItem from item
DAO.save([paramerter = PersistableItem]);
}
In my server method do I cast the item as PersistableItem? Do i make a constructor in PersistableItem that takes in a Item?
What’s the proper design here?
As you suggested, You can create a constructor in SomePersistedItem that takes a SomeItem. In the constructor, you call super with the name and size, so you have your SomePersistedItem correctly populated.
And you just add it.
That’s assuming you have a constructor in SomeItem that takes a name and size. Else you use whatever method you have to build SomeItem (Factory, setters…)