Spring Data JPA如何更新表中的数据

时间:2018-02-21 08:10:42

标签: spring-data-jpa hibernate-mapping many-to-one

在spring数据jpa中,我正在尝试将数据更新到account table.for update json数据来自Angular Application.tson数据不存在于一个表中。例如,sdepname列不存在是帐户表,sdepname列是在department表中。我想将相应的sdepname主键更新到帐户表中。

为此,我创建了用于接收数据的新类。我正在传递接收数据对象以保存帐户存储库的方法。但是我收到错误。

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

我在控制台中收到此错误

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

任何人都可以帮助我在我的申请中做错了吗

1 个答案:

答案 0 :(得分:3)

AccountMainSave不是Entity

这是一个简单的POJO。如果您想要保存整个对象,则必须将其设为Entity并创建Repository,例如AccountRepository,对AccountMainSaveRepositoryEntity。< / p>

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

如果您只想更新数据库中已存在的数据,则应加载要更新的实体,设置新值,然后使用JPA-Repository的save-Methode保存更新的实体