1 package org.hibernate.test.dom4j; 2 3 import java.util.List ; 4 5 import junit.framework.Test; 6 import junit.framework.TestSuite; 7 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.test.TestCase; 15 16 19 public class Dom4jManyToOneTest extends TestCase { 20 21 public Dom4jManyToOneTest(String str) { 22 super( str ); 23 } 24 25 public void testDom4jOneToMany() throws Exception { 26 Session s = openSession(); 27 Transaction t = s.beginTransaction(); 28 29 CarType carType = new CarType(); 30 carType.setTypeName("Type 1"); 31 s.save(carType); 32 33 Car car = new Car(); 34 car.setCarType(carType); 35 car.setModel("Model 1"); 36 s.save(car); 37 38 CarPart carPart1 = new CarPart(); 39 carPart1.setPartName("chassis"); 40 car.getCarParts().add(carPart1); 41 42 t.commit(); 43 s.close(); 44 45 s = openSession(); 46 Session dom4jSession = s.getSession( EntityMode.DOM4J ); 47 t = s.beginTransaction(); 48 49 Element element = (Element) dom4jSession.createQuery( "from Car c join fetch c.carParts" ).uniqueResult(); 50 51 String expectedResult = "<car id=\"" + 52 car.getId() + 53 "\"><carPart>" + 54 carPart1.getId() + 55 "</carPart><model>Model 1</model><carType id=\"" + 56 carType.getId() + 57 "\"><typeName>Type 1</typeName></carType></car>"; 58 59 print(element); 60 assertTrue(element.asXML().equals(expectedResult)); 61 62 s.createQuery("delete from CarPart").executeUpdate(); 63 s.createQuery("delete from Car").executeUpdate(); 64 s.createQuery("delete from CarType").executeUpdate(); 65 66 t.commit(); 67 s.close(); 68 } 69 70 public void testDom4jManyToOne() throws Exception { 71 72 Session s = openSession(); 73 Transaction t = s.beginTransaction(); 74 75 CarType carType = new CarType(); 76 carType.setTypeName("Type 1"); 77 s.save(carType); 78 79 Car car1 = new Car(); 80 car1.setCarType(carType); 81 car1.setModel("Model 1"); 82 s.save(car1); 83 84 Car car2 = new Car(); 85 car2.setCarType(carType); 86 car2.setModel("Model 2"); 87 s.save(car2); 88 89 t.commit(); 90 s.close(); 91 92 s = openSession(); 93 Session dom4jSession = s.getSession( EntityMode.DOM4J ); 94 t = s.beginTransaction(); 95 96 List list = dom4jSession.createQuery( "from Car c join fetch c.carType order by c.model asc" ).list(); 97 98 String [] expectedResults = new String [] { 99 "<car id=\"" + 100 car1.getId() + 101 "\"><model>Model 1</model><carType id=\"" + 102 carType.getId() + 103 "\"><typeName>Type 1</typeName></carType></car>", 104 "<car id=\"" + 105 car2.getId() + 106 "\"><model>Model 2</model><carType id=\"" + 107 carType.getId() + 108 "\"><typeName>Type 1</typeName></carType></car>" 109 }; 110 111 for (int i = 0; i < list.size(); i++) { 112 Element element = (Element) list.get(i); 113 114 print(element); 115 assertTrue(element.asXML().equals(expectedResults[i])); 116 } 117 118 s.createQuery("delete from Car").executeUpdate(); 119 s.createQuery("delete from CarType").executeUpdate(); 120 121 t.commit(); 122 s.close(); 123 } 124 125 public static void print(Element elt) throws Exception { 126 OutputFormat outformat = OutputFormat.createPrettyPrint(); 127 XMLWriter writer = new XMLWriter( System.out, outformat ); 129 writer.write( elt ); 130 writer.flush(); 131 } 133 134 protected String [] getMappings() { 135 return new String [] { "dom4j/Car.hbm.xml" }; 136 } 137 138 public static Test suite() { 139 return new TestSuite( Dom4jManyToOneTest.class ); 140 } 141 } 142 | Popular Tags |