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

📄 entitytest.java

📁 hibernate方便的操作数据库相当不错的 请各位下载看看啊
💻 JAVA
字号:
//$Id: EntityTest.java,v 1.9 2005/02/26 00:57:39 epbernard Exp $package org.hibernate.test.metadata;import org.hibernate.*;import java.util.List;/** * @author Emmanuel Bernard */public class EntityTest extends TestCase {	public EntityTest(String x) {		super(x);	}		public void testLoad() throws Exception {		//put an object in DB		Session s = openSession();		Transaction tx = s.beginTransaction();		Flight firstOne = new Flight();		firstOne.setId( new Long(1) );		firstOne.setName("AF3202");		firstOne.setDuration( new Long(1000000) );		firstOne.setDurationInSec(2000);		s.save(firstOne);		s.flush();		tx.commit();		s.close();				//read it		s = openSession();		tx = s.beginTransaction();		firstOne = (Flight) s.get( Flight.class, new Long(1) );		assertNotNull(firstOne);		assertEquals( new Long(1), firstOne.getId() );		assertEquals( "AF3202", firstOne.getName() );		assertEquals( new Long(1000000), firstOne.getDuration() );		assertFalse( "Transient is not working", 2000l == firstOne.getDurationInSec() );		tx.commit();		s.close();	}		public void testColumn() throws Exception {		//put an object in DB		Session s = openSession();		Transaction tx = s.beginTransaction();		Flight firstOne = new Flight();		firstOne.setId( new Long(1) );		firstOne.setName(null);				try {			s.save(firstOne);			tx.commit();			fail("Name column should be not null");		} catch (HibernateException e) {			//fine		}		finally {			s.close();		}				//insert an object and check that name is not updatable		s = openSession();		tx = s.beginTransaction();		firstOne = new Flight();		firstOne.setId( new Long(1) );		firstOne.setName("AF3202");		firstOne.setTriggeredData("should not be insertable");		tx.commit();		s.close();				s = openSession();		tx = s.beginTransaction();		firstOne = (Flight) s.get( Flight.class, new Long(1) );		assertNotNull(firstOne);		assertEquals( new Long(1), firstOne.getId() );		assertEquals( "AF3202", firstOne.getName() );		assertFalse( "should not be insertable".equals( firstOne.getTriggeredData() ) );		firstOne.setName("BA1234");		firstOne.setTriggeredData("should not be updatable");		tx.commit();		s.close();				s = openSession();		tx = s.beginTransaction();		firstOne = (Flight) s.get( Flight.class, new Long(1) );		assertNotNull(firstOne);		assertEquals( new Long(1), firstOne.getId() );		assertEquals( "AF3202", firstOne.getName() );		assertFalse( "should not be updatable".equals( firstOne.getTriggeredData() ) );		tx.commit();		s.close();	}		public void testColumnUnique() throws Exception  {		Session s;		Transaction tx;		s = openSession();		tx = s.beginTransaction();		Sky sky = new Sky();		sky.id = new Long(2);		sky.color = "blue";		sky.day = "monday";		sky.month="January";		Sky sameSky = new Sky();		sameSky.id = new Long(3);		sameSky.color = "blue";		sky.day = "tuesday";		sky.month="January";		try {			s.save(sky);			s.flush();			s.save(sameSky);			tx.commit();			fail("unique constraints not respected");		}		catch(HibernateException e) {			//success		} finally {			s.close();		}	}	public void testUniqueConstraint() throws Exception  {		int id=5;		Session s;		Transaction tx;		s = openSession();		tx = s.beginTransaction();		Sky sky = new Sky();		sky.id = new Long(id++);		sky.color = "green";		sky.day = "monday";		sky.month="March";		Sky otherSky = new Sky();		otherSky.id = new Long(id++);		otherSky.color = "red";		otherSky.day = "friday";		otherSky.month="March";		Sky sameSky = new Sky();		sameSky.id = new Long(id++);		sameSky.color = "green";		sameSky.day = "monday";		sameSky.month="March";		s.save(sky);		s.flush();		s.save(otherSky);		tx.commit();		s.close();		try {			s.save(sameSky);			tx.commit();			fail("unique constraints not respected");		}		catch(HibernateException e) {			//success		} finally {			s.close();		}	}	public void testVersion() throws Exception {//		put an object in DB		Session s = openSession();		Transaction tx = s.beginTransaction();		Flight firstOne = new Flight();		firstOne.setId( new Long(2) );		firstOne.setName("AF3202");		s.save(firstOne);		s.flush();		tx.commit();		s.close();		//read it		s = openSession();		tx = s.beginTransaction();		firstOne = (Flight) s.get( Flight.class, new Long(2) );		tx.commit();		s.close();				//read it again		s = openSession();		tx = s.beginTransaction();		Flight concurrentOne = (Flight) s.get( Flight.class, new Long(2) );		concurrentOne.setDuration( new Long(1000) );		s.update(concurrentOne);		tx.commit();		s.close();		assertFalse(firstOne ==  concurrentOne);		assertFalse( firstOne.getVersion().equals( concurrentOne.getVersion() ) );				//reattach the first one		s = openSession();		tx = s.beginTransaction();		firstOne.setName("Second access");		s.update(firstOne);		try {			tx.commit();			fail("Optimistic locking should work");		} 		catch (StaleStateException e) {			//fine		} 		finally {			s.close();		}			}		public void testFieldAccess() throws Exception {		Session s;		Transaction tx;		s = openSession();		tx = s.beginTransaction();		Sky sky = new Sky();		sky.id = new Long(1);		sky.color = "black";		s.save(sky);		tx.commit();		s.close();				s = openSession();		tx = s.beginTransaction();		sky = (Sky) s.get(Sky.class, sky.id);		assertNotNull(sky);		assertEquals("black", sky.color);		tx.commit();		s.close();	}	public void testEntityName() throws Exception {		Session s = openSession();		Transaction tx = s.beginTransaction();		Company comp = new Company();		s.persist(comp);		comp.setName("JBoss Inc");		tx.commit();		s.close();		s = openSession();		tx = s.beginTransaction();		List result = s.createQuery("from Corporation").list();		assertNotNull(result);		assertEquals( 1, result.size() );		tx.commit();		s.close();	}	/**	 * @see org.hibernate.test.metadata.TestCase#getMappings()	 */	protected Class[] getMappings() {		return new Class[] {			Flight.class,			Company.class, 			Sky.class		};	}}

⌨️ 快捷键说明

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