簡體   English   中英

JPA 中是否有某種方法可以根據其他值自動增加列

[英]Is there some way in JPA to auto-increment a column automatically based on other value

我有一個名為 Employee 的實體

@Entity
public Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;

    @Basic(optional = false)
    @Column(name = "name")
    private String name;

    @Basic(optional = false)
    @Column(name = "target")
    private int target;
}

現在我想要實現的是,每當我堅持使用同名員工而不是創建新記錄時,我都希望目標增加。

例如,如果我堅持一個員工兩次如下:

Employee e1 = new Employee();
e1.setName("John");
em.persit();

目標現在應該是 1

那么如果我這樣做:

Employee e2 = new Employee();
e1.setName("John");
em.persit();

target 會自動自增,現在它的值應該是 2。因為名字為 John 的 Employee 已經在表中了。

如果您的數據庫支持觸發器,它應該是要走的路! mysql 例如https://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html

除此之外,我沒有看到如何自動完成,您需要查詢具有相同名稱的員工,然后為新員工設置正確的目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM