📄 onetomanytest.java
字号:
//$Id: OneToManyTest.java,v 1.2 2005/02/25 12:44:42 maxcsaucdk Exp $package org.hibernate.test.metadata.onetomany;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.List;import java.util.Set;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.test.metadata.TestCase;import org.hibernate.test.metadata.Ticket;import org.hibernate.test.metadata.Customer;import org.hibernate.test.metadata.Discount;import org.hibernate.test.metadata.Passport;/** * @author Emmanuel Bernard */public class OneToManyTest extends TestCase { public OneToManyTest(String x) { super(x); } public void testSimpleOneToManySet() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); Ticket t = new Ticket(); t.setNumber("33A"); Customer c = new Customer(); Set<Ticket> tickets = new HashSet(); tickets.add(t); t.setCustomer(c); c.setTickets(tickets); s.persist(c); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); c = (Customer) s.load( Customer.class, c.getId() ); assertNotNull(c); assertTrue( Hibernate.isInitialized( c.getTickets() ) ); assertNotNull( c.getTickets() ); tickets = c.getTickets(); assertTrue(tickets.size() > 0); tx.commit(); s.close(); } public void testSimpleOneToManyCollection() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); Discount d = new Discount(); d.setDiscount(10); Customer c = new Customer(); List discounts = new ArrayList(); discounts.add(d); d.setOwner(c); c.setDiscountTickets(discounts); s.persist(c); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); c = (Customer) s.load( Customer.class, c.getId() ); assertNotNull(c); assertFalse( Hibernate.isInitialized( c.getDiscountTickets() ) ); assertNotNull( c.getDiscountTickets() ); Collection collecDiscount = c.getDiscountTickets(); assertTrue(collecDiscount.size() > 0); tx.commit(); s.close(); } public void testJoinColumns() throws Exception { Parent parent = new Parent(); ParentPk pk = new ParentPk(); pk.firstName = "Bruce"; pk.lastName = "Willis"; pk.isMale = true; parent.id = pk; parent.age = 40; List list = new ArrayList(); Child child = new Child(); Child child2 = new Child(); parent.addChild(child); parent.addChild(child2); Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.persist(parent); tx.commit(); s.close(); assertNotNull(child.id); assertNotNull(child2.id); assertNotSame(child.id, child2.id); s = openSession(); tx = s.beginTransaction(); parent = (Parent) s.get(Parent.class, pk); assertNotNull(parent.children); Hibernate.initialize(parent.children); assertEquals( 2, parent.children.size() ); tx.commit(); s.close(); } /** * @see org.hibernate.test.metadata.TestCase#getMappings() */ protected Class[] getMappings() { return new Class[] { Customer.class, Ticket.class, Discount.class, Passport.class, Parent.class, Child.class }; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -