@DiscriminatorFormula in Hibernate Annotation

By Arvind Rai, February 05, 2013
@DiscriminatorFormula can be used to provide the case while fetching the data from the database. It provides the formula for discriminator resolution. @DiscriminatorFormula is used at table label. I will show the use of @DiscriminatorFormula in the scenario that there is a Person entity. It has age column than can be null but we can provide DiscriminatorFormula to pick 0 when it gets null values in database.
@Entity
@Table(name="person")
@DiscriminatorFormula("case when age is null then 0 else age end")
public class Person implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue
	@Column(name="person_id")
	private int personId;
	
	@Column(name="name")
	private String name;
	
	@Column(name="age")
	private Integer age;
}
 
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us