JPA NamedNativeQuery and NamedNativeQueries annotations

The @NamedNativeQuery and @NamedNativeQueries annotations are used to define one or more SQL queries. These queries will be associated with that particular entity. @NamedNativeQuery Example: @Entity @NamedNativeQuery (name="Student.selectAllStudents", query="SELECT st FROM STUDENTS st") public class Students { … }@Entity @NamedNativeQuery (name="Student.selectAllStudents", query="SELECT st FROM STUDENTS st") public class Students { … } @NamedQueries Example: @Entity … Read more

Categories JPA

JPA NamedQuery and NamedQueries annotations

The @NamedQuery and @NamedQueries annotations are used to define one or more JPQL queries. These queries will be associated with that particular entity. @NamedQuery Example: @Entity @NamedQuery(name="Student.selectAllStudents", query="SELECT st FROM Students st") public class Students { … }@Entity @NamedQuery(name="Student.selectAllStudents", query="SELECT st FROM Students st") public class Students { … } @NamedQueries Example: @Entity @NamedQueries({ @NamedQuery(name="Student.selectAllStudents", … Read more

Categories JPA

JPA ManyToMany annotation

The @ManyToMany annotation is used to implement Many-to-Many relationship. Two entities are said to be in Many-to-Many relationship if many occurrence of entity belongs to many occurrence of other entity and vice versa. Example: // In Customer class: @ManyToMany @JoinTable(name="CUST_PHONES") public Set<PhoneNumber> getPhones() { return phones; }   // In PhoneNumber class: @ManyToMany(mappedBy="phones") public Set<Customer> … Read more

Categories JPA

JPA ManyToOne annotation

The @ManyToOne annotation is used to implement Many-to-One relationship. Two entities are said to be in Many-to-One relationship if one entity belongs to many occurrence of other entity. Note: One-to-Many and Many-to-One are both same it depends upon the side of entities in the relationship. As one department – many employees refers to the One-to-Many … Read more

Categories JPA

JPA OneToMany annotation

The @OneToMany annotation is used to implement One-to-Many relationship. Two entities are said to be in One-to-Many relationship if one entity has many occurrence in other entity. Example: // In Customer class: @OneToMany(targetEntity=com.example.Order.class, cascade=ALL, mappedBy="customer") public Set getOrders() { return orders; }   // In Order class: @ManyToOne @JoinColumn(name="CUST_ID", nullable=false) public Customer getCustomer() { return … Read more

Categories JPA

JPA OneToOne annotation

The @OneToOne annotation is used to implement One-to-One relationship. Two entities are said to be in One-to-One relationship if one entity has only one occurrence in other entity. Example: // On Customer class: @OneToOne(optional=false) @JoinColumn( name="CUSTREC_ID", unique=true, nullable=false, updatable=false) public CustomerRecord getCustomerRecord() { return customerRecord; }   // On CustomerRecord class: @OneToOne(optional=false, mappedBy="customerRecord") public Customer … Read more

Categories JPA

JPA Temporal annotation

The @Temporal annotation is specified for Date and Calendar type persistent. It can have one of following three values DATE, TIME, and TIMESTAMP. Default is TIMESTAMP. Example: @Temporal(TemporalType.TIME) java.util.Date createDtae;@Temporal(TemporalType.TIME) java.util.Date createDtae;

Categories JPA

JPA Transient annotation

It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class which is not persistent. Example: @Entity public class Employee { @Id int id; @Transient String testField; … }@Entity public class Employee { @Id int id; @Transient String testField; … } In case of above example, testField property … Read more

Categories JPA

JPA column annotation

The Column annotation is used to specify the mapped column for a persistent property or field. If no Column annotation is specified, the default value will be applied. Example: @Column(name="DESC", nullable=false, length=512) public String getDescription() { return description; }@Column(name="DESC", nullable=false, length=512) public String getDescription() { return description; } Attribute: Name: The name of the column. … Read more

Categories JPA

JPA Table annotation

The Table annotation is used to specify the primary table for the annotated entity. Additional tables may be specified using SecondaryTable or SecondaryTables annotation. Example: @Entity @Table(name="Student") public class Student {   …   }@Entity @Table(name="Student") public class Student { … } Attribute: Name: The name of the table. It is an optional attribute. Schema: … Read more

Categories JPA