The Recipe object is indeed serializable. Below some code from
the AddRecipeForm and Recipe:
public class Recipe implements Serializable {
public static final int NAME_LENGTH = 50;
public static final int DIFFICULTY_LENGTH = 6;
public static final int INTRODUCTION_LENGTH = 150;
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"recipe_seq")
private Long id;
...
@ManyToOne
@org.hibernate.annotations.Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn(name = "CATEGORY_ID")
@ForeignKey(name = "FK_RECIPE_CATEGORY")
private Category category;
...
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
assert category != null;
this.category = category;
}
}
public class AddRecipeForm extends Form<Recipe> {
private static final Logger log =
LoggerFactory.getLogger(AddRecipeForm.class);
private final List<FileUpload> uploads = new ArrayList<FileUpload>();
private Recipe recipe = new Recipe();
/**
* Creates a new {...@link AddRecipeForm}.
*
* @param id form id; may not be <code>null</code>
*/
public AddRecipeForm(String id) {
super(id);
setModel(new Model<Recipe>(recipe));
add(new FeedbackPanel("feedback"));
addRecipeInfoFormPart();
...
add(createSubmitLink(this));
}
private void addRecipeInfoFormPart() {
add(new RequiredTextField<String>("recipeTitle", new
PropertyModel<String>(recipe, "name"))
.add(StringValidator.lengthBetween(5, Recipe.NAME_LENGTH)));
add(new CategoriesDropDown("categories", new
PropertyModel<Category>(recipe, "category")).setRequired(true));
add(new RecipeDifficultiesDropDown("difficulties",
recipe).setRequired(true));
add(new TextArea<String>("introduction", new
PropertyModel<String>(recipe, "introduction")));
}
...
private StyledSubmitLink createSubmitLink(final Form<Recipe> form) {
StyledSubmitLink submitLink = new StyledSubmitLink("submitLink") {
@Override
public void onSubmit() {
RecipeService recipeService = RecipeService.getInstance();
Recipe recipe = form.getModelObject();
Category recipeCategory =
recipeService.findCategoryByCategoryName(recipe.getCategory().getName());
Set<Tag> persistentTags = new HashSet<Tag>();
for (Tag tag : recipe.getTags()) {
Tag persistentTag =
recipeService.findTagByTagName(tag.getName());
persistentTags.add(persistentTag == null ? tag :
persistentTag);
}
log.debug(String.format("from persisted cat: %s and from
form %s", recipeCategory.getName(), recipe.getCategory().getName()));
Status persistentStatus =
recipeService.findStatusByStatusName("NEW");
Recipe newRecipe = new Recipe(recipeCategory,
recipe.getDifficulty(),
recipe.getIntroduction(),
recipe.getMethod(),
recipe.getName(),
new Integer(0),
UserUtils.getUser(),
recipe.getPreparationTime(),
recipe.getRecipeImages(),
new
HashSet<RecipeIngredient>(recipe.getRecipeIngredients()),
recipe.getServings(),
persistentStatus == null ? new Status("NEW", "New")
: persistentStatus,
persistentTags);
uploadImages();
recipeService.insertNewRecipe(newRecipe);
setResponsePage(new
SuccessPage(getString("message.success.addrecipe.title"),
getString("message.success.addrecipe.body")));
}
};
submitLink.add(new Label("submit", new
ResourceModel("form.addrecipe.add")));
return submitLink;
}
private class CategoriesDropDown extends DropDownChoice<Category> {
public CategoriesDropDown(String id, IModel<Category> model) {
super(id, model,
RecipeService.getInstance().getAllPersistentRecipeCategories());
setChoiceRenderer(new IChoiceRenderer<Category>() {
@Override
public String getIdValue(Category category, int index) {
return category.getName();
}
@Override
public Object getDisplayValue(Category category) {
return category.getName();
}
});
}
}
private class RecipeDifficultiesDropDown extends
DropDownChoice<RecipeDifficulty> {
public RecipeDifficultiesDropDown(String id, Recipe recipe) {
super(id, new PropertyModel<RecipeDifficulty>(recipe,
"difficulty"), Arrays.asList(RecipeDifficulty.values()),
new IChoiceRenderer<RecipeDifficulty>() {
@Override
public Object getDisplayValue(RecipeDifficulty
recipeDifficulty) {
return
recipeDifficulty.getFormattedDifficulty();
}
@Override
public String getIdValue(RecipeDifficulty
recipeDifficulty, int index) {
return recipeDifficulty.getName();
}
});
}
}
}
On Sat, Feb 28, 2009 at 6:24 PM, Igor Vaynberg <[email protected]>wrote:
> where is the code that creates your form? and is Recipe serializable?
>
> -igor
>
> On Sat, Feb 28, 2009 at 8:58 AM, Azzeddine Daddah <[email protected]>
> wrote:
> > Thanks Igor,
> > I've already tried that but still have the same problem.
> > add(new CategoriesDropDown("categories", new
> PropertyModel<Category>(recipe,
> > "category")).setRequired(true));
> > ...
> > private class CategoriesDropDown extends DropDownChoice<Category> {
> > public CategoriesDropDown(String id, IModel<Category> model) {
> > super(id, model,
> > RecipeService.getInstance().getAllPersistentRecipeCategories(),
> > new IChoiceRenderer<Category>() {
> > @Override
> > public String getIdValue(Category category, int index) {
> > return category.getName();
> > }
> >
> > @Override
> > public Object getDisplayValue(Category category) {
> > return category.getName();
> > }
> > });
> > }
> > }
> >
> >
> >
> > On Sat, Feb 28, 2009 at 5:54 PM, Igor Vaynberg <[email protected]
> >wrote:
> >
> >> use a property model instead of recipe.getcategories()
> >>
> >> -igor
> >>
> >> On Sat, Feb 28, 2009 at 8:17 AM, Azzeddine Daddah <[email protected]
> >
> >> wrote:
> >> > Hi,
> >> > I'm trying to use a DropDownChoice to display and store the selected
> >> > category in the database. The value selected in the drop down is
> >> correctly
> >> > set, but when I look to the model (Category) of this drop down, it
> >> returns
> >> > always null. Do I do something wrong? Below some code.
> >> >
> >> > public AddRecipeForm(String id) {
> >> > super(id);
> >> > setModel(new Model<Recipe>(new Recipe()));
> >> > ...
> >> > add(new CategoriesDropDown("categories",
> >> > recipe.getCategory()).setRequired(true));
> >> > }
> >> > ...
> >> > public void onSubmit() {
> >> > RecipeService recipeService = RecipeService.getInstance();
> >> > Recipe recipe = form.getModelObject();
> >> > Category cat = recipe.getCategory(); // This returns always null
> >> > }
> >> >
> >> > ...
> >> >
> >> > private class CategoriesDropDown extends DropDownChoice<Category> {
> >> > public CategoriesDropDown(String id, Category category) {
> >> > super(id, new Model<Category>(category),
> >> > RecipeService.getInstance().getAllPersistentRecipeCategories(), new
> >> > IChoiceRenderer<Category>() {
> >> > @Override
> >> > public String getIdValue(Category category, int index)
> {
> >> > return category.getName();
> >> > }
> >> >
> >> > @Override
> >> > public Object getDisplayValue(Category category) {
> >> > return category.getName();
> >> > }
> >> > });
> >> > }
> >> > }
> >> >
> >> >
> >> > Kind regards,
> >> >
> >> > Hbiloo
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [email protected]
> >> For additional commands, e-mail: [email protected]
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>