Created
January 8, 2021 13:59
-
-
Save iamabs2001/2f967d6dfd7e1a41fb2e6b82118d2d2c to your computer and use it in GitHub Desktop.
Hibernate Demo Simplest
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
package com.sigma.corejava.configs; | |
import Models.UserModel; | |
import java.util.Properties; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.cfg.Configuration; | |
import org.hibernate.cfg.Environment; | |
public class HibernateConfig { | |
private static SessionFactory factory; | |
public static SessionFactory getSessionFactory() { | |
if (factory == null) { | |
try { | |
// Properties | |
Properties properties = new Properties(); | |
properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect"); | |
properties.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver"); | |
properties.setProperty(Environment.USER, "root"); | |
properties.setProperty(Environment.PASS, "123"); | |
properties.setProperty(Environment.URL, "jdbc:mysql://localhost:3306/abhijeet"); | |
// Hibernate Configurations | |
Configuration hibernate = new Configuration(); | |
hibernate.setProperties(properties); | |
// Add Models | |
hibernate.addAnnotatedClass(UserModel.class); | |
// Build Session Dactory | |
factory = hibernate.buildSessionFactory(); | |
} catch (Exception e) { return null; } | |
} | |
return factory; | |
} | |
} |
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
package com.sigma.corejava; | |
import Models.UserModel; | |
import com.sigma.corejava.configs.HibernateConfig; | |
import java.util.List; | |
import org.hibernate.Session; | |
import org.hibernate.Transaction; | |
public class Programme { | |
public static void main(String args[]) { | |
Session session = HibernateConfig.getSessionFactory().openSession(); | |
Transaction t = session.beginTransaction(); | |
// UserModel thenewuser = new UserModel(1, "hero","123"); | |
// session.save(thenewuser); | |
// t.commit(); | |
List<UserModel> UsersList = session.createQuery("from UserModel").list(); | |
for (UserModel user : UsersList) { | |
System.out.println("-------------------"); | |
System.out.println(user.getId()); | |
System.out.println(user.getName()); | |
System.out.println(user.getPass()); | |
} | |
} | |
} |
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
package Models; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.Table; | |
import javax.persistence.Id; | |
@Entity | |
@Table(name = "user") | |
public class UserModel { | |
@Id | |
@Column | |
private int id; | |
@Column | |
private String name; | |
@Column | |
private String pass; | |
public UserModel() { } | |
public UserModel(int id, String name, String pass) { | |
this.id = id; | |
this.name = name; | |
this.pass = pass; | |
} | |
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; | |
} | |
public String getPass() { | |
return pass; | |
} | |
public void setPass(String pass) { | |
this.pass = pass; | |
} | |
@Override | |
public String toString() { | |
return "UserModel{" + "id=" + id + ", name=" + name + ", pass=" + pass + '}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment