Posts

Showing posts from December, 2019

Hibernate and Primary Keys

Image
Hibernate and Primary Keys Primary Key :  Uniquely identifies each row in a table  Must be a unique value Cannot contain NULL values MySQL - Auto Increment Hibernate Identity - Primary Key ID Generation Strategies You can define your own CUSTOM generation strategy :-) Create implementation of org.hibernate.id.IdentifierGenerator Override the method: public Serializable generate(…)

How to read Dates with Hibernate

Handling Dates with Hibernate You can make use of a combination of Java's date formatting class and Hibernate annotations. Sample output: Student [id=50, firstName=Paul, lastName=Doe, email=paul@luv2code.com, dateOfBirth=null] Student [id=51, firstName=Daffy, lastName=Duck, email=daffy@luv2code.com, dateOfBirth=null] Student [id=52, firstName=Paul, lastName=Doe, email=paul@luv.com, dateOfBirth=31/12/1998] Development Process Overview 1. Alter database table for student 2. Add a date utils class for parsing and formatting dates 3. Add date field to Student class 4. Add toString method to Student class 5. Update CreateStudentDemo ---- Detailed steps 1. Alter database table for student We need to alter the database table to add a new column for "date_of_birth". Run the following SQL in your MySQL Workbench tool. ALTER TABLE `hb_student_tracker` . `student` ADD COLUMN `date_of_birth` DATETIME NULL AFTER `last_name` ; -- 2....

Build Hibernate Applications

Image
Hibernate What is Hibernate? • A framework for persisting / saving Java objects in a database Benefits of Hibernate • Hibernate handles all of the low-level SQL • Minimizes the amount of JDBC code you have to develop • Hibernate provides the Object-to-Relational Mapping (ORM) Hibernate and JDBC • Hibernate uses JDBC for all database communications SET UP YOUR ENVIRONMENT Required Software To Build Hibernate Applications, you need the following: 1. Java Integrated Development Environment (IDE) 2. Database Server 3. Hibernate JAR files and JDBC Driver Setup Hibernate in Eclipse: 1. Create Eclipse Project 2. Download Hibernate Files 3. Download MySQL JDBC Driver 4. Add JAR files to Eclipse Project … Build Path Testing The JDBC Connection Create class for TestJdbc package com.luv2code.com.jdbc; import java.sql.Connection; import java.sql.DriverManager; public class TestJdbc { public static void main(String[] args) { String jdbcUrl = ...