Professional Documents
Culture Documents
ORM tool
Used in data layer of application
Implements JPA
The Problem
Hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property
name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property
name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Enable Hibernate's current session context -->
<property
name="current_session_context_class">org.hibernate.context.ManagedSessionConte
xt</property>
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</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>
<mapping
<mapping
<mapping
<mapping
<!--<mapping
class="com.vishal.dto.UserDetails" />
class="com.vishal.dto.Vechicle" />
class="com.vishal.dto.TwoWheeler"/>
class="com.vishal.dto.FourWheeler"/>
resource="com/vishal/dto/UserDetails.hbm.xml"/>
<mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/>
--></session-factory>
</hibernate-configuration>
@Entity(name = "USER_DETAILS")
public class UserDetails {
@Id
@Column(name = "USER_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
@Column(name = "USER_NAME")
private String userName;
@Temporal(TemporalType.DATE)
@Column(name = "JOIN_DATE")
private Date joinDate;
Hibernate picks up the value from the getter method to save into the
Database. In the below snippet of code it will insert <userName>from get as
UserName. We can also put @Column annotation to the getter method.
@Column(name = "USER_NAME")
public String getUserName() {
return userName + "from get";
}
@Transient
@Temporal(TemporalType.TIMESTAMP)
@Lob
AUTO
IDENTITY
SEQUENCE
TABLE
@Entity(name = "USER_DETAILS")
public class UserDetails {
@Id
@Column(name = "USER_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
@Column(name = "USER_NAME")
private String userName;
@Embedded
private Address address;
@Embeddable
public class Address {
@Column(name="STREEET")
private String street;
If I have two Address Objects inside the User class (HomeAddress and
OfficeAddress)
In this case there will be two STREET columns. Will override the attribute.
@Embedded
@AttributeOverrides({
@AttributeOverride(name="street",column=@Column(name="HOME_STREET")),
@AttributeOverride(name="state",column=@Column(name="HOME_STATE")),
@AttributeOverride(name="pinCode",column=@Column(name="HOME_PIN")),
@AttributeOverride(name="city",column=@Column(name="HOME_CITY"))
})
private Address homeAdress;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="street",column=@Column(name="OFFICE_STREET")),
@AttributeOverride(name="state",column=@Column(name="OFFICE_STATE")),
@AttributeOverride(name="pinCode",column=@Column(name="OFFICE_PIN")),
@AttributeOverride(name="city",column=@Column(name="OFFICE_CITY"))
})
private Address officeAdress;