0
votes

Employee Entity

import java.util.List;

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
    @Id
    @Column(name = "wid")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer wid;

    @ManyToOne(optional = false)
    @JoinColumn(name = "bid")
    private BloodGroup bloodgroup;

    public Employee() {
    }

    public Integer getWid() {
        return this.wid;
    }

    public void setWid(Integer wid) {
        this.wid = wid;
    }

    public BloodGroup getBloodgroup() {
        return bloodgroup;
    }

    public void setBloodgroup(BloodGroup bloodgroup) {
        this.bloodgroup = bloodgroup;
    }
}

Blood Group Entity

@Entity
@Table(name="BLOODGROUP")
public class BloodGroup {
    @Id
    @Column(name = "bid")
    @GeneratedValue (strategy= GenerationType.AUTO)
    private Integer bid;

    @Size(min=1,max = 30)
    @Column(name = "blood_name")
    private String blood_name;

    @OneToMany(mappedBy = "bloodgroup",fetch=FetchType.LAZY,
            targetEntity=Employee.class, cascade=CascadeType.PERSIST)
    private List<Employee> emp;
    public BloodGroup() {

    }
    public Integer getBid() {
        return bid;
    }
    public void setBid(Integer bid) {
        this.bid = bid;
    }
    public String getBlood_name() {
        return blood_name;
    }
    public void setBlood_name(String blood_name) {
        this.blood_name = blood_name;
    }
    public List<Employee> getEmp() {
        return emp;
    }
    public void setEmp(List<Employee> emp) {
        this.emp = emp;
    }
}

EmployeeDaoImpl

import com.springmvc.model.BloodGroup;
import com.springmvc.model.Employee;

@Repository("employeeDao")
public class EmployeeDaoImpl extends AbstractDao<Integer, Employee>
        implements EmployeeDao {
    @Override
    public void save(Employee employee) {
        persist(employee);
    }

    @Override
    public List<Employee> allEmployee() {
        Criteria criteria = createEntityCriteria();
        return (List<Employee>) criteria.list();
    }
}

EmployeeServiceImpl

import com.springmvc.service.employeeService;

@Service("EmployeeService")
@Transactional
public class employeeServiceImpl implements employeeService {
    @Autowired
    private EmployeeDao dao;

    @Override

    public void save(Employee employee) {
        dao.save(employee);
    }

    @Override
    public List<Employee> allEmployee() {

        return dao.allEmployee();
    }
}

App Controller

@RequestMapping(value = {"/new-emp"}, method = RequestMethod.GET)
public String newEmp(ModelMap model) {
    Employee employee = new Employee();
    model.addAttribute("employee", employee);

    ///////// Blood Group List
    List<BloodGroup> bloodgroup = bloodService.allBloodGroup();
    model.addAttribute("bloodgroupList", bloodgroup);
    return "addEmp";
}

// Call on Submission of 'Save or Update' Button Add Employee
@RequestMapping(value = {"/new-emp"}, method = RequestMethod.POST)
public String addEmp(@Valid Employee employee, BindingResult result,
                     ModelMap model,
                     final RedirectAttributes redirectAttributes, HttpServletRequest
                             request, HttpServletResponse response) {
    wService.save(employee);

    return "addEmp";
}

JSP Page

<form:form method="POST" modelAttribute="employee">
    <form:input type="hidden" path="wid" id="wid"/>

    <label for="bloodgroup">Blood Group:</label>
    <form:select path="bloodgroup" items="${blood}"
                 itemValue="bid" itemLabel="blood_name" name="blood1"/>

    <c:choose>
        <c:when test="${edit}">
            <input type="submit" value="Update"/>
        </c:when>

        <c:otherwise>
            <input type="submit" value="Save"/>
        </c:otherwise>
    </c:choose>

</form:form>

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'bid' cannot be null sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) java.lang.reflect.Constructor.newInstance(Unknown Source) com.mysql.jdbc.Util.handleNewInstance(Util.java:408)

1
When you submit the form, you're not getting bloodgroup object instead you have bid. You have to set manually employee's bloodgroup in your service something like this employee.setBloodGroup(bloodDao.findOne(employee.bid)); dao.save(employee);JSingh

1 Answers

0
votes

Well you do have explicitly said: @ManyToOne(optional=false) meaning it can't be null.