Skip to content

Instantly share code, notes, and snippets.

@bayraktugrul
Last active September 23, 2018 12:12
Show Gist options
  • Save bayraktugrul/fd32442a78b6944c451544548e58b966 to your computer and use it in GitHub Desktop.
Save bayraktugrul/fd32442a78b6944c451544548e58b966 to your computer and use it in GitHub Desktop.
Hibernate #2 - Medium (Saving object to DB example)
package com.hibernateyazilari.hibernate_2;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.internal.SessionFactoryServiceRegistryBuilderImpl;
public class App {
public static void main( String[] args ) {
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
Session session = factory.getCurrentSession();
try {
Student student = new Student("Tuğrul","Bayrak", "mail@mail.com");
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
} finally {
factory.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment