Example of @Target in Hibernate

By Arvind Rai, May 29, 2013
@Target forces the entity field to return specified class. By default reflection returns the object of the class as return type. Suppose a field has a return type an interface but we want to use the implementation class. In that case @Target plays a role. Find the example how to use.
@Entity
@Table(name="lion")
public class Lion {
	@Id
	@Column(name = "id")
	private int id;

	@Embedded
	@Target(AnimalImpl.class)
	private Animal animal;
}
 
Animal is an interface and AnimalImpl is the class which implements Animal. The property animal has been annotated by @Target(AnimalImpl.class), so the field will return AnimalImpl.
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us