Tuesday, June 11, 2013

05 - More-Annotations



@Table (name="USER_DETAILS")
You can add table name using above annotation also.
@Entity is used to entire class and @Table is for particular table.

@Basic
Hibernate treat the variable as persistence.
 It will be used to configure other options like fetch(FetchType) and optional(boolean).
We will see more details about this in coming tutorials.
Every non static non transient property (field or method) of an entity bean is considered persistent, unless you annotate it as @Transient. Not having an annotation for your property is equivalent to the appropriate @Basic annotation. The @Basic annotation allows you to declare the fetching strategy for a property

@Transient
private String userName;  or  private static String userName;
By adding this annotation you can skip that column in DB. “userName” column will not be created in DB.

@Temporal (TemporalType.DATE)
private Date joinedDate;
It will save only Date not time in DB. Eg: "2013-06-04"

@Temporal (TemporalType.TIME)
private Date joinedDate;
To save only time in DB. Eg: "15:55:55.415"

@Lob
private String description;
It is nothing but large object. Eg: byte stream.

public transient int counter; //transient property

private String firstname; //persistent property

@Transient
String getLengthInMeter() { ... } //transient property

String getName() {... } // persistent property

@Basic
int getLength() { ... } // persistent property

@Basic(fetch = FetchType.LAZY)
String getDetailedComment() { ... } // persistent property

@Temporal(TemporalType.TIME)
java.util.Date getDepartureTime() { ... } // persistent property          

@Enumerated(STRING)
Starred getNote() { ... } //enum persisted as String in database

************************************************
UserDetails.java

package org.yash.dto;

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity (name="USER_DETAILS")
public class UserDetails {
      @Id
      private int userId;
      private static String userName;
      @Temporal (TemporalType.TIME)
      private Date joinedDate;
      private String Address;
      @Lob
      private String description;
     
      public Date getJoinedDate() {
            return joinedDate;
      }
      public void setJoinedDate(Date joinedDate) {
            this.joinedDate = joinedDate;
      }
      public String getAddress() {
            return Address;
      }
      public void setAddress(String address) {
            Address = address;
      }
      public String getDescription() {
            return description;
      }
      public void setDescription(String description) {
            this.description = description;
      }
           
      public void setUserId(int userId) {
            this.userId = userId;
      }
      public int getUserId() {
            return userId;
      }
      public void setUserName(String userName) {
            this.userName = userName;
      }
      public String getUserName() {
            return userName;
      }
}


************************************************
HibernateTest.java

package org.yash.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.yash.dto.UserDetails;

public class HibernateTest {

      public static void main(String args[]){
            UserDetails user = new UserDetails();
            user.setUserId(3);
            user.setUserName("Third User");
            user.setAddress("Bentonville");
            user.setJoinedDate(new Date());
            user.setDescription("Description of user goes here");
           
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
           
            Session session = sessionFactory.openSession();
            /* It is used to save all the objects and to define single unit of work */
            session.beginTransaction();
           
            session.save(user);
            session.getTransaction().commit();
      }
}

************************************************

************************************************
hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.driver_class">
org.postgresql.Driver
  </property>
        <property name="connection.url">
            jdbc:postgresql://localhost:5433/hibernatedb
        </property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">admin</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.PostgreSQLDialect
        </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
       
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
       
        <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">create</property> 
<!-- create / update -->
   
        <!-- Names the annotated entity class -->
        <mapping class="org.yash.dto.UserDetails"/>  

    </session-factory>

</hibernate-configuration>


************************************************















No comments:

Post a Comment