Tuesday, March 17, 2015

Creating Applications with hibernate. Few things to remember

hibernate.cfg.xml configuration file.
------------------------

<?xml version="1.0" encoding="utf-8"?gt;
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"gt;
<hibernate-configurationgt;
<session-factorygt;
<property name="hibernate.bytecode.use_reflection_optimizer"gt;false</propertygt;
<property name="hibernate.connection.driver_class"gt;com.mysql.jdbc.Driver</propertygt;
<property name="hibernate.connection.password"gt;root123</propertygt;
<property name="hibernate.connection.url"gt;jdbc:mysql://localhost:3306/test1</propertygt;
<property name="hibernate.connection.username"gt;root</propertygt;
<property name="hibernate.dialect"gt;org.hibernate.dialect.MySQLDialect</propertygt;
<property name="show_sql"gt;true</propertygt;

<!-- drop and recreate databse on startup --gt;
<!-- <property name="hbm2ddl.auto"gt;create</propertygt; --gt;
<property name="hbm2ddl.auto"gt;create</propertygt;

<!-- dto classes annotated --gt;
<!-- previously <mapping resource="com/common/Stock.hbm.xml"gt;</mappinggt; --gt;
<!-- if you do not have class the u have to comment --gt;
<!-- <mapping class="com.common.Stock"gt;</mappinggt; --gt;
<mapping class="dto.user.UserDetails"gt;</mappinggt;
</session-factorygt;
</hibernate-configurationgt;

1. <property name="hbm2ddl.auto"gt;create</propertygt;

this will create schema every time code runs.
It's better to use this when you want to create the db schema for first time after that use "update"

2. entering mapping files , dto

make sure you have class file/ java files with names given with class name. Other wise it will lead to
org.hibernate.MappingException: Unable to load class
<mapping class="dto.user.ClassName"gt;</mappinggt;

-------------------------
the dto file

@Entity(name="USER_DETAILS")
public class UserDetails {

@Id
@Column(name="USER_ID")
private int userId;
@Column(name="USER_NAME")
private String userName;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

}

---------------

3. @Entity - maps to table
if you need to specify name for entity and this will name for table.
Else if needed to have Entity and Table different names then use @Table

@Entity
@Table(name="USER_DETAILS")
public class UserDetails {

4. You need to have a primary key for table, @Id is the primary key
5. @Column = this will create columns with given name. If you have not specified(this annotation is optional and without column annotation table column names will be names corresponding to DTO field name) table column will be the field name eg: userId and userName
as we have metioned columns will be USER_ID, USER_NAME respectively.

-----------
In simple below is more than enough. Thus create table name= UserDetails  with two columns userId,userName making userId as primany Key.

@Entity
public class UserDetails {

@Id

private int userId;

private String userName;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

}

6. Note: where Annotation is used.
you can use annotation in the getter or field.
Above I have used in the filed. Let's look at what gone a happen if used in getter.
To check that, I have added a check string for user name +" with Getter"




@Entity
@Table(name="USER_DETAILS")
public class UserDetails {


private int userId;

private String userName;
@Id
@Column(name="USER_ID")
public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}
@Column(name="USER_NAME")
public String getUserName() {
return userName +" with Getter";
}

public void setUserName(String userName) {
this.userName = userName;
}

}

This will add the above mentioned string to user name at each records

select * from user_Details;
USER_ID |    USER_NAME
1               |  First User with Getter

If you use Field, then field
 Note : Do not use both field and getter
I have tried. I put  
@Id
@Column(name="USER_ID") above field,
that and out put was similar to annotation over field, where check string was not appended.

7. If you do not want to add a filed to table, use @Transient

8. @Basic

9. date and time
Here "joinDate" we have not  mentioned anything, it will default map to timeStamp
result will be

joinDate
------------
2015-03-17 15:39:46

you have other options

a.) @Temporal(TemporalType.DATE)
private Date joinDate;

joinDate
------------
2015-03-17

b.) @Temporal(TemporalType.TIME)
private Date joinDate;

joinDate
------------
15:45:29

c.) @Temporal(TemporalType.TIMESTAMP)
       private Date joinDate;


joinDate
------------
2015-03-17 15:46:27



10. @Lob
this is for large String, byte stream
Mysql this mapped to "longText"

depend on situation you might have CLOB- character or BLOB - byte stream









No comments:

Post a Comment