Search code examples
liferayhookliferay-service-builder

how to save custom entity with service builder liferay


how to save custom entity with service builder liferay? i create new entity (name=BasicProfile) and build by service builder liferay but an exception occurred while saving. my code :

    BasicProfile basicProfile = new BasicProfileImpl();
    basicProfile.setUserId(11701L);

    basicProfile.setBirthCertificateSerial(birthCertificate);
    basicProfile.setCreateDate(new Date());
    basicProfile.setFatherName(fatherName);
    basicProfile.setExtCustId(acceptorNumber);
    basicProfile.setMobileNumber(telNumber);
    basicProfile.setNationalCode(nationalCode);
   basicProfile = BasicProfileLocalServiceUtil.addBasicProfile(basicProfile);  

Solution

    1. In your BasicProfileLocalServiceImpl.java create the following method:

      public BasicProfile create(long userId, String birthCertificate, String fatherName, String acceptorNumber, String telNumber, String nationalCode) throws Exception {
          long id = counterLocalService.increment();
          BasicProfile obj = createBasicProfile(id);
          obj.setUserId(userId);
          obj.setBirthCertificateSerial(birthCertificate);
          obj.setCreateDate(new Date());
          obj.setFatherName(fatherName);
          obj.setExtCustId(acceptorNumber);
          obj.setMobileNumber(telNumber);
          obj.setNationalCode(nationalCode);
          obj = updateProduct(obj);
          return obj;
      }
      
    2. Then in your code call that method to create a new object:

      BasicProfile basicProfile = BasicProfileLocalServiceUtil.create(userId, birthCertificate, fatherName, acceptorNumber, telNumber, nationalCode);