⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manytoonetest.java

📁 hibernate方便的操作数据库相当不错的 请各位下载看看啊
💻 JAVA
字号:
//$Id: ManyToOneTest.java,v 1.5 2005/02/28 12:11:58 epbernard Exp $package org.hibernate.test.metadata.manytoone;import java.util.ArrayList;import java.util.Collection;import java.util.List;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.Query;import org.hibernate.test.metadata.TestCase;import org.hibernate.test.metadata.Flight;import org.hibernate.test.metadata.Company;import org.hibernate.test.metadata.Discount;import org.hibernate.test.metadata.Customer;import org.hibernate.test.metadata.Ticket;import org.hibernate.test.metadata.Passport;/** * @author Emmanuel Bernard */public class ManyToOneTest extends TestCase {	public ManyToOneTest(String x) {		super(x);	}		public void testCreate() throws Exception {		Session s;		Transaction tx;		s = openSession();		tx = s.beginTransaction();		Flight firstOne = new Flight();		firstOne.setId( new Long(1) );		firstOne.setName("AF0101");		Company frenchOne = new Company();		frenchOne.setName("Air France");		firstOne.setCompany(frenchOne);		s.persist(firstOne);		tx.commit();		s.close();		assertNotNull("identity id should work", frenchOne.getId() );				s = openSession();		tx = s.beginTransaction();		firstOne = (Flight) s.get(Flight.class, new Long(1));		assertNotNull(firstOne.getCompany());		assertEquals(frenchOne.getName(), firstOne.getCompany().getName());		tx.commit();		s.close();	}		public void testCascade() throws Exception {		Session s;		Transaction tx;		s = openSession();		tx = s.beginTransaction();		Discount discount = new Discount();		discount.setDiscount(20.12);		Customer customer = new Customer();		Collection discounts = new ArrayList();		discounts.add(discount);		customer.setName("Quentin Tarantino");		discount.setOwner(customer);		customer.setDiscountTickets(discounts);		s.persist(discount);		tx.commit();		s.close();				s = openSession();		tx = s.beginTransaction();		discount = (Discount) s.get( Discount.class, discount.getId() );		assertNotNull(discount);		assertEquals( 20.12, discount.getDiscount() );		assertNotNull( discount.getOwner() );		customer = new Customer();		customer.setName("Clooney");		discount.setOwner(customer);		discounts = new ArrayList();		discounts.add(discount);		customer.setDiscountTickets(discounts);		tx.commit();		s.close();				s = openSession();		tx = s.beginTransaction();		discount = (Discount) s.get( Discount.class, discount.getId() );		assertNotNull(discount);		assertNotNull( discount.getOwner() );		assertEquals( "Clooney", discount.getOwner().getName() );		tx.commit();		s.close();					}		public void testFetch() throws Exception {		Session s;		Transaction tx;		s = openSession();		tx = s.beginTransaction();		Discount discount = new Discount();		discount.setDiscount(20);		Customer customer = new Customer();		Collection discounts = new ArrayList();		discounts.add(discount);		customer.setName("Quentin Tarantino");		discount.setOwner(customer);		customer.setDiscountTickets(discounts);		s.persist(discount);		tx.commit();		s.close();				s = openSession();		tx = s.beginTransaction();		discount = (Discount) s.get( Discount.class, discount.getId() );		assertNotNull(discount);		assertFalse( Hibernate.isInitialized( discount.getOwner() ) );		tx.commit();		s.close();	}	public void testCompositeFK() throws Exception {		Session s;		Transaction tx;		s = openSession();		tx = s.beginTransaction();		ParentPk ppk = new ParentPk();		ppk.firstName = "John";		ppk.lastName = "Doe";		Parent p = new Parent();		p.age = 45;		p.id = ppk;		s.persist(p);		Child c = new Child();		c.parent = p;		s.persist(c);		tx.commit();		s.close();		s = openSession();		tx = s.beginTransaction();		//FIXME: fix this when the small parser bug will be fixed 		Query q = s.createQuery("from " + Child.class.getName() ); //+ " c where c.parent.id.lastName = :lastName");		//q.setString("lastName", p.id.lastName);		List result = q.list();		assertEquals( 1, result.size() );		Child c2 = (Child) result.get(0);		assertEquals(c2.id,  c.id);		tx.commit();		s.close();	}	public void testImplicitCompositeFk() throws Exception {		Session s;		Transaction tx;		s = openSession();		tx = s.beginTransaction();		Node n1 = new Node();		n1.setDescription("Parent");		NodePk n1pk = new NodePk();		n1pk.setLevel(1);		n1pk.setName("Root");		n1.setId(n1pk);		Node n2 = new Node();		NodePk n2pk = new NodePk();		n2pk.setLevel(2);		n2pk.setName("Level 1: A");		n2.setParent(n1);		n2.setId(n2pk);		s.persist(n2);		tx.commit();		s = openSession();		tx = s.beginTransaction();		n2 = (Node) s.get(Node.class, n2pk);		assertNotNull(n2);		assertNotNull(n2.getParent());		assertEquals(1, n2.getParent().getId().getLevel() );		tx.commit();		s.close();	}	/**	 * @see org.hibernate.test.metadata.TestCase#getMappings()	 */	protected Class[] getMappings() {		return new Class[] {			Flight.class,			Company.class,			Customer.class,			Discount.class,			Ticket.class,			Passport.class,			Parent.class,			Child.class,			Node.class		};	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -