【问题标题】:Spring Data JPA How to update data in table春季数据JPA如何更新表数据
【发布时间】:2018-07-31 17:42:52
【问题描述】:

在 spring data jpa 中,我正在尝试将数据更新到帐户表中。因为更新 json 数据来自 Angular 应用程序。json 数据不存在于一个表中。例如 sdepname 列不存在是帐户表,sdepname 列在部门表中。我想将各自的 sdepname 主键更新到帐户表中。

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

帐户模型

@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;

部门模型

  @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;

调查员模型

@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

帐户控制器

@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;
        }
    }

帐户存储库

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

    List<AccountModel> findByNaccountid(Integer naccountid);

    void save(AccountMainSave s);
}

我在控制台中收到此错误

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

谁能帮助我在我的应用程序中做错了什么

【问题讨论】:

  • JPA 应该如何知道如何将您的AccountMainSave 写入/映射到正确的表/对象。你不能只是递给它一些随意的东西,然后指望它神奇地想出要做什么。您需要将您的AcountMainSave 转换为您可以保存的AccountModel

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


【解决方案1】:

AccountMainSave 不是Entity

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

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

如果您只想更新数据库中已经存在的数据,那么您应该加载要更新的实体,设置新值,然后使用 JPA-Repository 的保存方法保存更新的实体

【讨论】:

    猜你喜欢
    • 2016-01-30
    • 2013-11-12
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 2019-03-02
    • 2020-08-19
    相关资源
    最近更新 更多