Querying is one of most important part in Hibernate. For this purpose we have to use HQL (Hibernate Query Language). HQL is simpler than SQL and bit differ from SQL. Here in HQL we consider on Classes instead of Tables and we consider on Attributes instead of Columns.
Lets see how to querying from Hibernate.
For this example I have used a College , Student relation which is a one to many association.
Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts
Friday, January 6, 2012
Thursday, January 5, 2012
Wednesday, January 4, 2012
Monday, January 2, 2012
Inheritance Mapping
Basically there are 3 types of inheritance mapping. We may use those methods suitably.
- SINGLE_TABLE (default)
- JOINED
- TABLE_PER_CLASS
Labels:
Annotations,
Embeded,
Hibernate,
Inheritance,
JPA,
Mapping,
Mysql
Sunday, January 1, 2012
Compound Primary Keys in HIBERNATE
Most of the time we need only 1 primary key. But there are some situations we may need compound primary keys.
Here in hibernate it is simple. We can use 2 classes into 1 table strategy.
Here in hibernate it is simple. We can use 2 classes into 1 table strategy.
Account.java
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Account {
private AccountCompoundKey compoundKey;
private int accBalance;
@Id
public AccountCompoundKey getCompoundKey() {
return compoundKey;
}
public void setCompoundKey(AccountCompoundKey compoundKey) {
this.compoundKey = compoundKey;
}
public int getAccBalance() {
return accBalance;
}
public void setAccBalance(int accBalance) {
this.accBalance = accBalance;
}
}
Labels:
Annotations,
Compound Primary Key,
Embeded,
Hibernate,
JPA,
Mysql
Saturday, December 31, 2011
One table for 2 or More Classes
In some situations we may need create 1 table out of 2 or more classes. for instance when we map inheritance.
New Annotations :
New Annotations :
- @Embeddable : when a class is going to be embedded inside another persistence class we have to annotate the class as embeddable
- @Embedded : When some (object) field is going to persist inside a class. The object type must annotated as embeddable
Labels:
Annotations,
Embeded,
Hibernate,
JPA,
Mysql,
Primary Key
One Class Into 2 Tables
In some cases we have to create 2 tables out of one class. In such cases those tables must must maintain the referential integrity.
New Annotations Used :
New Annotations Used :
- @Table(name="Customer") : Main table (1st table)
- @SecondaryTable(name="CustomerDetail") : Secondary table
- @Column(table="CustomerDetail") : Mark the fields which are going to maintain in the second table
Genetating Primary Key (HIBERNATE Note 2)
Here I am going to note down how to generate primary keys in hibernate.
Q : How do I drop a schema?
ok lets see how to annotate a class to generate primary keys.
Note : Useful annotations
Q : How do I drop a schema?
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Student.class);
config.configure("hibernate.cfg.xml");
new SchemaExport(config).drop(true, true);
ok lets see how to annotate a class to generate primary keys.
Note : Useful annotations
- @Table (name="TableName") : Specify a name for the table
- @Column(name="ColumnName") : Specify a column name
- @Column(nullable=false) : the specified column cannot be empty
- @Transient : The relevant attribute is volatile. not persistent
- @Basic : default annotation. if we don't give annotation. default one become Basic
- @Temporal(TemporalType.DATE) : used to annotate date types
- @Temporal(TemporalType.TIME) : used to annotate time
- @Temporal(TemporalType.TIMESTAMP) : used to annotate date and time (time stamp)
- @GeneratedValue : generate values automatically
- @GeneratedValue(strategy=GenerationType.TABLE, generator="stdId") most DBs support this type. and generate keys in a sequence. SEQUENCE type is not supported by DBDerby.
Labels:
Annotations,
Hibernate,
JPA,
Mysql,
Primary Key
Getting started with HIBERNATE note 1
The best Hibernate video tutorial I have ever seen.
http://www.youtube.com/user/patrickwashingtondc
I have configured the user library and hibernate.cfg.xml for the MySQL.
http://www.youtube.com/user/patrickwashingtondc
I have configured the user library and hibernate.cfg.xml for the MySQL.
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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatesample</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- define default schema -->
<property name="hibernate.default_schema">hibernatesample</property>
Subscribe to:
Posts (Atom)




