A minimal Java project demonstrating how to connect to a MySQL database using Hibernate ORM.
The sample code shows how to create a SessionFactory, persist a User entity, and cleanly shut down the session.
- JDK 8+
- Apache Maven
- MySQL server with a database named
HibernateDB
-
Clone the repository
git clone https://github.com/PapeMarc/Vortrag_Hibernate-ORM.git cd Vortrag_Hibernate-ORM -
Configure database credentials
Editsrc/main/resources/hibernate.cfg.xmland insert your MySQL password:<property name="connection.password">{INSERT PASSWORD HERE}</property>
-
Prepare the database
Ensure thatHibernateDBexists and contains a tableuserwith columns:id(INT, auto-increment, primary key)fullname(VARCHAR)pass(VARCHAR)
mvn clean compileExecute the Main class (via your IDE or the command line). The sample Main program:
DatabaseAccess db = new DatabaseAccess();
db.createFactory();
db.createUser("Tim", "TimSeinSehrSicheresPasswort,!");
db.disposeFactory();This sequence opens a session, inserts a new User record, and closes the session factory.
src/
└─ main/
├─ java/
│ ├─ Main.java
│ └─ com/pape/hibernate/
│ ├─ data/DatabaseAccess.java
│ └─ entity/User.java
└─ resources/
└─ hibernate.cfg.xml
- The project uses
hibernate-core5.6.8.Final and the MySQL Connector/J. - Additional CRUD operations can be added to
DatabaseAccessas needed. - No license information is provided; treat the code as reference material.