Annotations - bobocode-breskul/bibernate GitHub Wiki
Specifies that the class is an entity. This annotation is applied to the entity class.
@Entity
public class Person {
}
Specifies the primary table for the annotated entity. If no Table
annotation is specified for an entity class, the default values apply.
@Table(name = "persons")
public class Person {
}
Specifies the primary key of an entity. The field or property to which the Id
annotation is applied should be one of the following types: any Java primitive type; any primitive wrapper type; String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger. The mapped column for the primary key of the entity is assumed to be the primary key of the primary table. If no Column
annotation is specified, the primary key column name is assumed to be the name of the primary key property or field.
public class Person {
@Id
private Long id;
}
Specifies the mapped column for a persistent property or field. If no Column
annotation is specified, the default values apply.
public class Person {
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column
private Integer age;
}
The @OneToOne
annotation is used to define a one-to-one relationship between two entities. It is a type of association in which one instance of an entity is associated with exactly one instance of another entity, and vice versa. Also it necessary to specify the mappedBy
element must be used to specify the relationship field or property of the entity that is the owner of the relationship .
public class Person {
@Id
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@OneToOne(mappedBy = "personAccount")
private Account account;
}
public class Account {
@Id
private Long id;
@Column(name = "nick_name")
private String nickName;
@OneToOne
private Person personAccount;
}
Specifies a many-valued association with one-to-many multiplicity. If the collection is defined using generics to specify the element type, the associated target entity type need not be specified; otherwise, the target entity class must be specified. If the relationship is bidirectional, the mappedBy
element must be used to specify the relationship field or property of the entity that is the owner of the relationship.
public class Person {
@OneToMany
private List<Note> notes = new ArrayList<>();
}
Specifies a single-valued association to another entity class that has many-to-one multiplicity. It is not normally necessary to specify the target entity explicitly since it can usually be inferred from the type of the object being referenced. If the relationship is bidirectional, the non-owning OneToMany
entity side must use the mappedBy
element to specify the relationship field or property of the entity that is the owner of the relationship.
public class Note {
@ManyToOne
private Person person;
}
Specifies a column for joining an entity association or element collection. If the JoinColumn
annotation itself is defaulted, a single join column is assumed and the default values apply.
public class Note {
@JoinColumn(name = "person_id")
private Person person;
}
Specifies that SQL update statements for the annotated entity are generated dynamically, and only include columns which are actually being updated.
@DynamicUpdate
public class Person {
}
Enumeration representing the different fetch types for entity associations. Used as parameter in annotations @OneToMany
and @ManyToOne
.
Specifies that the associated entity or collection should be eagerly fetched when the owning entity is fetched.
Specifies that the associated entity or collection should be lazily fetched when the owning entity is fetched.