1 package org.hibernate.test.legacy; 3 4 import java.sql.SQLException ; 5 import java.util.ArrayList ; 6 import java.util.List ; 7 8 import org.hibernate.HibernateException; 9 import org.hibernate.classic.Session; 10 import org.hibernate.dialect.HSQLDialect; 11 import org.hibernate.dialect.MySQLDialect; 12 import org.hibernate.test.TestCase; 13 14 18 public class CustomSQLTest extends TestCase { 19 20 public CustomSQLTest(String name) { 21 super(name); 22 } 23 24 public String [] getMappings() { 25 return new String [] { "legacy/CustomSQL.hbm.xml" }; 26 } 27 28 public void testInsert() throws HibernateException, SQLException { 29 30 if ( getDialect() instanceof HSQLDialect ) return; 31 if ( getDialect() instanceof MySQLDialect ) return; 32 33 Role p = new Role(); 34 35 p.setName("Patient"); 36 37 Session s = openSession(); 38 39 s.save(p); 40 s.flush(); 41 42 s.connection().commit(); 43 s.close(); 44 45 getSessions().evict(Role.class); 46 s = openSession(); 47 48 Role p2 = (Role) s.get(Role.class, new Long (p.getId())); 49 assertNotSame(p, p2); 50 assertEquals(p2.getId(),p.getId()); 51 assertTrue(p2.getName().equalsIgnoreCase(p.getName())); 52 s.delete(p2); 53 s.flush(); 54 55 56 s.connection().commit(); 57 s.close(); 58 59 60 } 61 62 public void testJoinedSubclass() throws HibernateException, SQLException { 63 Medication m = new Medication(); 64 65 m.setPrescribedDrug(new Drug()); 66 67 m.getPrescribedDrug().setName("Morphine"); 68 69 70 Session s = openSession(); 71 72 s.save(m.getPrescribedDrug()); 73 s.save(m); 74 75 s.flush(); 76 s.connection().commit(); 77 s.close(); 78 s = openSession(); 79 80 Medication m2 = (Medication) s.get(Medication.class, m.getId()); 81 assertNotSame(m, m2); 82 83 s.flush(); 84 s.connection().commit(); 85 s.close(); 86 87 } 88 89 public void testCollectionCUD() throws HibernateException, SQLException { 90 91 if ( getDialect() instanceof HSQLDialect ) return; 92 if ( getDialect() instanceof MySQLDialect ) return; 93 94 Role role = new Role(); 95 96 role.setName("Jim Flanders"); 97 98 Intervention iv = new Medication(); 99 iv.setDescription("JF medical intervention"); 100 101 role.getInterventions().add(iv); 102 103 List sx = new ArrayList (); 104 sx.add("somewhere"); 105 sx.add("somehow"); 106 sx.add("whatever"); 107 role.setBunchOfStrings(sx); 108 109 Session s = openSession(); 110 111 s.save(role); 112 s.flush(); 113 s.connection().commit(); 114 s.close(); 115 116 s = openSession(); 117 118 Role r = (Role) s.get(Role.class,new Long (role.getId())); 119 assertNotSame(role,r); 120 121 assertEquals(1,r.getInterventions().size()); 122 123 assertEquals(3, r.getBunchOfStrings().size()); 124 125 r.getBunchOfStrings().set(1, "replacement"); 126 s.flush(); 127 s.connection().commit(); 128 s.close(); 129 130 s = openSession(); 131 132 r = (Role) s.get(Role.class,new Long (role.getId())); 133 assertNotSame(role,r); 134 135 assertEquals(r.getBunchOfStrings().get(1),"replacement"); 136 assertEquals(3, r.getBunchOfStrings().size()); 137 138 r.getBunchOfStrings().set(1, "replacement"); 139 140 r.getBunchOfStrings().remove(1); 141 s.flush(); 142 143 r.getBunchOfStrings().clear(); 144 s.flush(); 145 146 s.connection().commit(); 147 s.close(); 148 149 } 150 151 public void testCRUD() throws HibernateException, SQLException { 152 153 if ( getDialect() instanceof HSQLDialect ) return; 154 if ( getDialect() instanceof MySQLDialect ) return; 155 156 Person p = new Person(); 157 158 p.setName("Max"); 159 p.setLastName("Andersen"); 160 p.setNationalID("110974XYZÅ"); 161 p.setAddress("P. P. Street 8"); 162 163 Session s = openSession(); 164 165 s.save(p); 166 s.flush(); 167 168 s.connection().commit(); 169 s.close(); 170 171 getSessions().evict(Person.class); 172 s = openSession(); 173 174 Person p2 = (Person) s.get(Person.class, p.getId()); 175 assertNotSame(p, p2); 176 assertEquals(p2.getId(),p.getId()); 177 assertEquals(p2.getLastName(),p.getLastName()); 178 s.flush(); 179 180 List list = s.find("select p from Party as p"); 181 assertTrue(list.size() == 1); 182 183 s.connection().commit(); 184 s.close(); 185 186 s = openSession(); 187 188 list = s.find("select p from Person as p where p.address = 'Lærkevænget 1'"); 189 assertTrue(list.size() == 0); 190 p.setAddress("Lærkevænget 1"); 191 s.update(p); 192 list = s.find("select p from Person as p where p.address = 'Lærkevænget 1'"); 193 assertTrue(list.size() == 1); 194 list = s.find("select p from Party as p where p.address = 'P. P. Street 8'"); 195 assertTrue(list.size() == 0); 196 197 s.delete(p); 198 list = s.find("select p from Person as p"); 199 assertTrue(list.size() == 0); 200 201 s.connection().commit(); 202 s.close(); 203 204 205 } 206 } 207 | Popular Tags |