5. Revise Top-Down (Tester → DAO → Entity → DB):
Session
to perform database operations.@Entity
, @Table
, @Id
).hibernate.cfg.xml
(update password, URL, etc.).Session
to interact with entities → Entities map to DB tables.6. Create Entity, DAO, and Tester:
Product
(detailed in Day 8), create the entity, DAO, and testers as described below.hibernate_template_project
pom.xml
.Update hibernate.cfg.xml
:
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_db</property>
<property name="hibernate.connection.username">your_username</property>
<property name="hibernate.connection.password">your_password</property>
HibernateUtils
ClassUtility class to manage SessionFactory
:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
getSessionFactory().close();
}
}
Product
Entityid
: Long (auto-increment)name
: String (unique, varchar(20))description
: String (varchar(300))manufactureDate
: LocalDateprice
: doubleavailableQuantity
: intcategory
: Enum (STATIONARY, SHOES, GRAINS, OIL)