Hibernate Association Mapping Annotations
Hibernate Association Mapping Annotations
@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 {
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.
Ex:
@Controller
@RequestMapping("/owners/{ownerId}/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetForm {
@ModelAttribute("types")
@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();
}
}
}