Skip to content

Instantly share code, notes, and snippets.

@nitincoded
Created January 7, 2017 16:31
Show Gist options
  • Save nitincoded/2372ddca34e4f2f2ac02dc50a4b91929 to your computer and use it in GitHub Desktop.
Save nitincoded/2372ddca34e4f2f2ac02dc50a4b91929 to your computer and use it in GitHub Desktop.
Hibernate without XML
import com.fasterxml.classmate.AnnotationConfiguration;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import java.util.Properties;
/**
* Created by Developer on 1/7/17.
*/
public class GrizzlyHelper {
private static final SessionFactory concreteSessionFactory;
static {
try {
Properties prop = new Properties();
prop.setProperty("hibernate.connection.url", "jdbc:mysql://192.168.0.101:3306/go");
prop.setProperty("hibernate.connection.username", "go");
prop.setProperty("hibernate.connection.password", "go");
prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
prop.setProperty("hibernate.hbm2ddl.auto", "create");
concreteSessionFactory = new org.hibernate.cfg.Configuration()
.addProperties(prop)
//.addPackage("com.kat")
.addAnnotatedClass(Vehicle.class)
.buildSessionFactory()
;
}
catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return concreteSessionFactory.openSession();
}
}
/*
import javax.persistence.*;
@Entity
@Table(name = "vehicle")
public class Vehicle {
@Id
@GeneratedValue
private int id;
@Column(name="plateno")
private String plate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPlate() {
return plate;
}
public void setPlate(String plate) {
this.plate = plate;
}
}
*/
/*
import org.hibernate.Session;
import java.util.List;
public class Grizzly {
public static void main(String[] args) {
Session sess = GrizzlyHelper.getSession();
sess.beginTransaction();
List<Vehicle> lstVeh = sess.createCriteria(Vehicle.class).list();
System.out.println(lstVeh.size());
sess.close();
}
}
*/
/*
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.5.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment