Annotations - bobocode-breskul/bibernate GitHub Wiki

@Entity

Specifies that the class is an entity. This annotation is applied to the entity class.

Example:

@Entity
public class Person {
}

@Table(String name)

Specifies the primary table for the annotated entity. If no Table annotation is specified for an entity class, the default values apply.

Example:

@Table(name = "persons")
public class Person {
}

@Id

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.

Example:

public class Person {
  @Id
  private Long id;
}

@Column(String name)

Specifies the mapped column for a persistent property or field. If no Column annotation is specified, the default values apply.

Example:

public class Person {
  @Column(name = "first_name")
  private String firstName;
  
  @Column(name = "last_name")
  private String lastName;
  
  @Column
  private Integer age;
}

@OneToOne

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 .

Example:

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;
}

@OneToMany

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.

Example:

public class Person {
  @OneToMany
  private List<Note> notes = new ArrayList<>();
}

@ManyToOne

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.

Example:

public class Note {
  @ManyToOne
  private Person person;
}

@JoinColumn

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.

Example:

public class Note {
  @JoinColumn(name = "person_id")
  private Person person;
}

@DynamicUpdate

Specifies that SQL update statements for the annotated entity are generated dynamically, and only include columns which are actually being updated.

Example:

@DynamicUpdate
public class Person {
}

FetchType

Enumeration representing the different fetch types for entity associations. Used as parameter in annotations @OneToMany and @ManyToOne.

EAGER

Specifies that the associated entity or collection should be eagerly fetched when the owning entity is fetched.

LAZY

Specifies that the associated entity or collection should be lazily fetched when the owning entity is fetched.

⚠️ **GitHub.com Fallback** ⚠️