Hibernate Program for All Data Show

hibernate.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/batch137</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">batch137</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>

to create database Automatically replace this line name="hibernate.connection.url">jdbc:mysql://localhost:3306/batch137</property>
with below code.

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/batch111?createDatabaseIfNotExist=true</property>

Pojo Class



import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class AllDataShow {
@Id
public int id;
public String name;
public AllDataShow() {
super();
// TODO Auto-generated constructor stub
}
public AllDataShow(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "AllDataShow [id=" + id + ", name=" + name + "]";
}
}


TestAllDataShow.java


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

public class TestAllData {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(AllDataShow.class).configure();
SessionFactory sf = cfg.buildSessionFactory();
Session ss = sf.openSession();
// Transaction tx=ss.beginTransaction();

Query<AllDataShow> qry = ss.createQuery("From AllDataShow");

/// Iterable<AllDataShow> list=qry.list();
List<AllDataShow> list = qry.list();
for (AllDataShow allDataShow : list) {
System.out.println(allDataShow.getId() + " " + allDataShow.getName());
}

}

}





Comments

Popular posts from this blog

Primary/Main features of Java

HTTrack ऑफलाइन वेबसाइट डाउनलोडर क्या है ?

Which language is used for Java?