简体   繁体   中英

Why fields marked as final or static not saved in database

I have marked a field as static in a bean class and it is not saved in database. can someone explain it why the static fields are not persisted.

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="USER_DETAILS")
public class UserDetails {

    @Id
    @Column(name="USER_ID")
    private int userId;

    private static String userName;

    @Temporal(TemporalType.DATE)
    private Date joinedDate;
    private String Address;


    private String description;



    public Date getJoinedDate() {
        return joinedDate;
    }
    public void setJoinedDate(Date joinedDate) {
        this.joinedDate = joinedDate;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName + "from Getter";
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

I am trying to save it in postgreSQL database. and the field userName is not saving.

Since userName is static, it doesn't belong to an instance, it belongs to the class itsself. That's what static means.

According to docs:

If the entity class uses persistent fields, the Persistence runtime accesses entity-class instance variables directly. All fields not annotated javax.persistence.Transient or not marked as Java transient will be persisted to the data store. The object/relational mapping annotations must be applied to the instance variables.

Since userName is static, it won't be persisted.

Persistent fields and properties

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