-
-
Save pasza01/7bb523b0a3073aa26ae13f91332e2694 to your computer and use it in GitHub Desktop.
Hibernate without XML
This file contains hidden or 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
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