spring-data-jpa 如果实体在Sping Boot JPA中是持久性的,如何更新SQL数据库表

7cjasjjr  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(170)

我已经创建了一个Sping Boot JPA应用程序,在其中,我已经使一个实体持久化。如下所示。这是我创建实体的代码:

@Getter
@Setter
@ToString
@Entity
public class TableModel implements Persistable<String> {

    @Override
    public String toString() {
        return "TableModel [PrimaryKey=" + PrimaryKey + ", Term_Guid=" + Term_Guid + ", Term_Name=" + Term_Name
                + ", Term_Description=" + Term_Description + ", Term_Qualified_Name=" + Term_Qualified_Name
                + ", Term_Status=" + Term_Status + ", Term_Created_Date=" + Term_Created_Date + ", Term_Updated_Date="
                + Term_Updated_Date + ", Report_name=" + Report_name + ", Report_Guid=" + Report_Guid + ", Report_Type="
                + Report_Type + ", classification=" + classification + ", Term_Expert_Username=" + Term_Expert_Username
                + ", Term_Steward_Username=" + Term_Steward_Username + ", Report_Expert_Username="
                + Report_Expert_Username + ", Report_Owner_Username=" + Report_Owner_Username + ", Sql_db_load_time="
                + Sql_db_load_time + "]";
    }
    @Id
    @Column(name = "Primary_Key")
    private String PrimaryKey;

    @Column(name = "Term_Guid")
    private String Term_Guid;

    @Column(name = "Term_Name")
    private String Term_Name;

    @Column(name = "Term_Description",columnDefinition = "nvarchar(MAX)")
    private String Term_Description;

    @Column(name = "Term_Qualified_Name")
    private String Term_Qualified_Name;

    @Column(name = "Term_Status")
    private String Term_Status;

    @Temporal(TemporalType.TIMESTAMP)
    private java.util.Date Term_Created_Date;

    @Temporal(TemporalType.TIMESTAMP)
    private java.util.Date Term_Updated_Date;

    @Column(name = "Report_name")
    private String Report_name;

    @Column(name = "Report_Guid")
    private String Report_Guid;

    @Column(name = "Report_Type")
    private String Report_Type;

    @Column(name = "Classification")
    private String classification;

    @Column(name = "Term_Expert_Username")
    private String Term_Expert_Username;

    @Column(name = "Term_Steward_Username")
    private String Term_Steward_Username;

    @Column(name = "Report_Expert_Username")
    private String Report_Expert_Username;

    @Column(name = "Report_Owner_Username")
    private String Report_Owner_Username;

    @Temporal(TemporalType.TIMESTAMP)
    private java.util.Date Sql_db_load_time;

    @Override
    public boolean isNew() {
        // TODO Auto-generated method stub
        return true;
    }

@Override
public String getId() {
    // TODO Auto-generated method stub
    return null;
}

}

通过使用此代码,我能够成功地插入数据,但如果我想更新插入的数据,我不能这样做,请建议我如何才能更新数据,如果实体是持久使用Springboot jpa

zour9fqk

zour9fqk1#

如果您不清楚如何使用springdatajpa触发更新查询,
您可以参考本文档以获取部分更新:
https://www.baeldung.com/spring-data-partial-update
您也可以参考spring data jpa文档。
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.modifying-queries

8iwquhpp

8iwquhpp2#

我建议您保持实体的简单,然后创建一个扩展JpaRepository或CrudRepository的存储库,您可以在其中编写查询

@Repository
public interface TableModelRepository extends JpaRepository<TableModel, String>{

@Modifying
@Query("update TableModel u set u.firstname = ?1 where u.lastname = ?2")
void updateTable(String firstname, String lastname);

}

创建可在其中执行更新的服务类

@Service
public class TableModelServices{

    @Autowired
    private TableModelRepository tableModelRepository;

    @Override
    public String updateTable(String firstname, String lastname) {
        tableModelRepository.updateTable(firstname, lastname);
        return null;
    }

}

以上代码仅供参考,我会提到一些链接,这将有助于您更多的https://www.baeldung.com/spring-data-jpa-dynamicupdate
https://www.baeldung.com/spring-data-partial-update
你可以参考这两个链接。

相关问题