📄 indexedcollectiontest.java
字号:
primeMinisterEntry.setCity( "Paris" ); primeMinisterEntry.setStreet( "Hotel Matignon" ); primeMinisterEntry.setPerson( primeMinister ); book.getEntries().put( helene, heleneEntry ); book.getEntries().put( primeMinister, primeMinisterEntry ); s.persist( book ); s.flush(); s.clear(); book = (AddressBook) s.get( AddressBook.class, book.getId() ); assertEquals( 2, book.getLastNameEntries().size() ); assertEquals( heleneEntry.getCity(), book.getLastNameEntries().get( "Michau" ).getCity() ); AddressEntryPk fake = new AddressEntryPk( "Fake", "Fake" ); book.getEntries().put( fake, primeMinisterEntry ); s.flush(); s.clear(); book = (AddressBook) s.get( AddressBook.class, book.getId() ); assertEquals( 2, book.getEntries().size() ); assertNull( book.getEntries().get( fake ) ); s.delete( book ); tx.rollback(); s.close(); } public void testMapKeyOnManyToMany() throws Exception { Session s; s = openSession(); s.getTransaction().begin(); News airplane = new News(); airplane.setTitle( "Crash!" ); airplane.setDetail( "An airplaned crashed." ); s.persist( airplane ); Newspaper lemonde = new Newspaper(); lemonde.setName( "Lemonde" ); lemonde.getNews().put( airplane.getTitle(), airplane ); s.persist( lemonde ); s.flush(); s.clear(); lemonde = (Newspaper) s.get( Newspaper.class, lemonde.getId() ); assertEquals( 1, lemonde.getNews().size() ); News news = lemonde.getNews().get( airplane.getTitle() ); assertNotNull( news ); assertEquals( airplane.getTitle(), news.getTitle() ); s.delete( lemonde ); s.delete( news ); s.getTransaction().rollback(); s.close(); } public void testMapKeyOnManyToManyOnId() throws Exception { Session s; s = openSession(); s.getTransaction().begin(); News hibernate1 = new News(); hibernate1.setTitle( "#1 ORM solution in the Java world" ); hibernate1.setDetail( "Well, that's no news ;-)" ); s.persist( hibernate1 ); PressReleaseAgency schwartz = new PressReleaseAgency(); schwartz.setName( "Schwartz" ); schwartz.getProvidedNews().put( hibernate1.getId(), hibernate1 ); s.persist( schwartz ); s.flush(); s.clear(); schwartz = (PressReleaseAgency) s.get( PressReleaseAgency.class, schwartz.getId() ); assertEquals( 1, schwartz.getProvidedNews().size() ); News news = schwartz.getProvidedNews().get( hibernate1.getId() ); assertNotNull( news ); assertEquals( hibernate1.getTitle(), news.getTitle() ); s.delete( schwartz ); s.delete( news ); s.getTransaction().rollback(); s.close(); } public void testMapKeyAndIdClass() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Painter picasso = new Painter(); Painting laVie = new Painting( "La Vie", "Picasso", 50, 20 ); picasso.getPaintings().put( "La Vie", laVie ); Painting famille = new Painting( "La Famille du Saltimbanque", "Picasso", 50, 20 ); picasso.getPaintings().put( "La Famille du Saltimbanque", famille ); s.persist( picasso ); s.flush(); s.clear(); picasso = (Painter) s.get( Painter.class, picasso.getId() ); Painting painting = picasso.getPaintings().get( famille.getName() ); assertNotNull( painting ); assertEquals( painting.getName(), famille.getName() ); s.delete( picasso ); tx.rollback(); s.close(); } public void testRealMap() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Atmosphere atm = new Atmosphere(); Atmosphere atm2 = new Atmosphere(); GasKey key = new GasKey(); key.setName( "O2" ); Gas o2 = new Gas(); o2.name = "oxygen"; atm.gases.put( "100%", o2 ); atm.gasesPerKey.put( key, o2 ); atm2.gases.put( "100%", o2 ); atm2.gasesPerKey.put( key, o2 ); s.persist( key ); s.persist( atm ); s.persist( atm2 ); s.flush(); s.clear(); atm = (Atmosphere) s.get( Atmosphere.class, atm.id ); key = (GasKey) s.get( GasKey.class, key.getName() ); assertEquals( 1, atm.gases.size() ); assertEquals( o2.name, atm.gases.get( "100%" ).name ); assertEquals( o2.name, atm.gasesPerKey.get( key ).name ); tx.rollback(); s.close(); } public void testMapKeyEntityEntity() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); AddressBook book = new AddressBook(); s.persist( book ); AddressEntry entry = new AddressEntry(); entry.setCity( "Atlanta"); AddressEntryPk pk = new AddressEntryPk("Coca", "Cola" ); entry.setPerson( pk ); entry.setBook( book ); AlphabeticalDirectory ad = new AlphabeticalDirectory(); ad.setName( "C"); s.persist( ad ); entry.setDirectory( ad ); s.persist( entry ); book.getDirectoryEntries().put( ad, entry ); s.flush(); s.clear(); book = (AddressBook) s.get( AddressBook.class, book.getId() ); assertEquals( 1, book.getDirectoryEntries().size() ); assertEquals( "C", book.getDirectoryEntries().keySet().iterator().next().getName() ); tx.rollback(); s.close(); } public void testEntityKeyElementTarget() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Atmosphere atm = new Atmosphere(); Gas o2 = new Gas(); o2.name = "oxygen"; atm.composition.put( o2, 94.3 ); s.persist( o2 ); s.persist( atm ); s.flush(); s.clear(); atm = (Atmosphere) s.get( Atmosphere.class, atm.id ); assertTrue( ! Hibernate.isInitialized( atm.composition ) ); assertEquals( 1, atm.composition.size() ); assertEquals( o2.name, atm.composition.keySet().iterator().next().name ); tx.rollback(); s.close(); } public void testSortedMap() { Session s = openSession(); Transaction tx = s.beginTransaction(); Training training = new Training(); Trainee trainee = new Trainee(); trainee.setName( "Jim" ); Trainee trainee2 = new Trainee(); trainee2.setName( "Emmanuel" ); s.persist( trainee ); s.persist( trainee2 ); training.getTrainees().put( "Jim", trainee ); training.getTrainees().put( "Emmanuel", trainee2 ); s.persist( training ); s.flush(); s.clear(); training = (Training) s.get( Training.class, training.getId() ); assertEquals( "Emmanuel", training.getTrainees().firstKey() ); assertEquals( "Jim", training.getTrainees().lastKey() ); tx.rollback(); s.close(); } public void testMapKeyLoad() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); Software hibernate = new Software(); hibernate.setName( "Hibernate" ); Version v1 = new Version(); v1.setCodeName( "HumbaHumba" ); v1.setNumber( "1.0" ); v1.setSoftware( hibernate ); hibernate.addVersion( v1 ); s.persist( hibernate ); s.persist( v1 ); s.flush(); s.clear(); hibernate = (Software) s.get( Software.class, "Hibernate" ); assertEquals(1, hibernate.getVersions().size() ); Version v2 = new Version(); v2.setCodeName( "HumbaHumba2" ); v2.setNumber( "2.0" ); v2.setSoftware( hibernate ); hibernate.addVersion( v2 ); assertEquals( "One loaded persisted version, and one just added", 2, hibernate.getVersions().size() ); s.flush(); s.clear(); hibernate = (Software) s.get( Software.class, "Hibernate" ); for ( Version v : hibernate.getVersions().values() ) { s.delete( v ); } s.delete( hibernate ); tx.rollback(); s.close(); } public IndexedCollectionTest(String x) { super( x ); } protected Class[] getMappings() { return new Class[]{ Wardrobe.class, Drawer.class, Dress.class, Software.class, Version.class, AddressBook.class, AddressEntry.class, AddressEntryPk.class, //should be silently ignored Newspaper.class, News.class, PressReleaseAgency.class, Painter.class, Painting.class, Atmosphere.class, Gas.class, AlphabeticalDirectory.class, GasKey.class, Trainee.class, Training.class }; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -