Session 3 - Hibernate - Configuration With Annotations
Session 3 - Hibernate - Configuration With Annotations
CONFIGURATION WITH
ANNOTAION
Hibernate Configuration with Annotations
hibernate.connection.driver_class = your.database.driver.Class
hibernate.connection.url =
jdbc:mysql://localhost:3306/your_database
hibernate.connection.username = your_username
hibernate.connection.password = your_password
# Hibernate properties
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto = update
3. Create the Hibernate configuration
class:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
}
5. Perform database operations:
– You can now use the SessionFactory to
create Session instances
– And perform database operations.
– Example Follows :
import org.hibernate.Session;
import org.hibernate.Transaction;
try {
Transaction transaction = session.beginTransaction();
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
}