0% found this document useful (0 votes)
3 views

corejava

Uploaded by

Debasis Bhuyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

corejava

Uploaded by

Debasis Bhuyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Standard Java Annotations


Annotation Purpose Example

@Override Indicates that a method java public class Dog extends Animal { @Override

is overriding a method public void sound() { System.out.println("Dog


barks"); } }
in the superclass.

@Deprecated Marks a method or class java public class LegacyCode { @Deprecated public

as deprecated. void oldMethod() { System.out.println("Old


method"); } }

@SuppressWarnings Suppresses specified java @SuppressWarnings("unchecked") public class

compiler warnings. Main { List rawList = new ArrayList(); }

@FunctionalInterface Marks an interface as java @FunctionalInterface interface Calculator {

functional (one abstract int add(int a, int b); }

method).

2. Custom Java Annotations


Annotation Purpose Example

@MyCustomAnnotation Defines a java @interface MyCustomAnnotation { String author() default

custom "Unknown"; String date(); } @MyCustomAnnotation(date = "2025-


01-08", author = "John Doe") public class CustomExample {
annotation.
public void printMessage() { System.out.println("Custom
Annotation!"); } }

3. Advanced Java Annotations (Spring, Hibernate)


Annotation Purpose Example

@Component Marks a class as a Spring- java @Component public class Car { private

managed bean. Engine engine; @Autowired public Car(Engine


engine) { this.engine = engine; } }

@Autowired Automatically injects a Spring java @Autowired private Engine engine;

bean.
Annotation Purpose Example

@Controller Marks a class as a controller in java @Controller public class UserController

Spring MVC. { @RequestMapping("/user") public String


getUser() { return "user"; } }

@RestController Specialized @Controller to build java @RestController public class

RESTful APIs. UserController { @GetMapping("/user") public


User getUser() { return new User("John",
"john@example.com"); } }

@Transactional Ensures method executes within a java @Transactional public void

transaction. createOrder(Order order) { // Operations }

@Entity Marks a class as a Hibernate entity java @Entity @Table(name = "users") public

mapped to a database table. class User { @Id @GeneratedValue private Long


id; @Column private String username; }

@Table Specifies the table name for an java @Table(name = "users") public class User

entity. { @Id private Long id; @Column private String


username; }

@Id Marks a field as the primary key in java @Id @GeneratedValue private Long id;

an entity.

@Column Specifies the column mapping for java @Column(name = "username", nullable =

an entity field. false) private String username;

@OneToMany Defines a one-to-many java @OneToMany(mappedBy = "user") private

relationship between entities. List<Order> orders;

@ManyToOne Defines a many-to-one java @ManyToOne @JoinColumn(name = "user_id")

relationship between entities. private User user;

@Version Implements optimistic locking java @Version private Integer version;

with a version column.

4. Java EE / Jakarta EE Annotations


Annotation Purpose Example

@EJB Injects an Enterprise JavaBean java @EJB private AccountService

(EJB) into a class. accountService;


Annotation Purpose Example

@PersistenceContext Injects an entity manager for java @PersistenceContext private

database operations. EntityManager entityManager;

@Transactional Marks a method for java @Transactional public void

transactional behavior. processOrder(Order order) { // Logic }

5. JPA Specific Annotations


Annotation Purpose Example

@NamedQuery Defines a static query for an java @NamedQuery(name = "User.findByUsername",

entity. query = "SELECT u FROM User u WHERE u.username =


:username") public class User { }

@NamedNativeQuery Defines a static SQL query for java @NamedNativeQuery(name =

an entity. "User.findByEmail", query = "SELECT * FROM users


WHERE email = :email", resultClass = User.class)
public class User { }

@Embeddable Marks a class that can be java @Embeddable public class Address { private

embedded within another String street; private String city; }

entity.

@Embedded Embeds java @Entity public class User { @Embedded

an @Embeddable object inside private Address address; }

an entity.

You might also like