Vous êtes sur la page 1sur 4

Hibernate JPA Annotations: javax.persisttence.

1. @Entity: Annotate all your entity beans(class) with @Entity.


2. @Table: specify database table
3. @Column: column
4. @Id: primary key
5. @GeneratedValue: auto increment the id column
6. @Version: Control versioning or concurrency using @Version annotation.
7. @OrderBy: sort your data
8. @Transient: transient properties
9. @Lob: Large object

Hibernate Association Mapping Annotations


1. @OneToOne : use @PrimaryKeyJoinColumn
2. @OneToMany
3. @ManyToOne: @JoinColumn
4. @ManyToMany
5. @PrimaryKeyJoinColumn: associated entities sharing the same primary key.
6. @JoinColumn: one to one or many to one, foreign key is held by some other entity
7. @JoinTable: Use @JoinTable and mappedBy for entities linked through an association table.
8. @MapsId: Persist two entities with shared key (when one entity holds a foreign key to the
other) using @MapsId Annotation
9.

Ex: one to one


@Entity
@Table(name = "company")
public class Company implements Serializable {

@Id
@Column(name = "id")
@GeneratedValue
private int id;

@OneToOne(cascade = CascadeType.MERGE)
@PrimaryKeyJoinColumn
private CompanyDetail companyDetail;

...
}

@Entity
@Table(name = "companyDetail")
public class CompanyDetail implements Serializable {

@Id
@Column(name = "id")
private int id;

...
}
Spring Annotations

1. @Configuration: Used to indicate that a class declares one or more @Bean methods. These
classes are processed by the Spring container to generate bean definitions and service requests
for those beans at runtime.
2. @Bean: Indicates that a method produces a bean to be managed by the Spring container. This
is one of the most used and important spring annotation. @Bean annotation also can be used
with parameters like name, initMethod and destroyMethod.
 name – allows you give name for bean
 initMethod – allows you to choose method which will be invoked on context register
 destroyMethod – allows you to choose method which will be invoked on context
shutdown

For example:

@Configuration
public class AppConfig {

@Bean(name = "comp", initMethod = "turnOn",


destroyMethod = "turnOff")
Computer computer(){
return new Computer();
}
}
public class Computer {

public void turnOn(){


System.out.println("Load operating system");
}
public void turnOff(){
System.out.println("Close all programs");
}
}

3. @PreDestroy and @PostConstruct are alternative way for bean initMethod and
destroyMethod. It can be used when the bean class is defined by us. For
example;
4. public class Computer {
5.
6. @PostConstruct
7. public void turnOn(){
8. System.out.println("Load operating system");
9. }
10.
11. @PreDestroy
12. public void turnOff(){
13. System.out.println("Close all programs");
14. }
15. }
16. @ComponentScan: Configures component scanning directives for use with
@Configuration classes. Here we can specify the base packages to scan for spring
components.
17. @Component: Indicates that an annotated class is a “component”. Such classes are
considered as candidates for auto-detection when using annotation-based
configuration and classpath scanning.
18. @PropertySource: provides a simple declarative mechanism for adding a property source
to Spring’s Environment. There is a similar annotation for adding an array of property
source files i.e @PropertySources.
19. @Service: Indicates that an annotated class is a “Service”. This annotation serves as a
specialization of @Component, allowing for implementation classes to be autodetected
through classpath scanning.
20. @Repository: Indicates that an annotated class is a “Repository”. This annotation serves
as a specialization of @Component and advisable to use with DAO classes.
21. @Autowired: Spring @Autowired annotation is used for automatic injection of beans.
Spring @Qualifier annotation is used in conjunction with Autowired to avoid confusion
when we have two of more bean configured for same type.
22. @Required annotation applies to bean property setter methods and it indicates that the
affected bean property must be populated in XML configuration file at configuration
time. Otherwise, the container throws a BeanInitializationException exception.
Following is an example to show the use of @Required annotation.

Spring MVC Annotations:

1. @Controller: controller class, spring mvc


2. @Service: service class, business logic
3. @Repository: DAO class, Database access logic
4. @RequestMapping: map URLs onto an entire class or a particular handler method.
5. @PathVariable: @PathVariable spring annotation on a method argument to bin
6. @RequestParam: You can bind request parameters to method variables using spring
annotation @RequestParam.
7. @ModelAttribute: An @ModelAttribute on a method argument indicates the
argument should be retrieved from the model. If not present in the model, the
argument should be instantiated first and then added to the model. Once present in
the model, the argument's fields should be populated from all request parameters that
have matching names. This is known as data binding in Spring MVC, a very useful
mechanism that saves you from having to parse each form field individually.
8. @SessionAttribute: @SessionAttributes spring annotation declares session attributes.
This will typically list the names of model attributes which should be transparently
stored in the session, serving as form-backing beans between subsequent requests
9. @RequestBody and @ResponseBody: maps the HttpRequest/HttpResponsebody to a
transfer or domain object,
10. @RequestHeader and @ResponseHeader: to access header fields

Ex:
@Controller
@RequestMapping("/owners/{ownerId}/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetForm {

@ModelAttribute("types")

public Collection<PetType> populatePetTypes() {


return this.clinic.getPetTypes();
}

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet,
BindingResult result, SessionStatus status) {
new PetValidator().validate(pet, result);
if (result.hasErrors()) {
return "petForm";
}else {
this.clinic.storePet(pet);
status.setComplete();
return "redirect:owner.do?ownerId="
+ pet.getOwner().getId();
}
}
}

Spring Boot Annotations


1. @SpringBootApplication
2. @EnableAutoConfiguration

REST Annotations: JAX -RS Annotations: javax.ws.*

1. @GET @POST @PUT @PATCH


2. @Produces @Consume: Type of o/p(/i/p) of this method
3. @Path :URL on which method will be invoked
4. @PathParam: bind REST-style URL parameters to method arguments using PathParam
5. @QueryParam: Request parameters in query string can be accessed using
@QueryParam annotation as shown below.
6. @GetMapping: shortcut for @RequestMapping(method = RequestMethod.GET).
7. @PostMapping:
8. @PutMapping:
9. @DeleteMapping:

Vous aimerez peut-être aussi