First of all I have two tables job, category which are in diagram as
and my entities are :
@Entity
@Table( name = TableName.JOB_TABLE)
public class Job {
@Id
@GeneratedValue
private Integer id;
private String title;
private String description;
@OneToMany(mappedBy = "job")
private List<Category> categories;
// omitting setters an getters for brevity
}
and
@Entity
@Table( name = TableName.CATEGORY_TABLE)
public class Category {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name = "job_id")
private Job job;
// omitting setters an getters for brevity
}
JobService is
@Service
public class JobService implements IDatabaseCrud<Job>{
@Autowired
private JobRepository jobRepository;
@Autowired
private CategoryRepository categoryRepository;
public void saveCategory(Job job) {
List<Category> categories = job.getCategories();
for (Category category : categories) {
category.setJob(job);
categoryRepository.save(category);
}
}
@Override
public void save(Job obj) {
// TODO Auto-generated method stub
jobRepository.save(obj);
saveCategory(obj);
}
}
now I don't have any idea to save new job where I've to save one Job with many categories selected from list.
<form:form commandName="job">
<form:input path="title"/><br>
<form:input path="company"/><br>
<form:input path="location"/><br>
<form:input path="url"/><br>
<form:input path="email"/><br>
<form:input path="description"/><br>
<form:select path="categories">
<form:options items="${categories}" itemValue="id" itemLabel="name"/>
</form:select><br>
<form:input path="createdAt"/><br>
<form:input path="toApply"/><br>
<input type="submit" value="Add Job">
</form:form>
the above form is not submitting data to controller and gives error HTTP Status 400 -The request sent by the client was syntactically incorrect. following controller I want to save these details to DB
@Controller
public class JobController {
private static final Logger logger = LoggerFactory.getLogger(JobController.class);
@Autowired
private JobService jobService;
@Autowired
private CategoryService categoryService;
@ModelAttribute("job")
public Job constructJob() {
return new Job();
}
@RequestMapping(value = "/jobs", method = RequestMethod.GET)
public String showJobs(Model model) {
model.addAttribute("jobs", jobService.findAll());
return "jobs";
}
@RequestMapping(value = "/jobs/{id}", method = RequestMethod.GET)
public String showJobDetail(Model model, @PathVariable Integer id) {
model.addAttribute("job", jobService.findJobWithCategories(id));
return "job-detail";
}
@RequestMapping(value = "/show-add-job", method = RequestMethod.GET)
public String showJobForm(Model model) {
model.addAttribute("categories", categoryService.findAll());
return "add-job";
}
@RequestMapping(value = "/show-add-job", method = RequestMethod.POST)
public String addJobDetail(@ModelAttribute("job") Job job) {
///jobService.save(job);
List<Category> categories = job.getCategories();
for (Category category : categories) {
logger.info("DEBUG job object", category);
}
return "redirect:/jobs";
}
}
with the above stuff I'm unable to save job with categories when I submit the form I get HTTP Status 400
. is some thing wrong in form.
This is URL to that project.