Manytomany relation cannot store display id in mapping table in game structure

I am using play2.2.1 and am trying to create a ManyToMany relationship between the Jobads and JobCategory models .

My Jobads.java

package models;

@Entity
public class Jobads extends Model {

    @Id
    public Long id;

    @ManyToOne
    public Employers employer;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "jobads_jobcategories")
    public List<Jobcategories> jobcategory;

    @ManyToOne
    public Joblocations joblocations;

    @Required
    public String jobtype;

    @Required
    public String title;

    @Required
    public String text;

    @Required
    public Long salary;

    @Required
    public String experience;

    @Required
    public String active;

    @Formats.DateTime(pattern="yyyy-MM-dd hh:mm:yy")
    public Date created_time = new Date();

    @Formats.DateTime(pattern="yyyy-MM-dd hh:mm:yy")
    public Date modified_time ;

    @Required
    @Formats.DateTime(pattern="yyyy-MM-dd")
    public Date expire_date ;

    public static Finder<Long,Jobads> find = new Finder<Long,Jobads>(
            Long.class, Jobads.class
    );

    public static Jobads create(Jobads ja) {
        ja.save();  
        ja.saveManyToManyAssociations("jobcategory");    
        return ja; 
    }
}

      

My opinion:

<form action="@routes.JobAdController.save()" method="post" onsubmit="return checkEmpty();">
    <div class="row">
        <div class="col-md-6">
            <div class="block">
                <div class="header">
                    <h2>Create A New Job</h2>
                </div>
                <div class="content controls">
                    <div class="form-row">
                        <div class="col-md-3">Employer</div>
                        <div class="col-md-9">
                            <select class="form-control" name="employer.id" id="employer_id">
                                <option value="select">Select Employer</option>
                                @for(emp<-employersList) {
                                    <option value="@emp.id">@emp.company_name</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="col-md-3">Category</div>
                        <div class="col-md-9">
                            <select class="form-control" name="jobcategory.id" id="jobcategory_id">
                                <option value="select">Select Job Category</option>
                                @for(jc<-jobcategoryList) {
                                    <option value="@jc.id">@jc.name</option>
                                }
                            </select>
                        </div>
                    </div>   
                    <div class="form-row">
                        <div class="col-md-3">Location</div>
                        <div class="col-md-9">
                            <select class="form-control" name="joblocations.id" id="joblocation_id">
                                <option value="select">Select Job Location</option>
                                @for(jl<-joblocationList) {
                                    <option value="@jl.id">@jl.country,@jl.state,@jl.city</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="col-md-3">Job Type</div>
                        <div class="col-md-9">
                            <select class="form-control" name="jobtype" id="jobtype">
                                <option value="select">Select Job Type</option>
                                <option value="parttime">Part Time</option>
                                <option value="fulltime">Full Time</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="col-md-3">Title</div>
                        <div class="col-md-9"><input type="text" class="form-control" placeholder="Title" name="title" required="required"/></div>
                    </div>
                    <div class="form-row">
                        <div class="col-md-3">Text</div>
                        <div class="col-md-9"><textarea class="form-control" name="text" required="required"></textarea></div>
                    </div>
                    <div class="form-row">
                        <div class="col-md-3">salary</div>
                        <div class="col-md-9"><input type="text" class="form-control" placeholder="Salary per month"  name="salary" required="required"/></div>
                    </div>
                    <div class="form-row">
                        <div class="col-md-3">expire date</div>
                        <div class="col-md-9"><input type="date"   class="form-control" placeholder="expiry date" name="expire_date" id="expire_date"  required="required" /></div>
                        <input type="hidden"   class="form-control" placeholder="expiry date" name="exp_date" id="exp_date" />
                    </div>
                    <div class="form-row">
                        <div class="col-md-3">Active</div>
                        <div class="col-md-9">
                            <select class="form-control" name="active" id="active">
                                <option value="active">Active</option>
                                <option value="inactive">Inactive</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="col-md-12">
                            <button type="submit" class="btn btn-default btn-block">Add</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

      

And my controller:

public class JobAdController extends Controller {
    public static Result save() {

        Form<Jobads> jobadsFormData = jobadsForm.bindFromRequest();
        if (jobadsFormData.hasErrors()) {
            System.out.println("Error in form");
            return badRequest();
        } else {
            Jobads.create(jobadsFormData.get());
            return redirect(controllers.routes.JobAdController.index());
        }
    }
}

      

I don't have a variable for Jobads in the Jobcategory model because I don't need one. So the default table is created with the name jobads_jobcategories . My problem is when I try to insert data into the jobs table, it is inserted fine, but the display ids are not saved in the jobads_jobcategories table .

+3


source to share


3 answers


My job list was empty thanks to rtruszk for helping me. Play cannot bind mutiple valued variable from query to manytomany list variable models (I am still trying to figure it out why the game is doing this)

So what I did to solve this problem, I changed the form method to get the value of the url variable in my controller and then created a new list and added to my Form variable. This is optional, but I added getter and setter and changed the name of my list. The below code

For a 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 this method is not the correct way to preserve the manytomany relationship, but I had no other option to solve my problem.

If anyone has a better solution then post it because I have been trying this for a few days and have achieved it.

And why is the game not able to match or preserve the manytomany relationship?

+3


source


Your display is good. Try the following test to verify this:

@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 the following output:

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

      

You can also remove the following line from the Jobads class as it is deprecated:

ja.saveManyToManyAssociations("jobcategory");

      



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

jobadsFormData.get()

      

returns Jobads with an empty list of work categories. And then such an object will be saved correctly.

EDIT I can see in your code of view that you are using the HTML "select" element to select Jobcategories. Why are you doing this? You have a list of work categories in the Jobads class, but in the "select" element you can only select one category. Thus, this HTML element is good for choosing an employer or job, but not for choosing a list. So that's the problem. 'Select' elements have good properties but are not listed.

EDIT2 So you need to change two things:

  • Add multiple elements = "multiple" to your HTML selector
  • Check which categories are selected when you save the form. See this post for details .
+2


source


Try adding JoinColumn for Jobs and / or Job Categories

@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 the comment, but I don't have enough reputation.

0


source







All Articles