Instantly share code, notes, and snippets.

Embed
What would you like to do?
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