Skip to content

Instantly share code, notes, and snippets.

@wmuron
Created October 20, 2016 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmuron/da05f51926a85ba9c7f794afe6d81522 to your computer and use it in GitHub Desktop.
Save wmuron/da05f51926a85ba9c7f794afe6d81522 to your computer and use it in GitHub Desktop.
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
//prepare date
String createdAtUtcStr = "2016-04-24 9:54:23";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = format.parse(createdAtUtcStr);
//prepare test user to save into DB
String name = "Montrose";
User user = new User(1, name, date);
//save using Hibernate
session.save(user);
//save using native SQL
String sql = "INSERT INTO users (id, name, created_at_utc) VALUES ('2', '"
+ name + "', '" + createdAtUtcStr + "')";
Query query = session.createSQLQuery(sql);
query.executeUpdate();
session.flush();
transaction.commit();
session.close();
factory.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment