Hibernate annotation development of attribute-level annotations

review

Hibernate annotation development and other level annotations

Attribute level annotation

@Id

    Required, which means the attribute mapped to the primary key in the database table. An entity class can have one or more attributes mapped to the primary key, which can be placed on the primary key attribute or the getXxx() method

    If multiple attributes in a class are defined as the primary key, the entity class must implement the Serializable interface

@GeneratedValue(strategy = "")

    ① strategy = GeneratedValue.AUTO //Automatically select according to the underlying database (default)

@Id //设置为主键
@GeneratedValue //主键自增
private int sid; //学号

    ② strategy = GeneratedValue.IDENTITY // automatically generated according to the identify field of the database (the primary key needs to be an integer)

    ③ strategy = GeneratedValue.SEQUENCE //Use sequence to determine the value of the primary key

    ④ strategy = GeneratedValue.TABLE //Use the specified table to determine the value of the primary key (need to be combined with @TableGenerator(name = "specified table name"))

 

If the primary key is a string type

@Id //设置为主键
@GeneratedValue(generator = "sid")  //由于是主键的字符串类型, 无法使用默认自增, 此时需要指定主键生成器
@GenericGenerator(name = "sid", strategy = "assigned")  //使用hibernate的注解自定义主键生成器, assigned为手动赋值
@Column(length = 8)  //默认的长度为255, 对于主键来说太长, 需要重新指定
private String sid; //学号

 

@Column (attributes can be mapped to columns)

    name: Optional, represents the name of the field in the database table, the default situation is the same as the attribute name

    nullable: optional, indicates whether the field is allowed to be empty, the default is true

    unique: Optional, indicating the size of the field, only valid for String type fields, the default value is 255 (if it is a primary key, the default value cannot be used)

    insertable: Optional, indicates whether the field should appear in the insert statement when the ORM framework performs the insert operation, the default is true

    updatable: Optional, indicates whether the field should appear in the update statement when the ORM framework performs the update operation. The default is true. This attribute is very useful for fields that cannot be changed once created, such as birthday (birthday) field

@Embedded (added to the attribute, indicating that this is an embedded class): Need to cooperate with @Embeddable

@EmbeddedId (added to the attribute, indicating that this is a composite primary key class): Need to cooperate with @Embeddable

@Id //设置为主键
@EmbeddedId  //标注在属性上
private StudentPK pk; //学号

Composite primary key class StudentsPK

@Embeddable
public class StudentsPK implements Serializable {

    private static final long serialVersionUID = 6632444697825110476L;
    @Column(length = 18)
    private String id;  //身份证号
    @Column(length = 8)
    private String sid;  //学号

    public StudentsPK(String id, String sid) {
        this.id = id;
        this.sid = sid;
    }

    public StudentsPK() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StudentsPK studentPK = (StudentsPK) o;
        return id.equals(studentPK.id) &&
                sid.equals(studentPK.sid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, sid);
    }
}

@Transient

    Optional, it means that the attribute is not a mapping to a field of the database table, and the ORM framework will ignore this attribute. If an attribute is not a field mapping in the data table, it must be marked as @Transient, otherwise the ORM framework defaults to its annotation @Basic

Guess you like

Origin blog.csdn.net/ip_JL/article/details/85680175