Dialect: It is
configuration by which hibernate knows what language needs to use to
communication with DB.
Some of the databases have different SQLs to communicate with DB eg: Oracle and postGreSQL. Hibernate uses Dialect to write SQLs and execute queries.
Some of the databases have different SQLs to communicate with DB eg: Oracle and postGreSQL. Hibernate uses Dialect to write SQLs and execute queries.
* Below Dialect is used for PostgreSQL DB
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
* If table doesn’t present it will create the table in
DB
<!-- Drop
and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
* We need to list out all the MODEL classes
<!-- Names
the annotated entity class -->
<mapping class="org.yash.dto.UserDetails"/>
@Entity -> It is used for Mapping annotation
class
(@Entity declares the class as an entity (i.e. a
persistent POJO class))
What is an Entity?
An entity is something that has a distinct, separate
existence. An entity could be viewed as a set containing subsets. In
philosophy, such sets are said to be abstract objects. In simple language it is Plain Old Java objects without any business
logic inside it and mainly used as a Data carry object or value object with
may represent the database tuple in form of an object.
Rules for Writing
Entity
It is POGO with annotations mapping :-
- It must have default constructors.
- It should have getter/setters for all the fields.
- It should not contain static or final fields.
@Id ->
It is used to define variable name as primary key in database.
(@Id declares the identifier property of this entity.)
right click->new file->hibernate.cfg.xml->finish
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/hibernated
</property>
<property
name="connection.username">admin</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>
<property
name="cache.use_query_cache">true</property>
<property
name="cache.use_second_level_cache">true</property>
<property
name="cache.use_structured_entries">true</property>
<property
name="cache.region.factory_class">
org.hibernate.cache.EhCacheRegionFactory
</property>
<property
name="net.sf.ehcache.configurationResourceName">
/hibernate-config/ehcache.xml
</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>
<!-- Names the annotated entity class -->
<mapping class="org.yash.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>
UserDetails.java
package org.yash.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {
@Id
private int userId;
private String userName;
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;
}
}
No comments:
Post a Comment