Skip to main content

Posts

Showing posts with the label JPA

@AttributeOverrides annotation in hibernate/JPA

@AttributeOverrides annotation is used in hibernate/JPA to rename the column(s) name of the embadable class while it is being embedded in other class. Suppose A class is embedded in class B, then if A entity contains the column name of any property "phone", then B entity can override the column name to "mobileNumber". So, in the table, column name will be "mobileNumber". package  entity; import  java.io. Serializable ; import  javax.persistence.Column; import  javax.persistence.Embeddable; @Embeddable public   class  ContactKey  implements   Serializable   {      private   int  roll;      private   String  sem;      private   String  branch;      public   int   getRoll ()   {          return  roll;      }      public...

Hibernate (or JPA) - Using "mappedBy" attribute of mapping annotations(like @OneToOne, @OneToMany, @ManyToMany)

Using " mappedBy " attribute of mapping annotations(like @OneToOne, @OneToMany, @ManyToMany) for bi-directional relationship. This attribute allows you to refer the associated entities from both sides. If "X" has association with "Y" then you can get X from Y and Y from X. For example, If you have "Book" entity and "Author" entity those are associated to each other in the way that Book has a Author and Author associated with a Book. Now if you retrieve the Book Object from hibernate session, then you can get the Author entity from Book entity. Or if you get the Author entity then you can get the Book entity from Author entity. So you require the bidirectional navigation relationships between Book and Author entities. This is achieved in the Hibernate using the  @OneToOne relationship provided that child entity must have property type of parent and marked with annotation @OneToOne(mappedBy="parent") where parent is the Ow...

Using JPQL in JPA

This example is using JPQL and executing select query to get multiple result from table. Here Employee is the name of entity class not a table name in database and emp is a alias for Employee entity. Query object is used to represent a query and it has getResultList method that returns list of multiple entities for each row fetched from database using JPA. import  java.util. * ; import  javax.persistence. * ; public   class  JavaApp2 {      public   static   void   main ( String  ar []){                  EntityManagerFactory emf = Persistence. createEntityManagerFactory ( "persistunit" ) ;         EntityManager em = emf. createEntityManager () ;         Query q = em. createQuery ( "SELECT OBJECT(emp) FROM Employee emp" ) ;  ...

Generating Primary keys in JPA

Every entity that is mapped to a relational database must have a mapping to a primary key in the table. Primary Key Types JPA provides the Table , Sequence, Auto, and Identity strategies for generating primary keys. Table Strategy With this strategy the persistence engine uses a relational database table from which the keys are generated. This strategy has the advantage of portability; we can use it with any relational database. Sequence Strategy Some databases, such as Oracle, have a built-in mechanism called sequences for generating keys. To invoke such a sequence we need to use the @SequenceGenerator annotation. For example:   @SequenceGenerator(name="USER_SEQ", sequenceName="USER_SEQUENCE") Identity Strategy Some databases, such as Microsoft SQL Server , use an identity column for generating keys. To use this we specify the IDENTITY strategy in the @GeneratedValue annotation:   @GeneratedValue(strategy=GenerationType.IDENTITY) Note that there is no gener...