Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DEP: org.hibernate hibernate-core 5.2.10.final
- // DEP: org.hsqldb hsqldb 2.4.0
- // DEP: dom4j dom4j 1.6.1
- // DEP: org.codehaus.woodstox woodstox-core-asl 4.4.1
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.util.HashMap;
- import java.util.Map;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.boot.MetadataSources;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- @Entity
- class A {
- public int id;
- public int x;
- public int y;
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public String toString() {
- return String.format("[%s, %s, %s]", id, x, y);
- }
- }
- public class Main {
- public static void main(String[] args) throws Exception {
- runSql("drop schema public cascade");
- runSql("create table a ( id int generated by default as identity, x int, y int );");
- runSql("insert into a values ( null, 1, 2 )");
- runSql("insert into a values ( null, 3, 4 )");
- SessionFactory sessionFactory = getSessionFactory();
- try {
- Session session = sessionFactory.openSession();
- try {
- System.out.println(session.createQuery("from A").list());
- } finally {
- session.close();
- }
- } finally {
- sessionFactory.close();
- }
- }
- private static SessionFactory getSessionFactory() {
- Map<String, String> config = new HashMap<>();
- config.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");
- config.put("hibernate.connection.username", "root");
- config.put("hibernate.connection.password", "");
- config.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
- return new MetadataSources(new StandardServiceRegistryBuilder().applySettings(config).build())
- .addAnnotatedClass(A.class)
- .buildMetadata()
- .buildSessionFactory();
- }
- private static void runSql(String sql) throws Exception {
- Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:test", "root", "");
- try {
- PreparedStatement statement = connection.prepareStatement(sql);
- try {
- statement.execute();
- } finally {
- statement.close();
- }
- } finally {
- connection.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement