manytomany relation not able to save mapped id in mapping table in play framework

半腔热情 提交于 2019-12-05 21:01:21

My jobcategorylist was empty thanks to rtruszk for helping me out. Play is not able to bind the mutiple valued variable from request to models manytomany list variable (I am still tryng to figure this out why play is doing that)

So what I did to solve this problem, I changed the form method to get the value of mutiple valued variable from url in my controller then created a new list and added to my Form variable. It's optional but I added getter and setter and changed my list name. Code given below

For GET request

public static Result save() {

    List<Long> jobCatList = new ArrayList<Long>();
    final Set<Map.Entry<String, String[]>> entries = request().queryString().entrySet();
    for (Map.Entry<String, String[]> entry : entries) {
        final String key = entry.getKey();
        final String value = Arrays.toString(entry.getValue());
        System.out.println(key + " " + value);
        if (key.equalsIgnoreCase("jobcategories.id")) {
            String val = value;
            val = val.replace("[", "");
            val = val.replace("]", "");
            String[] a = val.split(",");
            for (int i = 0; i < a.length; i++) {
                jobCatList.add(Long.parseLong(a[i].trim()));
            }
        }
    }
    List<Jobcategories> jobCatObList = new ArrayList<Jobcategories>();
    for (Long id : jobCatList) {
        System.out.println("Id are:" + id);
        jobCatObList.add(Jobcategories.findById(id));
    }
    System.out.println(request().getQueryString("jobcategories.id"));
    Form<Jobads> jobadsFormData = jobadsForm.bindFromRequest();
    System.out.println("\nCategory Form are:" + (jobadsFormData.get()).getJobcategories());
   (jobadsFormData.get()).setJobcategories(jobCatObList);
    if (jobadsFormData.hasErrors()) {
        return badRequest();
    } else {
        Jobads.create(jobadsFormData.get());
        return redirect(controllers.routes.JobAdController.index());
    }
}

OR

For POST request

public static Result save() {

        Form<Jobads> jobadsFormData = jobadForm.bindFromRequest();

        Map<String, String[]> formUrlEncoded = request().body().asFormUrlEncoded();

        List<Jobcategories> fa = new ArrayList<Jobcategories>();


        for (String key : formUrlEncoded.keySet()) {
            String[] values = formUrlEncoded.get(key);
            for (String val : values) {
                if ("jobcategories.id".equals(key)) fa.add(Jobcategories.findById(Long.valueOf(val)));

            }
        }

            if (jobadsFormData.hasErrors()) {
            return badRequest();
        } else {
            (jobadsFormData.get()).setJobcategories(fa);

            Jobads.create(jobadsFormData.get());
            return redirect();

        }
    }

I know that this method is not the correct way of saving manytomany relation but I didn't have any other option to resolve my problem.

If anybody have a better solution then do post it because I am trying this for days and have achieves this much.

And why play is not able to map or save the manytomany relation?

rtruszk

Your mapping is good. Try running following test to be sure of that:

@Test
public void ebeanTest() {
    FakeApplication app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
    Helpers.start(app);

    Jobcategories jc = new Jobcategories();
    jc.id=1l;
    jc.title="cat 1";

    Jobads job = new Jobads();
    job.id=2l;
    job.title = "job 1";
    job.jobcategory.add(jc);
    Jobads.create(job);

    Jobads foundJob = Jobads.find.byId(2l);

    for(Jobcategories jobc:foundJob.jobcategory) {
        System.out.println("id: "+jobc.id);
        System.out.println("title: "+jobc.title);
    }       
}

You should have following result:

[play-java] $ test
id: 1
title: cat 1

You can also remove following line from your Jobads class because it is obsolete there:

ja.saveManyToManyAssociations("jobcategory");

It seems that you have error in your form. I think that

jobadsFormData.get()

returns Jobads with empty list of jobcategories. And then such object is saved correctly.

EDIT I see in your view code that you use 'select' HTML element for selecting Jobcategories. Why do you do that? You have list of Jobcategories in your Jobads class but in 'select' element you can select only one category. So this HTML element is good for selecting employer or joblocation but not for selecting list. So here is the problem. 'select' element maps good to fielld but not to list.

EDIT2 So you have to change two things:

  1. Add multiple="multiple" to your HTML select element
  2. Check which categories are selected while saving form. See this post for details

Try to add JoinColumn(s) for jobads and/or jobcategories

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "jobads_jobcategories", joinColumns = {@JoinColumn(name="Jobads_ID", referencedColumnName="ID")})
public List<Jobcategories> jobcategory;

I want to ask if you try it in a comment, but I don't have enough reputation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!