Hibernate attribute level annotation instance code

Attribute level annotation

Add method:

Write on the property field and on the get accessor of the property

@Id

Yes, the attribute mapped to the primary key of the database table is defined. An entity class can have one or more attributes mapped to the primary key, which can be placed before the primary key attribute or getxxx(). Note: if multiple attributes are defined as the primary key attribute, the entity class must implement the serializable interface

@Sequencegenerator @ generatedvalue @ generatedvalue (strategy = generationtype, generator = ""): optional, used to define the primary key generation policy

Strategy indicates the primary key generation policy. The values are:

GenerationType. Auto: automatically select (default) generationtype according to the underlying database. Index: generate generationtype according to the identity field of the database. Sequence: use sequence to determine the value of the primary key. Generationtype.table: use the specified table to determine the value of the primary key, which is used in combination with @ tablegenerator

@Column

@Column - attributes can be mapped to columns, and the annotation can be used to override the default value, @ column describes the detailed definition of the field in the database table, which is very useful for the tool to generate the database table structure according to the JPA annotation.

Common attributes:

Name: optional, indicating the name of the field in the database table. By default, the attribute name is the same. Nullable: optional, indicating whether the field is allowed to be null. The default is true unique: optional, indicating whether the field is a unique identifier. The default is false length: optional, indicating the size of the field. It is only valid for string fields. The default value is 225, The primary key cannot use the default value insertable: optional, indicating whether the field should appear in the insert statement when the ORM framework performs the insert operation. The default value is true. Updateable: optional, indicating whether the field should appear in the update statement when the ORM framework performs the update operation. The default value is true. This property is useful for fields that have been created and cannot be changed

@Embedded

@Embedded is the of the annotation attribute, and the class representing the attribute is an embedded class.

Note: embedded classes must also be annotated with @ embeddable annotation

@EmbeddedId

@Embeddedid uses embedded primary key classes to implement composite primary keys

Note: the embedded primary key class must implement the serializable interface, have a default public parameterless constructor, and override the equals and hashcode methods

@Lob @Version @Basic @Transient

Optional, indicating that the attribute is not a mapping to the field of the database table. The ORM framework will ignore this attribute. If an attribute is not a field mapping of the database table, it must be marked as @ transient. Otherwise, the ORM framework defaults to its annotation as @ basic

example:

hibernate. Properties configuration:

Entity class student:

Studentpk entity class:

Address class:

Test class:

summary

The above is all about the hibernate attribute level annotation example code in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: introduction to hibernate core ideas and interfaces, introduction to hibernate implementation of pessimistic lock and optimistic lock code, quick understanding of Hibernate configuration files and mapping files, etc. As the saying goes, there is Yan Ruyu in the book. Here are some good books for you to learn and refer to. I also hope you can give more support to this website.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>