Sobes.tech
Junior — Middle

Milliseid Springi annotatsioone kasutatakse andmebaasiga suhtlemiseks?

sobes.tech AI

Vastus AI-lt

In Spring, the following main annotations are used for interacting with the database:

  • @Entity — marks the class as an entity that will be mapped to a table in the database.
  • @Table — defines the name of the table and other parameters for the entity.
  • @Id — indicates the field as the primary key.
  • @GeneratedValue — specifies the strategy for generating the primary key value.
  • @Column — configures the display of the field in the table column.
  • @Repository — marks the class as a component for working with data (DAO), allowing Spring to handle database exceptions.
  • @Transactional — manages transactions when working with the database.

Example:

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    // getters and setters
}