Created
October 20, 2016 13:33
-
-
Save wmuron/da05f51926a85ba9c7f794afe6d81522 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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