hibernate-jpa-api.jar中的@Transient標(biāo)簽使用

1.標(biāo)簽描述:

package javax.persistence;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * Specifies that the property or field is not persistent. It is used
 * to annotate a property or field of an entity class, mapped
 * superclass, or embeddable class.
 *
 * <pre>
 *    Example:
 *
 *    &#064;Entity
 *    public class Employee {
 *        &#064;Id int id;
 *        &#064;Transient User currentUser;
 *        ...
 *    }
 * </pre>
 *
 * @since Java Persistence 1.0
 */
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
public @interface Transient {
}

核心點(diǎn):這個標(biāo)簽是用來指定屬性或字段,是瞬時狀態(tài),不持久化到數(shù)據(jù)庫中。

2.標(biāo)簽放置位置:親測只有放置到屬性的GET方法上,才會起作用!

@Entity
public class Books {
    private Integer id;
    private String name;
    private String author;
    private Integer price;
    private String description;
    private Date createTime;
    private Date updateTime;

    private String newParameter;


    @Transient
    public String getNewParameter() {
        return newParameter;
    }

    public void setNewParameter(String newParameter) {
        this.newParameter = newParameter;
    }
...

控制臺Hibernate底層執(zhí)行的SQL語句:可以看到在String newParameter;對應(yīng)的GET方法上加上@Transient標(biāo)簽,插入SQL語句將自動排除這個字段。

Hibernate: 
    insert 
    into
        books
        (author, create_time, description, name, price, update_time) 
    values
        (?, ?, ?, ?, ?, ?)
{"description":"好好學(xué)習(xí),承擔(dān)醫(yī)學(xué)傳承","id":21,"name":"神農(nóng)本草經(jīng)"}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容