-1

In spring data jpa ,I am trying to update data into account table.for update json data is coming from Angular Application.that json data is not present in one table. for example sdepname column not present is account table,sdepname column is in department table. I want to update respective sdepname primary key into account table.

For that I created new class for receiving data.I am passing receive data object to save method of Account repository.but i am getting error.

AccountModel

@Entity
@Table(name="account")
public class AccountModel implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_seq_generator")
    @SequenceGenerator(name="account_seq_generator", sequenceName = "account_seq")

    public Integer naccountid;
    public String namount;
    public String sacctdesc;
    public Integer naccountcpcmappingid;
    public Integer nindirectcostrate;
    public Integer nagencyid ;
    public Integer ndeptid ;
    public String sgrantnum;
    public Timestamp dstartdate;
    public Timestamp denddate;  
    public String slocation;
    public String sclientacctid;
    public Integer ninvestigatorid;
    public Integer ninstid;
    public Integer ntempaccountid;

    @ManyToOne(optional = true,cascade = {CascadeType.MERGE})
    @JoinColumn(name="ndeptid",insertable =  false, updatable = false)
    public DepartmentModel department;

    @ManyToOne(optional = true,cascade = {CascadeType.ALL})
    @JoinColumn(name="ninvestigatorid",insertable =  false, updatable = false)
    public InvestigatorModel investigator;

    @ManyToOne(optional = true,cascade = {CascadeType.ALL})  
    @JoinColumn(name="naccountcpcmappingid",insertable =  false, updatable = false)
    public AccountCPCMappingModel accountCPC;

DepartmentModel

  @Entity
    @Table(name = "department")
    public class DepartmentModel implements Serializable {  

        private static final long serialVersionUID = 1L;
         @Id
         @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "department_seq_generator")
         @SequenceGenerator(name="department_seq_generator", sequenceName = "department_seq")

        public  Integer ndeptid;    
        public String sdeptname ;
        public  Integer ninstid  ;
        public Boolean bislocked;
        public String sclientdeptid;
        public Integer nsurveymethodid;
        public Boolean bisjointuse;
        public Integer ntempdeptid;
        public  Boolean balternatejointusepercentage;
        public Integer ndivid;  

AccountCPCMappingModel

@Entity
@Table(name="accountcpcmapping")
public class AccountCPCMappingModel implements Serializable {

            private static final long serialVersionUID = 1L;

       @Id
       @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accountcpcmapping_seq_generator")
       @SequenceGenerator(name="accountcpcmapping_seq_generator", sequenceName = "accountcpcmapping_seq")
       //@GeneratedValue(strategy = GenerationType.SEQUENCE)
        public Integer naccountcpcmappingid;
        public String sccpcode;
        public Integer  nmastercpcid;
        public Integer ninstid;
        public String sccpcname;

InvestigatorModel

@Entity
@Table(name="investigator")
public class InvestigatorModel implements Serializable{

        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "investigator_seq_generator")
        @SequenceGenerator(name="investigator_seq_generator", sequenceName = "investigator_seq")
        //@GeneratedValue(strategy = GenerationType.SEQUENCE)
        public Integer ninvestigatorid;
        public String sinvestigatorname;
        public Integer ninstid ;
        public String stitle;
        public Integer ntempinvestigatorid;
        public Integer nempid;  

AccountMainSave

package com.demo.model;

public class AccountMainSave {  

    public Integer naccountid;
    public String sclientacctid;
    public String sacctdesc;
    public String slocation;
    public Integer ndeptid;
    public String sdepname;
    public Integer naccountcpcmappingid;
    public String sccpcode;
    public Integer ninvestigatorid;
    public String sinvestigat

AccountController

@RestController
 @RequestMapping("/SpaceStudy/SpaceAdmin")
    public class AccountController
    {
        @Autowired
        AccountRepository accRepo;

        @CrossOrigin(origins="*")
        @PutMapping("/AccountMaintenance/Save")
        public List<AccountModel> btnSaveClick(@RequestBody AccountMainSave s)
        {       
            List<AccountModel> objAcct=accRepo.findByNaccountid(s.naccountid);  
            if(objAcct!=null)
            {
                accRepo.save(s);
            }
            return objAcct;
        }
    }

AccountRepository

@Repository
public interface AccountRepository extends JpaRepository<AccountModel, Integer>{              

    List<AccountModel> findByNaccountid(Integer naccountid);

    void save(AccountMainSave s);
}

enter image description here

I am getting This error in console

org.hibernate.MappingException: Unknown entity: com.demo.model.AccountMainSave

can any one help me what i am doing wrong in my application

SpringUser
  • 1,351
  • 4
  • 29
  • 59
  • How should JPA know how to write/map your `AccountMainSave` to the right table/object. You cannot just hand it abritrary objects and expect it to magically figure out what to do. You need to convert your `AcountMainSave` into an `AccountModel` which you can save. – M. Deinum Feb 21 '18 at 11:33

1 Answers1

3

AccountMainSave is not an Entity.

It is a simple POJO. If you want to save the whole Object you would have to make it an Entity and create a Repository like AccountRepository, say AccountMainSaveRepository for that Entity.

@Repository public interface AccountMainSaveRepository extends JpaRepository<AccountMainSave, Integer>{
}

If you just want to update the Data that is already present in the Database then you should load the Entity which you want to update, set the new values and then save the updated entity with the save-Methode of the JPA-Repository

Michael Seiler
  • 650
  • 1
  • 5
  • 15