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:
*
* @Entity
* public class Employee {
* @Id int id;
* @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)"}