Create Query From Entity Manager
1 File: Professor.java 2 3 4 import javax.persistence.Entity; 5 import javax.persistence.Id; 6 7 @Entity 8 public class Professor { 9 @Id 10 private int id; 11 private String name; 12 private long salary; 13 14 public Professor() {} 15 public Professor(int id) { 16 this.id = id; 17 } 18 19 public int getId() { 20 return id; 21 } 22 23 public void setId(int id) { 24 this.id = id; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 public long getSalary() { 36 return salary; 37 } 38 39 public void setSalary(long salary) { 40 this.salary = salary; 41 } 42 43 public String toString() { 44 return "Professor id: " + getId() + " name: " + getName() + " salary: " + getSalary(); 45 } 46 } 47 48 49 File: ProfessorService.java 50 51 import java.util.Collection; 52 53 import javax.persistence.EntityManager; 54 import javax.persistence.Query; 55 56 public class ProfessorService { 57 protected EntityManager em; 58 59 public ProfessorService(EntityManager em) { 60 this.em = em; 61 } 62 63 public Professor createProfessor(int id, String name, long salary) { 64 Professor emp = new Professor(id); 65 emp.setName(name); 66 emp.setSalary(salary); 67 em.persist(emp); 68 return emp; 69 } 70 71 public void removeProfessor(int id) { 72 Professor emp = findProfessor(id); 73 if (emp != null) { 74 em.remove(emp); 75 } 76 } 77 78 public Professor raiseProfessorSalary(int id, long raise) { 79 Professor emp = em.find(Professor.class, id); 80 if (emp != null) { 81 emp.setSalary(emp.getSalary() + raise); 82 } 83 return emp; 84 } 85 86 public Professor findProfessor(int id) { 87 return em.find(Professor.class, id); 88 } 89 90 public Collection<Professor> findAllProfessors() { 91 Query query = em.createQuery("SELECT e FROM Professor e"); 92 return (Collection<Professor>) query.getResultList(); 93 } 94 } 95 96 97 File: JPAUtil.java 98 99 import java.io.Reader; 100 import java.sql.Connection; 101 import java.sql.DriverManager; 102 import java.sql.ResultSet; 103 import java.sql.ResultSetMetaData; 104 import java.sql.Statement; 105 106 public class JPAUtil { 107 Statement st; 108 109 public JPAUtil() throws Exception{ 110 Class.forName("org.hsqldb.jdbcDriver"); 111 System.out.println("Driver Loaded."); 112 String url = "jdbc:hsqldb:data/tutorial"; 113 114 Connection conn = DriverManager.getConnection(url, "sa", ""); 115 System.out.println("Got Connection."); 116 st = conn.createStatement(); 117 } 118 public void executeSQLCommand(String sql) throws Exception { 119 st.executeUpdate(sql); 120 } 121 public void checkData(String sql) throws Exception { 122 ResultSet rs = st.executeQuery(sql); 123 ResultSetMetaData metadata = rs.getMetaData(); 124 125 for (int i = 0; i < metadata.getColumnCount(); i++) { 126 System.out.print("\t"+ metadata.getColumnLabel(i + 1)); 127 } 128 System.out.println("\n----------------------------------"); 129 130 while (rs.next()) { 131 for (int i = 0; i < metadata.getColumnCount(); i++) { 132 Object value = rs.getObject(i + 1); 133 if (value == null) { 134 System.out.print("\t "); 135 } else { 136 System.out.print("\t"+value.toString().trim()); 137 } 138 } 139 System.out.println(""); 140 } 141 } 142 } 143 144 145 File: Main.java 146 147 import java.util.Collection; 148 import java.util.Iterator; 149 150 import javax.persistence.EntityManager; 151 import javax.persistence.EntityManagerFactory; 152 import javax.persistence.Persistence; 153 154 public class Main { 155 public static void main(String[] a) throws Exception { 156 JPAUtil util = new JPAUtil(); 157 158 EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService"); 159 EntityManager em = emf.createEntityManager(); 160 ProfessorService service = new ProfessorService(em); 161 162 em.getTransaction().begin(); 163 164 Professor emp = service.createProfessor(1,"name", 100); 165 emp = service.createProfessor(2,"name 2", 100); 166 167 Collection emps = em.createQuery("SELECT e FROM Professor e").getResultList(); 168 for (Iterator i = emps.iterator(); i.hasNext();) { 169 Professor e = (Professor) i.next(); 170 System.out.println("Professor " + e.getId() + ", " + e.getName()); 171 } 172 util.checkData("select * from Professor"); 173 174 em.getTransaction().commit(); 175 em.close(); 176 emf.close(); 177 } 178 } 179 180 181 182 183 File: persistence.xml 184 185 <persistence xmlns="http://java.sun.com/xml/ns/persistence" 186 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 187 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence" version="1.0"> 188 <persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL"> 189 <properties> 190 <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 191 <property name="hibernate.hbm2ddl.auto" value="update"/> 192 <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> 193 <property name="hibernate.connection.username" value="sa"/> 194 <property name="hibernate.connection.password" value=""/> 195 <property name="hibernate.connection.url" value="jdbc:hsqldb:data/tutorial"/> 196 </properties> 197 </persistence-unit> 198 </persistence>
posted on 2013-06-05 01:08 Step-BY-Step 阅读(321) 评论(0) 编辑 收藏 举报