僱員實體Hibernate映射 - 外鍵列不能爲空 - 春天JSP
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;
}
}
血型實體
@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();
}
}
應用控制器
@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頁
<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狀態500 - 請求處理失敗;嵌套的例外是 org.hibernate.exception.ConstraintViolationException:無法執行 聲明 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 列「出價」不能爲空 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本機方法) sun.reflect.NativeConstructorAccessorImpl.newInstance(未知來源) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知來源) java.lang.reflect.Constructor.newInstance(未知來源) com.mysql.jdbc.Util.handleNewInstance(的Util .java:408)
當您提交表單時,您沒有獲取血型組對象,而是出價。你必須在你的服務中手動設置員工的血型,像這樣'employee.setBloodGroup(bloodDao.findOne(employee.bid)); dao.save(employee);' – JSingh
employee.setBloodGroup(bloodDao.findOne(employee.bid)); 「出價」類型是員工實體中的血型組。請幫我找findOne(employee.bid); – shami