1 package org.hibernate.test.dom4j; 3 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 7 import org.dom4j.DocumentFactory; 8 import org.dom4j.Element; 9 import org.dom4j.io.OutputFormat; 10 import org.dom4j.io.XMLWriter; 11 import org.hibernate.EntityMode; 12 import org.hibernate.Session; 13 import org.hibernate.Transaction; 14 import org.hibernate.cfg.Configuration; 15 import org.hibernate.cfg.Environment; 16 import org.hibernate.criterion.Example; 17 import org.hibernate.test.TestCase; 18 19 22 public class Dom4jTest extends TestCase { 23 24 public Dom4jTest(String str) { 25 super( str ); 26 } 27 28 76 public void testCompositeId() throws Exception { 77 Element a = DocumentFactory.getInstance().createElement( "a" ); 78 a.addAttribute("id", "1"); 79 a.addElement("x").setText("foo bar"); 80 Element b = a.addElement("b"); 82 b.addAttribute("bId", "1"); 85 b.addAttribute("aId", "1"); 86 b.setText("foo foo"); 87 b = a.addElement("b"); 88 b.addAttribute("bId", "2"); 91 b.addAttribute("aId", "1"); 92 b.setText("bar bar"); 93 94 Session s = openSession(); 95 Transaction t = s.beginTransaction(); 96 s.persist("A", a); 97 t.commit(); 98 s.close(); 99 100 s = openSession(); 101 t = s.beginTransaction(); 102 a = (Element) s.createCriteria("A").uniqueResult(); 103 assertEquals( a.elements("b").size(), 2 ); 104 print(a); 105 s.delete("A", a); 106 t.commit(); 107 s.close(); 108 } 109 110 public void testDom4j() throws Exception { 111 Element acct = DocumentFactory.getInstance().createElement( "account" ); 112 acct.addAttribute( "id", "abc123" ); 113 acct.addElement( "balance" ).setText( "123.45" ); 114 Element cust = acct.addElement( "customer" ); 115 cust.addAttribute( "id", "xyz123" ); 116 Element foo1 = cust.addElement( "stuff" ).addElement( "foo" ); 117 foo1.setText( "foo" ); 118 foo1.addAttribute("bar", "x"); 119 Element foo2 = cust.element( "stuff" ).addElement( "foo" ); 120 foo2.setText( "bar" ); 121 foo2.addAttribute("bar", "y"); 122 cust.addElement( "amount" ).setText( "45" ); 123 cust.setText( "An example customer" ); 124 Element name = cust.addElement( "name" ); 125 name.addElement( "first" ).setText( "Gavin" ); 126 name.addElement( "last" ).setText( "King" ); 127 128 Element loc = DocumentFactory.getInstance().createElement( "location" ); 129 loc.addElement( "address" ).setText( "Karbarook Avenue" ); 130 131 print( acct ); 132 133 Session s = openSession(); 134 Transaction t = s.beginTransaction(); 135 s.persist( "Location", loc ); 136 cust.addElement( "location" ).addAttribute( "id", loc.attributeValue( "id" ) ); 137 s.persist( "Account", acct ); 138 t.commit(); 139 s.close(); 140 141 print( loc ); 142 143 s = openSession(); 144 t = s.beginTransaction(); 145 cust = (Element) s.get( "Customer", "xyz123" ); 146 print( cust ); 147 acct = (Element) s.get( "Account", "abc123" ); 148 print( acct ); 149 assertEquals( acct.element( "customer" ), cust ); 150 cust.element( "name" ).element( "first" ).setText( "Gavin A" ); 151 Element foo3 = cust.element("stuff").addElement("foo"); 152 foo3.setText("baz"); 153 foo3.addAttribute("bar", "z"); 154 cust.element("amount").setText("3"); 155 cust.addElement("amount").setText("56"); 156 t.commit(); 157 s.close(); 158 159 System.out.println(); 160 161 acct.element( "balance" ).setText( "3456.12" ); 162 cust.addElement( "address" ).setText( "Karbarook Ave" ); 163 164 assertEquals( acct.element( "customer" ), cust ); 165 166 cust.setText( "Still the same example!" ); 167 168 s = openSession(); 169 t = s.beginTransaction(); 170 s.saveOrUpdate( "Account", acct ); 171 t.commit(); 172 s.close(); 173 174 s = openSession(); 175 t = s.beginTransaction(); 176 cust = (Element) s.get( "Customer", "xyz123" ); 177 print( cust ); 178 acct = (Element) s.get( "Account", "abc123" ); 179 print( acct ); 180 assertEquals( acct.element( "customer" ), cust ); 181 t.commit(); 182 s.close(); 183 184 System.out.println(); 185 186 s = openSession(); 187 t = s.beginTransaction(); 188 cust = (Element) s.createCriteria( "Customer" ) 189 .add( Example.create( cust ) ) 190 .uniqueResult(); 191 print( cust ); 192 t.commit(); 193 s.close(); 194 195 System.out.println(); 196 197 s = openSession(); 198 t = s.beginTransaction(); 199 acct = (Element) s.createQuery( "from Account a left join fetch a.customer" ) 200 .uniqueResult(); 201 print( acct ); 202 t.commit(); 203 s.close(); 204 205 System.out.println(); 206 207 s = openSession(); 208 t = s.beginTransaction(); 209 acct = (Element) s.createQuery( "from Account" ).uniqueResult(); 210 print( acct ); 211 t.commit(); 212 s.close(); 213 214 System.out.println(); 215 216 s = openSession(); 217 t = s.beginTransaction(); 218 cust = (Element) s.createQuery( "from Customer c left join fetch c.stuff" ).uniqueResult(); 219 print( cust ); 220 t.commit(); 221 s.close(); 222 223 System.out.println(); 224 225 s = openSession(); 226 t = s.beginTransaction(); 227 cust = (Element) s.createQuery( "from Customer c left join fetch c.morestuff" ).uniqueResult(); 228 print( cust ); 229 t.commit(); 230 s.close(); 231 232 System.out.println(); 233 234 s = openSession(); 235 t = s.beginTransaction(); 236 cust = (Element) s.createQuery( "from Customer c left join fetch c.morestuff" ).uniqueResult(); 237 cust = (Element) s.createQuery( "from Customer c left join fetch c.stuff" ).uniqueResult(); 238 print( cust ); 239 t.commit(); 240 s.close(); 241 242 System.out.println(); 243 244 s = openSession(); 245 t = s.beginTransaction(); 246 cust = (Element) s.createQuery( "from Customer c left join fetch c.accounts" ).uniqueResult(); 247 Element a1 = cust.element( "accounts" ).addElement( "account" ); 248 a1.addElement( "balance" ).setText( "12.67" ); 249 a1.addAttribute( "id", "lkj345" ); 250 a1.addAttribute("acnum", "0"); 251 Element a2 = cust.element( "accounts" ).addElement( "account" ); 252 a2.addElement( "balance" ).setText( "10000.00" ); 253 a2.addAttribute( "id", "hsh987" ); 254 a2.addAttribute("acnum", "1"); 255 print( cust ); 256 t.commit(); 257 s.close(); 258 259 System.out.println(); 260 261 s = openSession(); 262 t = s.beginTransaction(); 263 cust = (Element) s.createQuery( "from Customer c left join fetch c.accounts" ).uniqueResult(); 264 print( cust ); 265 t.commit(); 266 s.close(); 267 } 268 269 protected String [] getMappings() { 270 return new String [] { "dom4j/Account.hbm.xml", "dom4j/AB.hbm.xml", "dom4j/Employer.hbm.xml" }; 271 } 272 273 public static Test suite() { 274 return new TestSuite( Dom4jTest.class ); 275 } 276 277 public static void print(Element elt) throws Exception { 278 OutputFormat outformat = OutputFormat.createPrettyPrint(); 279 XMLWriter writer = new XMLWriter( System.out, outformat ); 281 writer.write( elt ); 282 writer.flush(); 283 System.out.println( "" ); 284 } 286 287 protected void configure(Configuration cfg) { 288 cfg.setProperty( Environment.DEFAULT_ENTITY_MODE, EntityMode.DOM4J.toString() ); 290 } 291 } 292 | Popular Tags |