├── 11 Fetching.txt ├── 12 Embeddable.txt ├── 14 Mapping Relations Practical.txt ├── 15 Fetch EAGER LAZY.txt ├── 17 Caching Level 1.txt ├── 18 Caching Level 2.txt ├── 19 Caching Level 2 with Query.txt ├── 21 (HQL) Hibernate Query Language part 1.txt ├── 22 (HQL) Hibernate Query Language part 2.txt ├── 23 (HQL) Hibernate Query Language part 3.txt ├── 25 Persistence Life Cycle.txt ├── 26 Hibernate get vs load.txt ├── 4 Practical.txt ├── 6 Configuration File.txt ├── 7 Working.txt ├── 8 Show sql Property.txt └── 9 Annotation.txt /11 Fetching.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | App.java 3 | -------------------------------------------------------------------------------- 4 | 5 | package com.telusko.DemoHib; 6 | 7 | 8 | import org.hibernate.Session; 9 | import org.hibernate.Transaction; 10 | import org.hibernate.SessionFactory; 11 | import org.hibernate.cfg.Configuration; 12 | import org.hibernate.service.ServiceRegistry; 13 | import org.hibernate.service.ServiceRegistryBuilder; 14 | 15 | /** 16 | * Hello world! 17 | * 18 | */ 19 | public class App 20 | { 21 | public static void main( String[] args ) 22 | { 23 | Alien telusko = null; 24 | 25 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 26 | 27 | ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry(); 28 | 29 | SessionFactory sf = con.buildSessionFactory(); 30 | 31 | Session session = sf.openSession(); 32 | 33 | Transaction tx = session.beginTransaction(); 34 | 35 | telusko = (Alien) session.get(Alien.class, 102); 36 | 37 | tx.commit(); 38 | 39 | System.out.println(telusko); 40 | } 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- 45 | Alien.java 46 | -------------------------------------------------------------------------------- 47 | 48 | package com.telusko.DemoHib; 49 | 50 | import javax.persistence.Column; 51 | import javax.persistence.Entity; 52 | import javax.persistence.Id; 53 | import javax.persistence.Table; 54 | import javax.persistence.Transient; 55 | 56 | @Entity 57 | @Table(name="alien_table") 58 | public class Alien 59 | { 60 | @Id 61 | private int aid; 62 | private String aname; 63 | @Column(name="alien_color") 64 | private String color; 65 | 66 | public int getAid() { 67 | return aid; 68 | } 69 | public void setAid(int aid) { 70 | this.aid = aid; 71 | } 72 | public String getAname() { 73 | return aname; 74 | } 75 | public void setAname(String aname) { 76 | this.aname = aname; 77 | } 78 | public String getColor() { 79 | return color; 80 | } 81 | public void setColor(String color) { 82 | this.color = color; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "Alien [aid=" + aid + ", aname=" + aname + ", color=" + color + "]"; 88 | } 89 | 90 | 91 | 92 | 93 | } 94 | 95 | 96 | -------------------------------------------------------------------------------- 97 | hibernate.cfg.xml 98 | -------------------------------------------------------------------------------- 99 | 100 | 101 | 104 | 105 | 106 | com.mysql.jdbc.Driver 107 | 1234 108 | jdbc:mysql://localhost:3306/neon 109 | root 110 | org.hibernate.dialect.MySQLDialect 111 | update 112 | true 113 | 114 | -------------------------------------------------------------------------------- /12 Embeddable.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | App.java 3 | -------------------------------------------------------------------------------- 4 | 5 | package com.telusko.DemoHib; 6 | 7 | 8 | import org.hibernate.Session; 9 | import org.hibernate.Transaction; 10 | import org.hibernate.SessionFactory; 11 | import org.hibernate.cfg.Configuration; 12 | import org.hibernate.service.ServiceRegistry; 13 | import org.hibernate.service.ServiceRegistryBuilder; 14 | 15 | /** 16 | * Hello world! 17 | * 18 | */ 19 | public class App 20 | { 21 | public static void main( String[] args ) 22 | { 23 | AlienName an = new AlienName(); 24 | an.setFname("NavinKumarReady"); 25 | an.setLname("Thatipalli"); 26 | an.setMname("BapReddy"); 27 | 28 | Alien telusko = new Alien(); 29 | telusko.setAid(101); 30 | telusko.setColor("Green"); 31 | telusko.setAname(an); 32 | 33 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 34 | 35 | ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry(); 36 | 37 | SessionFactory sf = con.buildSessionFactory(); 38 | 39 | Session session = sf.openSession(); 40 | 41 | Transaction tx = session.beginTransaction(); 42 | 43 | session.save(telusko); 44 | 45 | tx.commit(); 46 | 47 | System.out.println(telusko); 48 | } 49 | } 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- 54 | Alien.java 55 | -------------------------------------------------------------------------------- 56 | 57 | package com.telusko.DemoHib; 58 | 59 | import javax.persistence.Column; 60 | import javax.persistence.Entity; 61 | import javax.persistence.Id; 62 | import javax.persistence.Table; 63 | import javax.persistence.Transient; 64 | 65 | @Entity 66 | @Table(name="alien_table") 67 | public class Alien 68 | { 69 | @Id 70 | private int aid; 71 | private AlienName aname; 72 | private String color; 73 | 74 | public int getAid() { 75 | return aid; 76 | } 77 | public void setAid(int aid) { 78 | this.aid = aid; 79 | } 80 | public AlienName getAname() { 81 | return aname; 82 | } 83 | public void setAname(AlienName aname) { 84 | this.aname = aname; 85 | } 86 | public String getColor() { 87 | return color; 88 | } 89 | public void setColor(String color) { 90 | this.color = color; 91 | } 92 | 93 | @Override 94 | public String toString() { 95 | return "Alien [aid=" + aid + ", aname=" + aname + ", color=" + color + "]"; 96 | } 97 | 98 | 99 | 100 | 101 | } 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- 106 | hibernate.cfg.xml 107 | -------------------------------------------------------------------------------- 108 | 109 | 110 | 113 | 114 | 115 | com.mysql.jdbc.Driver 116 | 1234 117 | jdbc:mysql://localhost:3306/neon 118 | root 119 | org.hibernate.dialect.MySQLDialect 120 | create 121 | true 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- 126 | AlienName.java 127 | -------------------------------------------------------------------------------- 128 | 129 | package com.telusko.DemoHib; 130 | 131 | import javax.persistence.Embeddable; 132 | 133 | @Embeddable 134 | public class AlienName 135 | { 136 | private String fname; 137 | private String lname; 138 | private String mname; 139 | 140 | 141 | public String getFname() { 142 | return fname; 143 | } 144 | public void setFname(String fname) { 145 | this.fname = fname; 146 | } 147 | public String getLname() { 148 | return lname; 149 | } 150 | public void setLname(String lname) { 151 | this.lname = lname; 152 | } 153 | public String getMname() { 154 | return mname; 155 | } 156 | public void setMname(String mname) { 157 | this.mname = mname; 158 | } 159 | 160 | @Override 161 | public String toString() { 162 | return "AlienName [fname=" + fname + ", lname=" + lname + ", mname=" + mname + "]"; 163 | } 164 | 165 | 166 | 167 | 168 | } 169 | -------------------------------------------------------------------------------- /14 Mapping Relations Practical.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | 9 | import org.hibernate.Session; 10 | import org.hibernate.Transaction; 11 | import org.hibernate.SessionFactory; 12 | import org.hibernate.cfg.Configuration; 13 | import org.hibernate.service.ServiceRegistry; 14 | import org.hibernate.service.ServiceRegistryBuilder; 15 | 16 | /** 17 | * Hello world! 18 | * 19 | */ 20 | public class App 21 | { 22 | public static void main( String[] args ) 23 | { 24 | Laptop laptop = new Laptop(); 25 | laptop.setLid(101); 26 | laptop.setLname("Dell"); 27 | 28 | Student s = new Student(); 29 | s.setName("Navin"); 30 | s.setRollno(1); 31 | s.setMarks(50); 32 | s.getLaptop().add(laptop); 33 | 34 | laptop.getStudent().add(s); 35 | 36 | Configuration config = new Configuration().configure().addAnnotatedClass(Student.class).addAnnotatedClass(Laptop.class); 37 | ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 38 | SessionFactory sf = config.buildSessionFactory(registry); 39 | Session session = sf.openSession(); 40 | 41 | session.beginTransaction(); 42 | 43 | session.save(laptop); 44 | session.save(s); 45 | 46 | session.getTransaction().commit(); 47 | } 48 | } 49 | 50 | 51 | -------------------------------------------------------------------------------- 52 | Laptop.java 53 | -------------------------------------------------------------------------------- 54 | 55 | package com.telusko.DemoHib; 56 | 57 | import java.util.ArrayList; 58 | import java.util.List; 59 | 60 | import javax.persistence.Entity; 61 | import javax.persistence.Id; 62 | import javax.persistence.ManyToMany; 63 | import javax.persistence.ManyToOne; 64 | 65 | @Entity 66 | public class Laptop 67 | { 68 | @Id 69 | private int lid; 70 | private String lname; 71 | @ManyToMany 72 | private List student = new ArrayList(); 73 | 74 | public int getLid() { 75 | return lid; 76 | } 77 | public void setLid(int lid) { 78 | this.lid = lid; 79 | } 80 | public String getLname() { 81 | return lname; 82 | } 83 | public void setLname(String lname) { 84 | this.lname = lname; 85 | } 86 | public List getStudent() { 87 | return student; 88 | } 89 | public void setStudent(List student) { 90 | this.student = student; 91 | } 92 | 93 | 94 | } 95 | 96 | 97 | -------------------------------------------------------------------------------- 98 | Student.java 99 | -------------------------------------------------------------------------------- 100 | 101 | package com.telusko.DemoHib; 102 | 103 | import java.util.ArrayList; 104 | import java.util.List; 105 | 106 | import javax.persistence.Entity; 107 | import javax.persistence.Id; 108 | import javax.persistence.ManyToMany; 109 | import javax.persistence.OneToMany; 110 | import javax.persistence.OneToOne; 111 | 112 | @Entity 113 | public class Student 114 | { 115 | @Id 116 | private int rollno; 117 | private String name; 118 | private int marks; 119 | @ManyToMany(mappedBy = "student") 120 | private List laptop = new ArrayList(); 121 | 122 | public int getRollno() { 123 | return rollno; 124 | } 125 | public void setRollno(int rollno) { 126 | this.rollno = rollno; 127 | } 128 | public String getName() { 129 | return name; 130 | } 131 | public void setName(String name) { 132 | this.name = name; 133 | } 134 | public int getMarks() { 135 | return marks; 136 | } 137 | public void setMarks(int marks) { 138 | this.marks = marks; 139 | } 140 | public List getLaptop() { 141 | return laptop; 142 | } 143 | public void setLaptop(List laptop) { 144 | this.laptop = laptop; 145 | } 146 | 147 | @Override 148 | public String toString() { 149 | return "Student [rollno=" + rollno + ", name=" + name + ", marks=" + marks + "]"; 150 | } 151 | 152 | 153 | } 154 | 155 | -------------------------------------------------------------------------------- 156 | hibernate.cfg.xml 157 | -------------------------------------------------------------------------------- 158 | 159 | 160 | 163 | 164 | 165 | com.mysql.jdbc.Driver 166 | 1234 167 | jdbc:mysql://localhost:3306/neon 168 | root 169 | org.hibernate.dialect.MySQLDialect 170 | create 171 | true 172 | 173 | 174 | -------------------------------------------------------------------------------- /15 Fetch EAGER LAZY.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | 9 | import java.util.Collection; 10 | 11 | import org.hibernate.Session; 12 | import org.hibernate.Transaction; 13 | import org.hibernate.SessionFactory; 14 | import org.hibernate.cfg.Configuration; 15 | import org.hibernate.service.ServiceRegistry; 16 | import org.hibernate.service.ServiceRegistryBuilder; 17 | 18 | public class App 19 | { 20 | public static void main( String[] args ) 21 | { 22 | 23 | Configuration config = new Configuration().configure().addAnnotatedClass(Laptop.class).addAnnotatedClass(Alien.class); 24 | SessionFactory sf = config.buildSessionFactory(); 25 | Session session = sf.openSession(); 26 | 27 | session.beginTransaction(); 28 | 29 | Alien a1 = session.get(Alien.class, 1); 30 | 31 | System.out.println(a1.getAname()); 32 | // Collection laps = a1.getLaps(); 33 | // 34 | // for(Laptop l : laps) 35 | // { 36 | // System.out.println(l); 37 | // } 38 | 39 | session.getTransaction().commit(); 40 | } 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- 45 | Laptop.java 46 | -------------------------------------------------------------------------------- 47 | 48 | package com.telusko.DemoHib; 49 | 50 | import java.util.ArrayList; 51 | import java.util.List; 52 | 53 | import javax.persistence.Entity; 54 | import javax.persistence.Id; 55 | import javax.persistence.ManyToMany; 56 | import javax.persistence.ManyToOne; 57 | 58 | @Entity 59 | public class Laptop 60 | { 61 | @Id 62 | private int lid; 63 | private String brand; 64 | private String price; 65 | @ManyToOne 66 | private Alien alien; 67 | 68 | public int getLid() { 69 | return lid; 70 | } 71 | public void setLid(int lid) { 72 | this.lid = lid; 73 | } 74 | public String getBrand() { 75 | return brand; 76 | } 77 | public void setBrand(String brand) { 78 | this.brand = brand; 79 | } 80 | public String getPrice() { 81 | return price; 82 | } 83 | public void setPrice(String price) { 84 | this.price = price; 85 | } 86 | public Alien getAlien() { 87 | return alien; 88 | } 89 | public void setAlien(Alien alien) { 90 | this.alien = alien; 91 | } 92 | 93 | 94 | 95 | 96 | } 97 | 98 | -------------------------------------------------------------------------------- 99 | Alien.java 100 | -------------------------------------------------------------------------------- 101 | 102 | package com.telusko.DemoHib; 103 | 104 | import java.util.ArrayList; 105 | import java.util.Collection; 106 | 107 | import javax.persistence.Column; 108 | import javax.persistence.Entity; 109 | import javax.persistence.FetchType; 110 | import javax.persistence.Id; 111 | import javax.persistence.OneToMany; 112 | import javax.persistence.Table; 113 | import javax.persistence.Transient; 114 | 115 | 116 | @Entity 117 | public class Alien 118 | { 119 | @Id 120 | private int aid; 121 | private String aname; 122 | 123 | @OneToMany(mappedBy = "alien",fetch=FetchType.EAGER) 124 | private Collection laps = new ArrayList(); 125 | 126 | 127 | public Collection getLaps() { 128 | return laps; 129 | } 130 | public void setLaps(Collection laps) { 131 | this.laps = laps; 132 | } 133 | public int getAid() { 134 | return aid; 135 | } 136 | public void setAid(int aid) { 137 | this.aid = aid; 138 | } 139 | public String getAname() { 140 | return aname; 141 | } 142 | public void setAname(String aname) { 143 | this.aname = aname; 144 | } 145 | 146 | @Override 147 | public String toString() { 148 | return "Alien [aid=" + aid + ", aname=" + aname + " ]"; 149 | } 150 | 151 | 152 | } 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- 157 | hibernate.cfg.xml 158 | -------------------------------------------------------------------------------- 159 | 160 | 161 | 164 | 165 | 166 | com.mysql.jdbc.Driver 167 | 1234 168 | jdbc:mysql://localhost:3306/test 169 | root 170 | org.hibernate.dialect.MySQLDialect 171 | create 172 | true 173 | 174 | 175 | -------------------------------------------------------------------------------- /17 Caching Level 1.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | 9 | import java.util.Collection; 10 | 11 | import org.hibernate.Session; 12 | import org.hibernate.Transaction; 13 | import org.hibernate.SessionFactory; 14 | import org.hibernate.cfg.Configuration; 15 | import org.hibernate.service.ServiceRegistry; 16 | import org.hibernate.service.ServiceRegistryBuilder; 17 | 18 | public class App 19 | { 20 | public static void main( String[] args ) 21 | { 22 | Alien a = null; 23 | 24 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 25 | ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry(); 26 | SessionFactory sf = con.buildSessionFactory(reg); 27 | Session session1 = sf.openSession(); 28 | session1.beginTransaction(); 29 | 30 | 31 | a = (Alien) session1.get(Alien.class, 101); 32 | System.out.println(a); 33 | 34 | 35 | session1.getTransaction().commit(); 36 | session1.close(); 37 | 38 | Session session2 = sf.openSession(); 39 | session2.beginTransaction(); 40 | 41 | a = (Alien) session2.get(Alien.class, 101); 42 | System.out.println(a); 43 | 44 | session2.getTransaction().commit(); 45 | session2.close(); 46 | 47 | 48 | 49 | } 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- 54 | Alien.java 55 | -------------------------------------------------------------------------------- 56 | 57 | package com.telusko.DemoHib; 58 | 59 | import java.util.ArrayList; 60 | import java.util.Collection; 61 | 62 | import javax.persistence.Column; 63 | import javax.persistence.Entity; 64 | import javax.persistence.FetchType; 65 | import javax.persistence.Id; 66 | import javax.persistence.OneToMany; 67 | import javax.persistence.Table; 68 | import javax.persistence.Transient; 69 | 70 | 71 | @Entity 72 | @Table(name="alien_table") 73 | public class Alien 74 | { 75 | @Id 76 | private int aid; 77 | private String aname; 78 | private String color; 79 | public int getAid() { 80 | return aid; 81 | } 82 | public void setAid(int aid) { 83 | this.aid = aid; 84 | } 85 | public String getAname() { 86 | return aname; 87 | } 88 | public void setAname(String aname) { 89 | this.aname = aname; 90 | } 91 | public String getColor() { 92 | return color; 93 | } 94 | public void setColor(String color) { 95 | this.color = color; 96 | } 97 | 98 | @Override 99 | public String toString() { 100 | return "Alien [aid=" + aid + ", aname=" + aname + ", color=" + color + "]"; 101 | } 102 | 103 | 104 | 105 | 106 | } 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- 111 | hibernate.cfg.xml 112 | -------------------------------------------------------------------------------- 113 | 114 | 115 | 118 | 119 | 120 | com.mysql.jdbc.Driver 121 | 1234 122 | jdbc:mysql://localhost:3306/neon 123 | root 124 | org.hibernate.dialect.MySQLDialect 125 | update 126 | true 127 | 128 | -------------------------------------------------------------------------------- /18 Caching Level 2.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | 9 | import java.util.Collection; 10 | 11 | import org.hibernate.Session; 12 | import org.hibernate.Transaction; 13 | import org.hibernate.SessionFactory; 14 | import org.hibernate.cfg.Configuration; 15 | import org.hibernate.service.ServiceRegistry; 16 | import org.hibernate.service.ServiceRegistryBuilder; 17 | 18 | public class App 19 | { 20 | public static void main( String[] args ) 21 | { 22 | Alien a = null; 23 | 24 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 25 | ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry(); 26 | SessionFactory sf = con.buildSessionFactory(reg); 27 | Session session1 = sf.openSession(); 28 | session1.beginTransaction(); 29 | 30 | 31 | a = (Alien) session1.get(Alien.class, 101); 32 | System.out.println(a); 33 | 34 | 35 | session1.getTransaction().commit(); 36 | session1.close(); 37 | 38 | Session session2 = sf.openSession(); 39 | session2.beginTransaction(); 40 | 41 | a = (Alien) session2.get(Alien.class, 101); 42 | System.out.println(a); 43 | 44 | session2.getTransaction().commit(); 45 | session2.close(); 46 | 47 | 48 | 49 | } 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- 54 | Alien.java 55 | -------------------------------------------------------------------------------- 56 | 57 | package com.telusko.DemoHib; 58 | 59 | import java.util.ArrayList; 60 | import java.util.Collection; 61 | 62 | import javax.persistence.Cacheable; 63 | import javax.persistence.Column; 64 | import javax.persistence.Entity; 65 | import javax.persistence.FetchType; 66 | import javax.persistence.Id; 67 | import javax.persistence.OneToMany; 68 | import javax.persistence.Table; 69 | import javax.persistence.Transient; 70 | 71 | import org.hibernate.annotations.Cache; 72 | import org.hibernate.annotations.CacheConcurrencyStrategy; 73 | 74 | 75 | @Entity 76 | @Table(name="alien_table") 77 | @Cacheable 78 | @Cache(usage=CacheConcurrencyStrategy.READ_ONLY) 79 | public class Alien 80 | { 81 | @Id 82 | private int aid; 83 | private String aname; 84 | private String color; 85 | public int getAid() { 86 | return aid; 87 | } 88 | public void setAid(int aid) { 89 | this.aid = aid; 90 | } 91 | public String getAname() { 92 | return aname; 93 | } 94 | public void setAname(String aname) { 95 | this.aname = aname; 96 | } 97 | public String getColor() { 98 | return color; 99 | } 100 | public void setColor(String color) { 101 | this.color = color; 102 | } 103 | 104 | @Override 105 | public String toString() { 106 | return "Alien [aid=" + aid + ", aname=" + aname + ", color=" + color + "]"; 107 | } 108 | 109 | 110 | 111 | 112 | } 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- 118 | hibernate.cfg.xml 119 | -------------------------------------------------------------------------------- 120 | 121 | 122 | 125 | 126 | 127 | com.mysql.jdbc.Driver 128 | 1234 129 | jdbc:mysql://localhost:3306/neon 130 | root 131 | org.hibernate.dialect.MySQLDialect 132 | update 133 | true 134 | 135 | true 136 | 137 | org.hibernate.cache.ehcache.EhCacheRegionFactory 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- 143 | pom.xml 144 | -------------------------------------------------------------------------------- 145 | 146 | 148 | 4.0.0 149 | 150 | com.telusko 151 | DemoHib 152 | 0.0.1-SNAPSHOT 153 | jar 154 | 155 | DemoHib 156 | http://maven.apache.org 157 | 158 | 159 | UTF-8 160 | 161 | 162 | 163 | 164 | junit 165 | junit 166 | 3.8.1 167 | test 168 | 169 | 170 | 171 | 172 | org.hibernate 173 | hibernate-core 174 | 4.1.6.Final 175 | 176 | 177 | 178 | net.sf.ehcache 179 | ehcache 180 | 2.10.3 181 | 182 | 183 | 184 | org.hibernate 185 | hibernate-ehcache 186 | 4.1.6.Final 187 | 188 | 189 | 190 | 191 | mysql 192 | mysql-connector-java 193 | 5.1.47 194 | 195 | 196 | 197 | 198 | -------------------------------------------------------------------------------- /19 Caching Level 2 with Query.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | 9 | import java.util.Collection; 10 | 11 | import org.hibernate.Query; 12 | import org.hibernate.Session; 13 | import org.hibernate.Transaction; 14 | import org.hibernate.SessionFactory; 15 | import org.hibernate.cfg.Configuration; 16 | import org.hibernate.service.ServiceRegistry; 17 | import org.hibernate.service.ServiceRegistryBuilder; 18 | 19 | public class App 20 | { 21 | public static void main( String[] args ) 22 | { 23 | Alien a = null; 24 | 25 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 26 | ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry(); 27 | SessionFactory sf = con.buildSessionFactory(reg); 28 | Session session1 = sf.openSession(); 29 | session1.beginTransaction(); 30 | 31 | Query q1 = session1.createQuery("from Alien where aid=101"); 32 | q1.setCacheable(true); 33 | a = (Alien) q1.uniqueResult(); 34 | System.out.println(a); 35 | 36 | 37 | session1.getTransaction().commit(); 38 | session1.close(); 39 | 40 | Session session2 = sf.openSession(); 41 | session2.beginTransaction(); 42 | 43 | Query q2 = session2.createQuery("from Alien where aid=101"); 44 | q2.setCacheable(true); 45 | a = (Alien) q2.uniqueResult(); 46 | System.out.println(a); 47 | 48 | session2.getTransaction().commit(); 49 | session2.close(); 50 | 51 | 52 | 53 | } 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- 58 | Alien.java 59 | -------------------------------------------------------------------------------- 60 | 61 | package com.telusko.DemoHib; 62 | 63 | import java.util.ArrayList; 64 | import java.util.Collection; 65 | 66 | import javax.persistence.Cacheable; 67 | import javax.persistence.Column; 68 | import javax.persistence.Entity; 69 | import javax.persistence.FetchType; 70 | import javax.persistence.Id; 71 | import javax.persistence.OneToMany; 72 | import javax.persistence.Table; 73 | import javax.persistence.Transient; 74 | 75 | import org.hibernate.annotations.Cache; 76 | import org.hibernate.annotations.CacheConcurrencyStrategy; 77 | 78 | 79 | @Entity 80 | @Table(name="alien_table") 81 | @Cacheable 82 | @Cache(usage=CacheConcurrencyStrategy.READ_ONLY) 83 | public class Alien 84 | { 85 | @Id 86 | private int aid; 87 | private String aname; 88 | private String color; 89 | public int getAid() { 90 | return aid; 91 | } 92 | public void setAid(int aid) { 93 | this.aid = aid; 94 | } 95 | public String getAname() { 96 | return aname; 97 | } 98 | public void setAname(String aname) { 99 | this.aname = aname; 100 | } 101 | public String getColor() { 102 | return color; 103 | } 104 | public void setColor(String color) { 105 | this.color = color; 106 | } 107 | 108 | @Override 109 | public String toString() { 110 | return "Alien [aid=" + aid + ", aname=" + aname + ", color=" + color + "]"; 111 | } 112 | 113 | 114 | 115 | 116 | } 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- 123 | hibernate.cfg.xml 124 | -------------------------------------------------------------------------------- 125 | 126 | 127 | 130 | 131 | 132 | com.mysql.jdbc.Driver 133 | 1234 134 | jdbc:mysql://localhost:3306/neon 135 | root 136 | org.hibernate.dialect.MySQLDialect 137 | update 138 | true 139 | true 140 | org.hibernate.cache.ehcache.EhCacheRegionFactory 141 | true 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- 146 | pom.xml 147 | -------------------------------------------------------------------------------- 148 | 149 | 151 | 4.0.0 152 | 153 | com.telusko 154 | DemoHib 155 | 0.0.1-SNAPSHOT 156 | jar 157 | 158 | DemoHib 159 | http://maven.apache.org 160 | 161 | 162 | UTF-8 163 | 164 | 165 | 166 | 167 | junit 168 | junit 169 | 3.8.1 170 | test 171 | 172 | 173 | 174 | 175 | org.hibernate 176 | hibernate-core 177 | 4.1.6.Final 178 | 179 | 180 | 181 | net.sf.ehcache 182 | ehcache 183 | 2.10.3 184 | 185 | 186 | 187 | org.hibernate 188 | hibernate-ehcache 189 | 4.1.6.Final 190 | 191 | 192 | 193 | 194 | mysql 195 | mysql-connector-java 196 | 5.1.47 197 | 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /21 (HQL) Hibernate Query Language part 1.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | import java.util.List; 9 | import java.util.Random; 10 | 11 | import org.hibernate.Query; 12 | import org.hibernate.Session; 13 | import org.hibernate.SessionFactory; 14 | import org.hibernate.cfg.Configuration; 15 | import org.hibernate.service.ServiceRegistry; 16 | import org.hibernate.service.ServiceRegistryBuilder; 17 | 18 | public class App 19 | { 20 | public static void main( String[] args ) 21 | { 22 | Configuration config = new Configuration().configure().addAnnotatedClass(Student.class); 23 | ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 24 | SessionFactory sf = config.buildSessionFactory(registry); 25 | Session session = sf.openSession(); 26 | 27 | session.beginTransaction(); 28 | 29 | // Query q = session.createQuery("from Student"); //(Fetch all student data) 30 | // Query q = session.createQuery("from Student where marks > 50"); //(it show only that record where marks will be greater than 50) 31 | 32 | // List students = q.list(); 33 | 34 | Query q = session.createQuery("from Student where rollno=7"); 35 | Student student = (Student) q.uniqueResult(); 36 | 37 | System.out.println(student); 38 | 39 | 40 | // for(Student s : students) 41 | // { 42 | // System.out.println(s); 43 | // } 44 | // 45 | // Random r = new Random(); 46 | // 47 | // 48 | // for(int i=1;i<=50;i++) 49 | // { 50 | // Student s = new Student(); 51 | // s.setRollno(i); 52 | // s.setName("Name" + i); 53 | // s.setMarks(r.nextInt(100)); 54 | // session.save(s); 55 | // } 56 | 57 | session.getTransaction().commit(); 58 | 59 | } 60 | 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- 65 | Student.java 66 | -------------------------------------------------------------------------------- 67 | 68 | package com.telusko.DemoHib; 69 | 70 | 71 | import javax.persistence.Entity; 72 | import javax.persistence.Id; 73 | 74 | 75 | @Entity 76 | public class Student 77 | { 78 | @Id 79 | private int rollno; 80 | private String name; 81 | private int marks; 82 | 83 | 84 | public int getRollno() { 85 | return rollno; 86 | } 87 | public void setRollno(int rollno) { 88 | this.rollno = rollno; 89 | } 90 | public String getName() { 91 | return name; 92 | } 93 | public void setName(String name) { 94 | this.name = name; 95 | } 96 | public int getMarks() { 97 | return marks; 98 | } 99 | public void setMarks(int marks) { 100 | this.marks = marks; 101 | } 102 | 103 | 104 | @Override 105 | public String toString() { 106 | return "Student [rollno=" + rollno + ", name=" + name + ", marks=" + marks + "]"; 107 | } 108 | 109 | 110 | } 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- 116 | hibernate.cfg.xml 117 | -------------------------------------------------------------------------------- 118 | 119 | 120 | 123 | 124 | 125 | org.postgresql.Driver 126 | jdbc:postgresql://localhost:5432/postgres 127 | postgres 128 | navin123 129 | org.hibernate.dialect.PostgreSQLDialect 130 | update 131 | true 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- 136 | pom.xml 137 | -------------------------------------------------------------------------------- 138 | 139 | 141 | 4.0.0 142 | 143 | com.telusko 144 | DemoHib 145 | 0.0.1-SNAPSHOT 146 | jar 147 | 148 | DemoHib 149 | http://maven.apache.org 150 | 151 | 152 | UTF-8 153 | 154 | 155 | 156 | 157 | junit 158 | junit 159 | 3.8.1 160 | test 161 | 162 | 163 | 164 | 165 | org.hibernate 166 | hibernate-core 167 | 4.1.6.Final 168 | 169 | 170 | 171 | net.sf.ehcache 172 | ehcache 173 | 2.10.3 174 | 175 | 176 | 177 | org.hibernate 178 | hibernate-ehcache 179 | 4.1.6.Final 180 | 181 | 182 | 183 | 184 | mysql 185 | mysql-connector-java 186 | 5.1.47 187 | 188 | 189 | 190 | 191 | org.postgresql 192 | postgresql 193 | 42.2.6 194 | 195 | 196 | 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /22 (HQL) Hibernate Query Language part 2.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | import java.util.List; 9 | import java.util.Random; 10 | 11 | import org.hibernate.Query; 12 | import org.hibernate.Session; 13 | import org.hibernate.SessionFactory; 14 | import org.hibernate.cfg.Configuration; 15 | import org.hibernate.service.ServiceRegistry; 16 | import org.hibernate.service.ServiceRegistryBuilder; 17 | 18 | public class App 19 | { 20 | public static void main( String[] args ) 21 | { 22 | Configuration config = new Configuration().configure().addAnnotatedClass(Student.class); 23 | ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 24 | SessionFactory sf = config.buildSessionFactory(registry); 25 | Session session = sf.openSession(); 26 | int b = 60; 27 | 28 | session.beginTransaction(); 29 | 30 | // Query q = session.createQuery("select rollno,name,marks from Student where rollno=7"); 31 | // Query q = session.createQuery("select rollno,name,marks from Student"); 32 | Query q = session.createQuery("select sum(marks) from Student s where s.marks> :b"); 33 | q.setParameter("b", b); 34 | Long marks = (Long) q.uniqueResult(); 35 | 36 | // for(Object o : student) 37 | // { 38 | // System.out.println(o); 39 | // } 40 | 41 | 42 | System.out.println(marks); 43 | 44 | session.getTransaction().commit(); 45 | 46 | 47 | } 48 | 49 | } 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- 54 | Student.java 55 | -------------------------------------------------------------------------------- 56 | 57 | package com.telusko.DemoHib; 58 | 59 | 60 | import javax.persistence.Entity; 61 | import javax.persistence.Id; 62 | 63 | 64 | @Entity 65 | public class Student 66 | { 67 | @Id 68 | private int rollno; 69 | private String name; 70 | private int marks; 71 | 72 | 73 | public int getRollno() { 74 | return rollno; 75 | } 76 | public void setRollno(int rollno) { 77 | this.rollno = rollno; 78 | } 79 | public String getName() { 80 | return name; 81 | } 82 | public void setName(String name) { 83 | this.name = name; 84 | } 85 | public int getMarks() { 86 | return marks; 87 | } 88 | public void setMarks(int marks) { 89 | this.marks = marks; 90 | } 91 | 92 | 93 | @Override 94 | public String toString() { 95 | return "Student [rollno=" + rollno + ", name=" + name + ", marks=" + marks + "]"; 96 | } 97 | 98 | 99 | } 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- 105 | hibernate.cfg.xml 106 | -------------------------------------------------------------------------------- 107 | 108 | 109 | 112 | 113 | 114 | org.postgresql.Driver 115 | jdbc:postgresql://localhost:5432/postgres 116 | postgres 117 | navin123 118 | org.hibernate.dialect.PostgreSQLDialect 119 | update 120 | true 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- 125 | pom.xml 126 | -------------------------------------------------------------------------------- 127 | 128 | 130 | 4.0.0 131 | 132 | com.telusko 133 | DemoHib 134 | 0.0.1-SNAPSHOT 135 | jar 136 | 137 | DemoHib 138 | http://maven.apache.org 139 | 140 | 141 | UTF-8 142 | 143 | 144 | 145 | 146 | junit 147 | junit 148 | 3.8.1 149 | test 150 | 151 | 152 | 153 | 154 | org.hibernate 155 | hibernate-core 156 | 4.1.6.Final 157 | 158 | 159 | 160 | net.sf.ehcache 161 | ehcache 162 | 2.10.3 163 | 164 | 165 | 166 | org.hibernate 167 | hibernate-ehcache 168 | 4.1.6.Final 169 | 170 | 171 | 172 | 173 | mysql 174 | mysql-connector-java 175 | 5.1.47 176 | 177 | 178 | 179 | 180 | org.postgresql 181 | postgresql 182 | 42.2.6 183 | 184 | 185 | 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /23 (HQL) Hibernate Query Language part 3.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Random; 11 | 12 | import org.hibernate.Criteria; 13 | import org.hibernate.Query; 14 | import org.hibernate.SQLQuery; 15 | import org.hibernate.Session; 16 | import org.hibernate.SessionFactory; 17 | import org.hibernate.cfg.Configuration; 18 | 19 | import org.hibernate.service.ServiceRegistry; 20 | import org.hibernate.service.ServiceRegistryBuilder; 21 | 22 | public class App 23 | { 24 | public static void main( String[] args ) 25 | { 26 | Configuration config = new Configuration().configure().addAnnotatedClass(Student.class); 27 | ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 28 | SessionFactory sf = config.buildSessionFactory(registry); 29 | Session session = sf.openSession(); 30 | session.beginTransaction(); 31 | 32 | //Native Queries.. 33 | 34 | // SQLQuery query = session.createSQLQuery("select * from student where marks>60"); 35 | SQLQuery query = session.createSQLQuery("select name,marks from student where marks>60"); 36 | 37 | // query.addEntity(Student.class); 38 | 39 | 40 | // List students = query.list(); 41 | 42 | query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); 43 | 44 | List students = query.list(); 45 | 46 | for(Object o : students) 47 | { 48 | Map m = (Map)o; 49 | System.out.println(m.get("name") + " : " + m.get("marks")); 50 | } 51 | 52 | session.getTransaction().commit(); 53 | 54 | 55 | } 56 | 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- 61 | Student.java 62 | -------------------------------------------------------------------------------- 63 | 64 | package com.telusko.DemoHib; 65 | 66 | 67 | import javax.persistence.Entity; 68 | import javax.persistence.Id; 69 | 70 | 71 | @Entity 72 | public class Student 73 | { 74 | @Id 75 | private int rollno; 76 | private String name; 77 | private int marks; 78 | 79 | 80 | public int getRollno() { 81 | return rollno; 82 | } 83 | public void setRollno(int rollno) { 84 | this.rollno = rollno; 85 | } 86 | public String getName() { 87 | return name; 88 | } 89 | public void setName(String name) { 90 | this.name = name; 91 | } 92 | public int getMarks() { 93 | return marks; 94 | } 95 | public void setMarks(int marks) { 96 | this.marks = marks; 97 | } 98 | 99 | 100 | @Override 101 | public String toString() { 102 | return "Student [rollno=" + rollno + ", name=" + name + ", marks=" + marks + "]"; 103 | } 104 | 105 | 106 | } 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- 112 | hibernate.cfg.xml 113 | -------------------------------------------------------------------------------- 114 | 115 | 116 | 119 | 120 | 121 | org.postgresql.Driver 122 | jdbc:postgresql://localhost:5432/postgres 123 | postgres 124 | navin123 125 | org.hibernate.dialect.PostgreSQLDialect 126 | update 127 | true 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- 132 | pom.xml 133 | -------------------------------------------------------------------------------- 134 | 135 | 137 | 4.0.0 138 | 139 | com.telusko 140 | DemoHib 141 | 0.0.1-SNAPSHOT 142 | jar 143 | 144 | DemoHib 145 | http://maven.apache.org 146 | 147 | 148 | UTF-8 149 | 150 | 151 | 152 | 153 | junit 154 | junit 155 | 3.8.1 156 | test 157 | 158 | 159 | 160 | 161 | org.hibernate 162 | hibernate-core 163 | 4.1.6.Final 164 | 165 | 166 | 167 | net.sf.ehcache 168 | ehcache 169 | 2.10.3 170 | 171 | 172 | 173 | org.hibernate 174 | hibernate-ehcache 175 | 4.1.6.Final 176 | 177 | 178 | 179 | 180 | mysql 181 | mysql-connector-java 182 | 5.1.47 183 | 184 | 185 | 186 | 187 | org.postgresql 188 | postgresql 189 | 42.2.6 190 | 191 | 192 | 193 | 194 | 195 | 196 | -------------------------------------------------------------------------------- /25 Persistence Life Cycle.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Random; 11 | 12 | import org.hibernate.Criteria; 13 | import org.hibernate.Query; 14 | import org.hibernate.SQLQuery; 15 | import org.hibernate.Session; 16 | import org.hibernate.SessionFactory; 17 | import org.hibernate.cfg.Configuration; 18 | 19 | import org.hibernate.service.ServiceRegistry; 20 | import org.hibernate.service.ServiceRegistryBuilder; 21 | 22 | public class App 23 | { 24 | public static void main( String[] args ) 25 | { 26 | Configuration config = new Configuration().configure().addAnnotatedClass(Laptop.class); 27 | SessionFactory sf = config.buildSessionFactory(); 28 | Session session = sf.openSession(); 29 | 30 | session.beginTransaction(); 31 | 32 | Laptop l = new Laptop(); 33 | l.setLid(52); 34 | l.setBrand("Lenovo"); 35 | l.setPrice(800); 36 | 37 | session.save(l); 38 | l.setPrice(750); 39 | 40 | // 41 | 42 | // session.remove(l); 43 | 44 | session.getTransaction().commit(); 45 | // session.detach(l); 46 | l.setPrice(600); 47 | 48 | 49 | } 50 | 51 | } 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- 56 | Laptop.java 57 | -------------------------------------------------------------------------------- 58 | 59 | package com.telusko.DemoHib; 60 | 61 | import java.util.ArrayList; 62 | import java.util.List; 63 | 64 | import javax.persistence.Entity; 65 | import javax.persistence.Id; 66 | import javax.persistence.ManyToMany; 67 | import javax.persistence.ManyToOne; 68 | 69 | @Entity 70 | public class Laptop 71 | { 72 | @Id 73 | private int lid; 74 | private String brand; 75 | private int price; 76 | public int getLid() { 77 | return lid; 78 | } 79 | public void setLid(int lid) { 80 | this.lid = lid; 81 | } 82 | public String getBrand() { 83 | return brand; 84 | } 85 | public void setBrand(String brand) { 86 | this.brand = brand; 87 | } 88 | public int getPrice() { 89 | return price; 90 | } 91 | public void setPrice(int price) { 92 | this.price = price; 93 | } 94 | 95 | @Override 96 | public String toString() { 97 | return "Laptop [lid=" + lid + ", brand=" + brand + ", price=" + price + "]"; 98 | } 99 | 100 | 101 | 102 | } 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- 108 | hibernate.cfg.xml 109 | -------------------------------------------------------------------------------- 110 | 111 | 112 | 115 | 116 | 117 | com.mysql.jdbc.Driver 118 | 1234 119 | jdbc:mysql://localhost:3306/alpha 120 | root 121 | org.hibernate.dialect.MySQLDialect 122 | update 123 | true 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- 128 | pom.xml 129 | -------------------------------------------------------------------------------- 130 | 131 | 133 | 4.0.0 134 | 135 | com.telusko 136 | DemoHib 137 | 0.0.1-SNAPSHOT 138 | jar 139 | 140 | DemoHib 141 | http://maven.apache.org 142 | 143 | 144 | UTF-8 145 | 146 | 147 | 148 | 149 | junit 150 | junit 151 | 3.8.1 152 | test 153 | 154 | 155 | 156 | 157 | org.hibernate 158 | hibernate-core 159 | 4.1.6.Final 160 | 161 | 162 | 163 | net.sf.ehcache 164 | ehcache 165 | 2.10.3 166 | 167 | 168 | 169 | org.hibernate 170 | hibernate-ehcache 171 | 4.1.6.Final 172 | 173 | 174 | 175 | 176 | mysql 177 | mysql-connector-java 178 | 5.1.47 179 | 180 | 181 | 182 | 183 | org.postgresql 184 | postgresql 185 | 42.2.6 186 | 187 | 188 | 189 | 190 | 191 | 192 | -------------------------------------------------------------------------------- /26 Hibernate get vs load.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- 3 | App.java 4 | -------------------------------------------------------------------------------- 5 | 6 | package com.telusko.DemoHib; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Random; 11 | 12 | import org.hibernate.Criteria; 13 | import org.hibernate.Query; 14 | import org.hibernate.SQLQuery; 15 | import org.hibernate.Session; 16 | import org.hibernate.SessionFactory; 17 | import org.hibernate.cfg.Configuration; 18 | 19 | import org.hibernate.service.ServiceRegistry; 20 | import org.hibernate.service.ServiceRegistryBuilder; 21 | 22 | public class App 23 | { 24 | public static void main( String[] args ) 25 | { 26 | Configuration config = new Configuration().configure().addAnnotatedClass(Laptop.class); 27 | SessionFactory sf = config.buildSessionFactory(); 28 | Session session = sf.openSession(); 29 | 30 | session.beginTransaction(); 31 | 32 | // Laptop lap = (Laptop) session.get(Laptop.class, 201); 33 | Laptop lap = (Laptop) session.load(Laptop.class, 201); //Proxy 34 | 35 | 36 | // try { 37 | // Thread.sleep(3000); 38 | // } catch (InterruptedException e) { 39 | // // TODO Auto-generated catch block 40 | // e.printStackTrace(); 41 | // } 42 | 43 | System.out.println(lap); 44 | 45 | session.getTransaction().commit(); 46 | 47 | 48 | } 49 | 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- 54 | Laptop.java 55 | -------------------------------------------------------------------------------- 56 | 57 | package com.telusko.DemoHib; 58 | 59 | import java.util.ArrayList; 60 | import java.util.List; 61 | 62 | import javax.persistence.Entity; 63 | import javax.persistence.Id; 64 | import javax.persistence.ManyToMany; 65 | import javax.persistence.ManyToOne; 66 | 67 | @Entity 68 | public class Laptop 69 | { 70 | @Id 71 | private int lid; 72 | private String brand; 73 | private int price; 74 | 75 | public int getLid() { 76 | return lid; 77 | } 78 | public void setLid(int lid) { 79 | this.lid = lid; 80 | } 81 | public String getBrand() { 82 | return brand; 83 | } 84 | public void setBrand(String brand) { 85 | this.brand = brand; 86 | } 87 | public int getPrice() { 88 | return price; 89 | } 90 | public void setPrice(int price) { 91 | this.price = price; 92 | } 93 | 94 | @Override 95 | public String toString() { 96 | return "Laptop [lid=" + lid + ", brand=" + brand + ", price=" + price + "]"; 97 | } 98 | 99 | 100 | 101 | } 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- 106 | hibernate.cfg.xml 107 | -------------------------------------------------------------------------------- 108 | 109 | 110 | 113 | 114 | 115 | com.mysql.jdbc.Driver 116 | 1234 117 | jdbc:mysql://localhost:3306/alpha 118 | root 119 | org.hibernate.dialect.MySQLDialect 120 | update 121 | true 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- 126 | pom.xml 127 | -------------------------------------------------------------------------------- 128 | 129 | 131 | 4.0.0 132 | 133 | com.telusko 134 | DemoHib 135 | 0.0.1-SNAPSHOT 136 | jar 137 | 138 | DemoHib 139 | http://maven.apache.org 140 | 141 | 142 | UTF-8 143 | 144 | 145 | 146 | 147 | junit 148 | junit 149 | 3.8.1 150 | test 151 | 152 | 153 | 154 | 155 | org.hibernate 156 | hibernate-core 157 | 4.1.6.Final 158 | 159 | 160 | 161 | net.sf.ehcache 162 | ehcache 163 | 2.10.3 164 | 165 | 166 | 167 | org.hibernate 168 | hibernate-ehcache 169 | 4.1.6.Final 170 | 171 | 172 | 173 | 174 | mysql 175 | mysql-connector-java 176 | 5.1.47 177 | 178 | 179 | 180 | 181 | org.postgresql 182 | postgresql 183 | 42.2.6 184 | 185 | 186 | 187 | 188 | 189 | 190 | -------------------------------------------------------------------------------- /4 Practical.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | App.java 3 | -------------------------------------------------------------------------------- 4 | 5 | package com.telusko.DemoHib; 6 | 7 | import org.hibernate.Session; 8 | import org.hibernate.SessionFactory; 9 | import org.hibernate.cfg.Configuration; 10 | 11 | /** 12 | * Hello world! 13 | * 14 | */ 15 | public class App 16 | { 17 | public static void main( String[] args ) 18 | { 19 | Alien telusko = new Alien(); 20 | telusko.setAid(101); 21 | telusko.setAname("navin"); 22 | telusko.setColor("Green"); 23 | 24 | Configuration con = new Configuration(); 25 | 26 | SessionFactory sf = con.buildSessionFactory(); 27 | 28 | Session session = sf.openSession(); 29 | 30 | session.save(telusko); 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- 35 | Alien.java 36 | -------------------------------------------------------------------------------- 37 | 38 | package com.telusko.DemoHib; 39 | 40 | public class Alien 41 | { 42 | private int aid; 43 | private String aname; 44 | private String color; 45 | 46 | public int getAid() { 47 | return aid; 48 | } 49 | public void setAid(int aid) { 50 | this.aid = aid; 51 | } 52 | public String getAname() { 53 | return aname; 54 | } 55 | public void setAname(String aname) { 56 | this.aname = aname; 57 | } 58 | public String getColor() { 59 | return color; 60 | } 61 | public void setColor(String color) { 62 | this.color = color; 63 | } 64 | 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- 69 | DemoHib/pom.xml 70 | -------------------------------------------------------------------------------- 71 | 72 | 74 | 4.0.0 75 | 76 | com.telusko 77 | DemoHib 78 | 0.0.1-SNAPSHOT 79 | jar 80 | 81 | DemoHib 82 | http://maven.apache.org 83 | 84 | 85 | UTF-8 86 | 87 | 88 | 89 | 90 | junit 91 | junit 92 | 3.8.1 93 | test 94 | 95 | 96 | 97 | 98 | org.hibernate 99 | hibernate-core 100 | 4.1.6.Final 101 | 102 | 103 | 104 | 105 | mysql 106 | mysql-connector-java 107 | 5.1.38 108 | 109 | 110 | 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /6 Configuration File.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | App.java 3 | -------------------------------------------------------------------------------- 4 | 5 | package com.telusko.DemoHib; 6 | 7 | 8 | import org.hibernate.Session; 9 | import org.hibernate.SessionFactory; 10 | import org.hibernate.Transaction; 11 | import org.hibernate.cfg.Configuration; 12 | 13 | /** 14 | * Hello world! 15 | * 16 | */ 17 | public class App 18 | { 19 | public static void main( String[] args ) 20 | { 21 | Alien telusko = new Alien(); 22 | telusko.setAid(101); 23 | telusko.setAname("navin"); 24 | telusko.setColor("Green"); 25 | 26 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 27 | 28 | SessionFactory sf = con.buildSessionFactory(); 29 | 30 | Session session = sf.openSession(); 31 | 32 | Transaction tx = session.beginTransaction(); 33 | 34 | session.save(telusko); 35 | 36 | tx.commit(); 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- 41 | Alien.java 42 | -------------------------------------------------------------------------------- 43 | 44 | package com.telusko.DemoHib; 45 | 46 | import javax.persistence.Entity; 47 | import javax.persistence.Id; 48 | 49 | @Entity 50 | public class Alien 51 | { 52 | @Id 53 | private int aid; 54 | private String aname; 55 | private String color; 56 | 57 | public int getAid() { 58 | return aid; 59 | } 60 | public void setAid(int aid) { 61 | this.aid = aid; 62 | } 63 | public String getAname() { 64 | return aname; 65 | } 66 | public void setAname(String aname) { 67 | this.aname = aname; 68 | } 69 | public String getColor() { 70 | return color; 71 | } 72 | public void setColor(String color) { 73 | this.color = color; 74 | } 75 | 76 | 77 | } 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- 82 | DemoHib/pom.xml 83 | -------------------------------------------------------------------------------- 84 | 85 | 87 | 4.0.0 88 | 89 | com.telusko 90 | DemoHib 91 | 0.0.1-SNAPSHOT 92 | jar 93 | 94 | DemoHib 95 | http://maven.apache.org 96 | 97 | 98 | UTF-8 99 | 100 | 101 | 102 | 103 | junit 104 | junit 105 | 3.8.1 106 | test 107 | 108 | 109 | 110 | 111 | org.hibernate 112 | hibernate-core 113 | 4.1.6.Final 114 | 115 | 116 | 117 | 118 | mysql 119 | mysql-connector-java 120 | 5.1.38 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- 127 | hibernate.cfg.xml 128 | -------------------------------------------------------------------------------- 129 | 130 | 131 | 134 | 135 | 136 | com.mysql.jdbc.Driver 137 | 1234 138 | jdbc:mysql://localhost:3307/neon 139 | root 140 | org.hibernate.dialect.MySQLDialect 141 | 142 | 143 | -------------------------------------------------------------------------------- /7 Working.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | App.java 3 | -------------------------------------------------------------------------------- 4 | 5 | package com.telusko.DemoHib; 6 | 7 | 8 | import org.hibernate.Session; 9 | import org.hibernate.SessionFactory; 10 | import org.hibernate.Transaction; 11 | import org.hibernate.cfg.Configuration; 12 | import org.hibernate.service.ServiceRegistry; 13 | import org.hibernate.service.ServiceRegistryBuilder; 14 | 15 | /** 16 | * Hello world! 17 | * 18 | */ 19 | public class App 20 | { 21 | public static void main( String[] args ) 22 | { 23 | Alien telusko = new Alien(); 24 | telusko.setAid(102); 25 | telusko.setAname("mridula"); 26 | telusko.setColor("Blue"); 27 | 28 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 29 | 30 | ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry(); 31 | 32 | SessionFactory sf = con.buildSessionFactory(); 33 | 34 | Session session = sf.openSession(); 35 | 36 | Transaction tx = session.beginTransaction(); 37 | 38 | session.save(telusko); 39 | 40 | tx.commit(); 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- 45 | Alien.java 46 | -------------------------------------------------------------------------------- 47 | 48 | package com.telusko.DemoHib; 49 | 50 | import javax.persistence.Entity; 51 | import javax.persistence.Id; 52 | 53 | @Entity 54 | public class Alien 55 | { 56 | @Id 57 | private int aid; 58 | private String aname; 59 | private String color; 60 | 61 | public int getAid() { 62 | return aid; 63 | } 64 | public void setAid(int aid) { 65 | this.aid = aid; 66 | } 67 | public String getAname() { 68 | return aname; 69 | } 70 | public void setAname(String aname) { 71 | this.aname = aname; 72 | } 73 | public String getColor() { 74 | return color; 75 | } 76 | public void setColor(String color) { 77 | this.color = color; 78 | } 79 | 80 | 81 | } 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- 87 | DemoHib/pom.xml 88 | -------------------------------------------------------------------------------- 89 | 90 | 92 | 4.0.0 93 | 94 | com.telusko 95 | DemoHib 96 | 0.0.1-SNAPSHOT 97 | jar 98 | 99 | DemoHib 100 | http://maven.apache.org 101 | 102 | 103 | UTF-8 104 | 105 | 106 | 107 | 108 | junit 109 | junit 110 | 3.8.1 111 | test 112 | 113 | 114 | 115 | 116 | org.hibernate 117 | hibernate-core 118 | 4.1.6.Final 119 | 120 | 121 | 122 | 123 | mysql 124 | mysql-connector-java 125 | 5.1.47 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- 135 | hibernate.cfg.xml 136 | -------------------------------------------------------------------------------- 137 | 138 | 139 | 142 | 143 | 144 | com.mysql.jdbc.Driver 145 | 1234 146 | jdbc:mysql://localhost:3306/neon 147 | root 148 | org.hibernate.dialect.MySQLDialect 149 | update 150 | 151 | 152 | -------------------------------------------------------------------------------- /8 Show sql Property.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | App.java 3 | -------------------------------------------------------------------------------- 4 | 5 | package com.telusko.DemoHib; 6 | 7 | 8 | import org.hibernate.Session; 9 | import org.hibernate.SessionFactory; 10 | import org.hibernate.Transaction; 11 | import org.hibernate.cfg.Configuration; 12 | import org.hibernate.service.ServiceRegistry; 13 | import org.hibernate.service.ServiceRegistryBuilder; 14 | 15 | /** 16 | * Hello world! 17 | * 18 | */ 19 | public class App 20 | { 21 | public static void main( String[] args ) 22 | { 23 | Alien telusko = new Alien(); 24 | telusko.setAid(101); 25 | telusko.setAname("Navin"); 26 | telusko.setColor("Green"); 27 | 28 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 29 | 30 | ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry(); 31 | 32 | SessionFactory sf = con.buildSessionFactory(); 33 | 34 | Session session = sf.openSession(); 35 | 36 | Transaction tx = session.beginTransaction(); 37 | 38 | session.save(telusko); 39 | 40 | tx.commit(); 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- 45 | Alien.java 46 | -------------------------------------------------------------------------------- 47 | 48 | package com.telusko.DemoHib; 49 | 50 | import javax.persistence.Entity; 51 | import javax.persistence.Id; 52 | 53 | @Entity 54 | public class Alien 55 | { 56 | @Id 57 | private int aid; 58 | private String aname; 59 | private String color; 60 | 61 | public int getAid() { 62 | return aid; 63 | } 64 | public void setAid(int aid) { 65 | this.aid = aid; 66 | } 67 | public String getAname() { 68 | return aname; 69 | } 70 | public void setAname(String aname) { 71 | this.aname = aname; 72 | } 73 | public String getColor() { 74 | return color; 75 | } 76 | public void setColor(String color) { 77 | this.color = color; 78 | } 79 | 80 | 81 | } 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- 87 | DemoHib/pom.xml 88 | -------------------------------------------------------------------------------- 89 | 90 | 92 | 4.0.0 93 | 94 | com.telusko 95 | DemoHib 96 | 0.0.1-SNAPSHOT 97 | jar 98 | 99 | DemoHib 100 | http://maven.apache.org 101 | 102 | 103 | UTF-8 104 | 105 | 106 | 107 | 108 | junit 109 | junit 110 | 3.8.1 111 | test 112 | 113 | 114 | 115 | 116 | org.hibernate 117 | hibernate-core 118 | 4.1.6.Final 119 | 120 | 121 | 122 | 123 | mysql 124 | mysql-connector-java 125 | 5.1.47 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- 135 | hibernate.cfg.xml 136 | -------------------------------------------------------------------------------- 137 | 138 | 139 | 142 | 143 | 144 | com.mysql.jdbc.Driver 145 | 1234 146 | jdbc:mysql://localhost:3306/neon 147 | root 148 | org.hibernate.dialect.MySQLDialect 149 | update 150 | true 151 | 152 | 153 | 154 | sachin bansal , flipkart founder 155 | binny bansal , flipkart founder 156 | 157 | 158 | 159 | sabir bhatiya , hotmail founder 160 | Reed Hastings, Netflix ceo 161 | Ritesh agarwal , oyo founder 162 | Sriharsha Majety , ceo swiggy 163 | Jeff Bezos , ceo amazon 164 | ceo swiggy 165 | Drew Houston, founder dropbox 166 | Prashanth Chandrasekar , ceo stack overflow 167 | Shantanu Narayen , ceo adobe 168 | vitalik buterin , founder ethereum 169 | tim berners-lee , html founder 170 | Nat Friedman , Github ceo 171 | 172 | Vijay Shekhar Sharma , paytm founder 173 | -------------------------------------------------------------------------------- /9 Annotation.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | App.java 3 | -------------------------------------------------------------------------------- 4 | 5 | package com.telusko.DemoHib; 6 | 7 | 8 | import org.hibernate.Session; 9 | import org.hibernate.SessionFactory; 10 | import org.hibernate.Transaction; 11 | import org.hibernate.cfg.Configuration; 12 | import org.hibernate.service.ServiceRegistry; 13 | import org.hibernate.service.ServiceRegistryBuilder; 14 | 15 | /** 16 | * Hello world! 17 | * 18 | */ 19 | public class App 20 | { 21 | public static void main( String[] args ) 22 | { 23 | Alien telusko = new Alien(); 24 | telusko.setAid(101); 25 | telusko.setAname("Navin"); 26 | telusko.setColor("Green"); 27 | 28 | Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class); 29 | 30 | ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry(); 31 | 32 | SessionFactory sf = con.buildSessionFactory(); 33 | 34 | Session session = sf.openSession(); 35 | 36 | Transaction tx = session.beginTransaction(); 37 | 38 | session.save(telusko); 39 | 40 | tx.commit(); 41 | } 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- 46 | Alien.java 47 | -------------------------------------------------------------------------------- 48 | 49 | package com.telusko.DemoHib; 50 | 51 | import javax.persistence.Column; 52 | import javax.persistence.Entity; 53 | import javax.persistence.Id; 54 | import javax.persistence.Table; 55 | import javax.persistence.Transient; 56 | 57 | @Entity 58 | @Table(name="alien_table") 59 | public class Alien 60 | { 61 | @Id 62 | private int aid; 63 | @Transient 64 | private String aname; 65 | @Column(name="alien_color") 66 | private String color; 67 | 68 | public int getAid() { 69 | return aid; 70 | } 71 | public void setAid(int aid) { 72 | this.aid = aid; 73 | } 74 | public String getAname() { 75 | return aname; 76 | } 77 | public void setAname(String aname) { 78 | this.aname = aname; 79 | } 80 | public String getColor() { 81 | return color; 82 | } 83 | public void setColor(String color) { 84 | this.color = color; 85 | } 86 | 87 | 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- 92 | hibernate.cfg.xml 93 | -------------------------------------------------------------------------------- 94 | 95 | 96 | 99 | 100 | 101 | com.mysql.jdbc.Driver 102 | 1234 103 | jdbc:mysql://localhost:3306/neon 104 | root 105 | org.hibernate.dialect.MySQLDialect 106 | create 107 | true 108 | 109 | --------------------------------------------------------------------------------