Amazon

Sunday, June 17, 2012

More than one entity in Hibernate fetch

Can we do this kind of query in hibernate:select * from ticket, ticket_audit where ticket.ticket_id = ticket_audit.ticket_id;


Ticket and TicketAudit are related with OneToMany releationship, i.e. there can be many records in TicketAudit with respect to one Ticket. Below is the class definition


@Entity
@Table(name="hd_ticket")
public class Ticket implements Serializable {

private String  ticketID;
private Set auditTrails;

        @OneToMany(targetEntity=AuditTrail.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name = "TICKET_ID")
public Set getAuditTrails() {
return auditTrails;
}
public void setAuditTrails(Set auditTrails) {
this.auditTrails = auditTrails;
}
        
       @Id 
@Column(name="Ticket_Id")
public String getTicketID() {
return ticketID;
}
public void setTicketID(String ticketID) {
this.ticketID = ticketID;
}
@Column(name="Date_Created")
public String getDateCreated() {
return dateCreated;
}
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
}


@Entity
@Table(name="hd_audit_trail")
public class AuditTrail implements Serializable {
private String ticketID;
       
       @Id
@Column(name="Ticket_Id")
public String getTicketID() {
return ticketID;
}
public void setTicketID(String ticketID) {
this.ticketID = ticketID;
}
@Column(name="Revision_Date_time")
public String getRevisionDatetime() {
return revisionDatetime;
}
public void setRevisionDatetime(String revisionDateTime) {
this.revisionDatetime = revisionDateTime;
}
}


The method definition of DAO is as below 
@SuppressWarnings("unchecked")
public List getTickets(final String userID, final String statusID) throws AppException{
if(logger.isDebugEnabled()){
logger.debug("AudtiTrial userID:"+userID);
logger.debug("getHibernateTemplate():"+getHibernateTemplate());
}
List results=null;
try {
results = (List)getHibernateTemplate().find(" from com.helpdesk.domain.Ticket ticket where creator='"+userID+"' and statusID LIKE '"+statusID+"'");
} catch (RuntimeException e) {
logger.fatal("RuntimeException", e);
throw new AppException(e);
}
if(logger.isDebugEnabled()){
logger.debug("TicketAudti details is :"+results);
}
if(results==null){
results = new ArrayList();
}
return results;
}

Although I did all the setups but I was getting below exception 

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.helpdesk.domain.Ticket.auditTrails, no session or session was closed

        @OneToMany(targetEntity=AuditTrail.class, cascade=CascadeType.ALL)

The above annotation was having one flag missing and that is, fetch=FetchType.EAGER. Add this property in your annotation and enjoy cascade data fetch. 
        @OneToMany(targetEntity=AuditTrail.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)

Chillex.


Friday, May 25, 2012

Reading properties files in Spring


1. Inject to properties file in Spring Bean using PropertyPlaceholderConfigurer

bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    
        classpath:com/foo/jdbc.properties
    

2. Use the injected properties in your bean class. Here taking example of dataSource.

    ${jdbc.driverClassName}"/>
    ${jdbc.url}"/>
    ${jdbc.username}"/>
    ${jdbc.password}"/>

3. The Content of properties file is below 

The actual values come from another file in the standard Java
        Properties format:
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

Fetching create table script of Oracle Tables

spool off
/
-----Open file to dump the output of procedure
spool table_struct_data.txt
/

set serveroutput on
/
declare
       l_data varchar2(10000);
       l_clob clob;
begin
----------select the DDL script of table using GET_DDL Oracle Library
       select DBMS_METADATA.GET_DDL('TABLE','MASTER_TAB') into
l_clob from DUAL;
       l_data := dbms_lob.substr( l_clob, 4245, 1 );
       dbms_output.put_line( l_data);

end;
/
spool off


open the file table_struct_data.txt . It will contain the create script of table MASTER_TAB

Saturday, May 5, 2012

Exporting mysql Database to .sql file

Exporting DB 
Taking dump of all tables of a Database. For Example - DB name is development_studio

C:\>mysqldump --single-transaction -u sanjeev -p development_Studio > dump.sql
Enter password: *******

--single-transaction option is above command make sure that taking dump does not give TABLE LOCK error. For example

>mysqldump  -u sanjeev -p development_Studio > dump.sql
Enter password: *******
mysqldump: Got error: 1044: Access denied for user 'sanjeev'@'localhost' to database 'development_studio' when using LOCK TABLES


Another option is to grant LOCK TABLES to your user:
c:\>mysql -u root -p

mysql> GRANT SELECT,LOCK TABLES ON DBNAME.* TO 'username'@'localhost'; 

Monday, February 27, 2012

Core Java - Key Performance Optimization

  1. Create Objects locally (method level) as much as possible. Keeping objects at Object Level or Class Level blocks the memory for the life time of Object or till class gets unloaded.
  2. List should be browsed using size() and index in for loop or other loops, rather than using Iterator to iterate the List. Using index is faster than using Iterator.
  3. When large size of text are used in program, always prefer StringBuffer than String. StringBuffer objects will get deleted once the program ends, but String objects don't get deleted from the JVM. Read Perm-Generation for more details.
  4. Never say hashMap.containsKey("key") to see the item is present in map or not and after that retrieve it using hashMap.get("key"). containsKey() and get() use same methods internally to find elements in a map. Better do hashMap.get("key") and do null check on the value retrieved from the hashMap using key.

Amazon Best Sellors

TOGAF 9.2 - STUDY [ The Open Group Architecture Framework ] - Chap 01 - Introduction

100 Feet View of TOGAF  What is Enterprise? Collection of Organization that has common set of Goals. Enterprise has People - organized by co...