简体   繁体   中英

How not to persist model entities to the database spring data jpa

This is my model

@Entity
@Table(name="MYTABLE")
public class MyTable {

    Integer errCode; 

    String errorText;

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    Integer id;
    ...
}

There are some fields that I want to persist to the database like id but there are some I don't want to like errCode and errText. Are there any annotations to not have them persist? Is it @Transient that I am looking for?

您要查找的注释是@Transient。

You want to add @Transient to all the getters for the fields that you do not wish to persist. These are usually for helper methods. I would suggest that you might not want to keep the error code and text in a class that you're attempting to persist. Maybe build another object that uses this class and provides the errCode and errorText . As sometimes Entities can get messy if there are a load of @Transient methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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