Question

I have a create page to create Courses. I can create courses but all their attributes are null except for the automatically generate attribute id. Here is my Model

public class Course extends Model {
    public String CourseName;
    public String CourseCode;

    @Lob
    public String CourseDescription;

    @ManyToOne
    public Department department;

    @OneToMany
    public List<Session> sessions;

    public Course(String CourseName, String CourseCode, String CourseDescription, Department department){
    this.CourseName = CourseName;
    this.CourseCode = CourseCode;
    this.CourseDescription = CourseDescription;
    this.department = department;
    }

Here is the form

 #{form @Courses.save(), id:'createUser'}
   <div>
      Course Name: <input type="text" name="courseName"  />
   </div>
   <div>
      Course Code: <input type="text" name="courseCode"  /> 
   </div>
   <div>
      Course Description: <textarea name="courseDescription" form="createUser"></textarea> 
   </div>
   <div>
      Department Id:<input type = "text" name = "Dept_Id" /> 
   </div>
   <div>
      <input type="submit" value="Create Course" /> 
   </div>

 #{/form}

And here is my controller method save()

   public static void save(String CourseName, String CourseCode, String CourseDescription, Long Dept_Id){
        Department department  = Department.findById(Dept_Id);
        Course course = new Course(CourseName, CourseCode, CourseDescription, department);
        course.save();
        department.addCourse(course);
        department.save();
        index();
     }

In my debug console I get this SQL statement insert into Course (CourseCode, CourseDescription, CourseName, department_id, id) values (?, ?, ?, ?, ?) which looks fine to me.

Edit 1:::

I renamed the attributes to lower case, checked if attribute names were a match and then tried to create a new course. No Luck. Here is the screenshot of the course table

Screenshot of the Course Table

Was it helpful?

Solution

Except for the Dept_Id parameter, your form inputs do not match your controller parameters, so Play can't bind them. You have to make them match. For example if in your view you use courseName, in your controller you also have to use courseName.

It's also very important that you respect Java naming conventions. It will save you from many headaches in the future.

I have rewritten your code below with better naming :

Model :

public class Course extends Model {
    public String courseName;
    public String courseCode;

    @Lob
    public String courseDescription;

    @ManyToOne
    public Department department;

    @OneToMany
    public List<Session> sessions;

    public Course(String courseName, String courseCode, String courseDescription, Department department){
        this.courseName = courseName;
        this.courseCode = courseCode;
        this.courseDescription = courseDescription;
        this.department = department;
    }

Then the form :

#{form @Courses.save(), id:'createUser'}
    <div>
      Course Name: <input type="text" name="courseName"  />
   </div>
   <div>
      Course Code: <input type="text" name="courseCode"  /> 
   </div>
   <div>
      Course Description: <textarea name="courseDescription" form="createUser"></textarea> 
   </div>
   <div>
      Department Id:<input type = "text" name = "departmentId" /> 
   </div>
   <div>
      <input type="submit" value="Create Course" /> 
   </div>
#{/form}

Then the controller :

public static void save(String courseName, String courseCode, String courseDescription, Long departmentId){
    Department department  = Department.findById(departmentId);
    Course course = new Course(courseName, courseCode, courseDescription, department);
    course.save();
    department.addCourse(course);
    department.save();
    index();
 }

OTHER TIPS

Have you checked your variables are correctly populated. You should stick with javabeans naming convention to avoid potential issues..

Try renaming your properties with lower case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top