简体   繁体   English

JPA瞬态注释

[英]JPA Transient annotation

I am learning Spring , Hibernate 我正在学习SpringHibernate

I have read the following and other tutorial sources. 我已经阅读了以下和其他教程资源。 What I understand is @Transient will prevent the object field from serializing and stop it from persisting to the database. 我了解的是@Transient将阻止对象字段序列化并阻止其持久化到数据库。

Transient variable in java

Transient annotation

JPA Transient annotation and JSON

My question is what if you have an object's field that you don't want to persist to database but you want the field to be retrieved when getting data from server? 我的问题是,如果您有一个对象字段不希望持久保存到数据库,但又希望从服务器获取数据时要检索该字段,该怎么办?

For example on your MySql createDate set to CURRENT_TIMESTAMP so when a data is inserted a TIMESTAMP is then created my MySql. 例如,在将MySql createDate设置为CURRENT_TIMESTAMP因此,当插入数据时,便会创建TIMESTAMP

MySql is set up something like this MySql的设置是这样的

ALTER TABLE `books` 
ADD COLUMN `dateCreated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON 
UPDATE CURRENT_TIMESTAMP AFTER `dateCreated`;

My controller is 我的控制器是

@RequestMapping(path="/booklist", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Map<String, Object> getListBooks(@RequestParam(name="search", required=false) String search, 
                @RequestParam(name="ordering", required=false) String ordering){


    Map<String, Object> data = new HashMap<String, Object>();

    // Get list of books from server
    List<Book> books = bookDao.getBookList(search, ordering);

    System.out.println(books.get(0));

    // do other things and return map
}

My return data is 我的退货数据是

Book [id=1, name=Murach Jquery, 2rd Edition, ispn=978-1890774912, price=4.24, dateCreated=null, datePublished=null]

My Book object is as follows 我的书对象如下

@Entity
@Table(name="books")
@Component
public class Book implements Serializable{

    private static final long serialVersionUID = -2042607611480064259L;

    @Id
    @GeneratedValue
    private int id;

    @NotBlank
    private String name;

    @NotBlank
    @Size(min=2, max=16)
    private String ispn;

    @DecimalMin(value = "0.1")
    private double price;

    @Transient
    private Timestamp dateCreated;

    private Date datePublished;

    // getter and setters

}

Thank you. 谢谢。

@Transient wont help in your case. @Transient 不会帮助您。

Use @PrePersist on a method in your entity class. 在实体类中的方法上使用@PrePersist

@PrePersist
public void beforePersist() {
    this.dateCreated = new Date();
}

beforePersist() method annotated with @PrePersist will help you automatically initilize entity fields before you want to persist a new entity. 带有@PrePersist注释的beforePersist()方法将帮助您在要持久保存新实体之前自动初始化实体字段。 For your case you can initialize your date property inside this method. 对于您的情况,您可以在此方法内初始化date属性。 You dont have to manually set it everytime you persist a new entity. 您不必在每次存在实体时都进行手动设置。

Now when you fetch entities from your database you will get your date property. 现在,当您从数据库中获取实体时,您将获得date属性。

Also look at @PreUpdate which helps you update entity fields when you update an entity. 另外,也要看看@PreUpdate这有助于当你更新实体更新实体领域。

Use @Formula annotation for the field and just pass the column. 在字段中使用@Formula批注,只需传递列即可。 See the example 例子

@Formula("createDate")
private Timestamp dateCreated;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM