Я пытаюсь вставить в базу данных. Я публикую свой код ниже.
Класс DAO:
package com.persistent.progracers.data.dao;
import org.hibernate.Session;
import com.persistent.progracers.data.pojo.UserDemographicInfo;
public class UserDemographicInfoDAO {
public void persist(UserDemographicInfo userDemographicInfo, Session session){
session.save("UserDemographicInfo", userDemographicInfo);
}
public void update(UserDemographicInfo userDemographicInfo, Session session){
session.saveOrUpdate("UserDemographicInfo", userDemographicInfo);
}
public void delete(UserDemographicInfo userDemographicInfo, Session session){
session.delete("UserDemographicInfo", userDemographicInfo);
}
}
POJO класс:
package com.persistent.progracers.data.pojo;
import java.util.Date;
@SuppressWarnings("serial")
public class UserDemographicInfo implements java.io.Serializable {
private long id;
private String firstName;
private String lastName;
private Date dateOfBirth;
private String mobileNumber;
private String gender;
private String status;
private Address address;
private String employeeID;
private String bloodGroup;
private Date dateCreated;
private Date dateModified;
public UserDemographicInfo() {
}
public UserDemographicInfo(String firstName, String lastName,
Date dateOfBirth, String mobileNumber, String gender,
String status, Address address, String employeeID,
String bloodGroup, Date dateCreated, Date dateModified) {
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.mobileNumber = mobileNumber;
this.gender = gender;
this.status = status;
this.address = address;
this.employeeID = employeeID;
this.bloodGroup = bloodGroup;
this.dateCreated = dateCreated;
this.dateModified = dateModified;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDateOfBirth() {
return this.dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getMobileNumber() {
return this.mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
public Address getAddress() {
return this.address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getEmployeeID() {
return this.employeeID;
}
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
public String getBloodGroup() {
return this.bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Date getDateModified() {
return this.dateModified;
}
public void setDateModified(Date dateModified) {
this.dateModified = dateModified;
}
}
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="SessionFactory">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">admin123</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ngo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">ngo</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<mapping resource="com/persistent/progracers/data/pojo/UserDemographicInfo.hbm.xml"/>
Теперь проблема в том, что я использую WaveMaker Studio. Он создает один Java-сервис. Итак, код сервиса Java здесь:
import org.hibernate.Session;
import com.persistent.progracers.data.dao.UserDemographicInfoDAO;
import com.persistent.progracers.data.pojo.UserDemographicInfo;
import com.persistent.progracers.wecare.factory.HibernateSessionFactory;
public class UserRegistrationService extends com.wavemaker.runtime.javaservice.JavaServiceSuperClass {
/* Pass in one of FATAL, ERROR, WARN, INFO and DEBUG to modify your log level;
* recommend changing this to FATAL or ERROR before deploying. For info on these levels, look for tomcat/log4j documentation
*/
public UserRegistrationService() {
super(INFO);
}
public UserDemographicInfo sampleJavaOperation(UserDemographicInfo demopojo) {
UserDemographicInfoDAO demodao=new UserDemographicInfoDAO();
Session session = HibernateSessionFactory.getSession();
try {
log(INFO, "Starting sample operation");
demodao.persist(demopojo, session);
log(INFO, "Returning ");
} catch(Exception e) {
log(ERROR, "The sample java service operation has failed", e);
}
return demopojo;
}
}
В wavemaker есть одна служебная переменная, которая связывает все значения со связанными полями таблицы. Но при нажатии на кнопку сохранения появляется сообщение об ошибке
: java.lang.IllegalArgumentException: попытка создать событие saveOrUpdate с нулевой сущностью.
StackTrace:
INFO (JavaServiceSuperClass.java:155) 22:41:25 - Starting sample operation
ERROR (JavaServiceSuperClass.java:131) 22:41:26 - The sample java service operation has failed
java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity
at org.hibernate.event.SaveOrUpdateEvent.(SaveOrUpdateEvent.java:40)
at org.hibernate.event.SaveOrUpdateEvent.(SaveOrUpdateEvent.java:23)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at com.persistent.progracers.data.dao.UserDemographicInfoDAO.persist(UserDemographicInfoDAO.java:8)
at UserRegistrationService.sampleJavaOperation(UserRegistrationService.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.wavemaker.runtime.service.reflect.ReflectServiceType.invokeMethod(ReflectServiceType.java:122)
at com.wavemaker.runtime.server.ServerUtils.invokeMethodWithEvents(ServerUtils.java:303)
at com.wavemaker.runtime.server.ControllerBase.invokeMethod(ControllerBase.java:269)
at com.wavemaker.runtime.server.JSONRPCController.executeRequest(JSONRPCController.java:123)
at com.wavemaker.runtime.server.ControllerBase.handleRequestInternal(ControllerBase.java:125)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.jav a:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:143)
at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
Может кто-нибудь сказать мне, в чем здесь ошибка?