📄 onetoonetest.java
字号:
//$Id: OneToOneTest.java,v 1.5 2005/02/25 12:44:42 maxcsaucdk Exp $package org.hibernate.test.metadata.onetoone;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.Query;import org.hibernate.Hibernate;import org.hibernate.test.metadata.TestCase;import org.hibernate.test.metadata.Customer;import org.hibernate.test.metadata.Passport;import org.hibernate.test.metadata.Ticket;import org.hibernate.test.metadata.Discount;/** * @author Emmanuel Bernard */public class OneToOneTest extends TestCase { public OneToOneTest(String x) { super(x); } public void testEagerFetching() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); Client c = new Client(); c.setName("Emmanuel"); Address a = new Address(); a.setCity("Courbevoie"); c.setAddress(a); s.persist(c); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); Query q = s.createQuery("select c from Client c where c.name = :name"); q.setString( "name", c.getName() ); c = (Client) q.uniqueResult(); //c = (Client) s.get(Client.class, c.getId()); assertNotNull(c); tx.commit(); s.close(); assertNotNull( c.getAddress() ); //assertTrue( "Should be eager fetched", Hibernate.isInitialized( c.getAddress() ) ); } public void testOneToOneAssigned() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); Customer c = new Customer(); c.setName("Hibernatus"); Passport p = new Passport(); p.setNumber("123456789"); s.persist(c); //we need the id to assigned it to passport c.setPassport(p); p.setOwner(c); p.setId( c.getId() ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); c = (Customer) s.get( Customer.class, c.getId() ); assertNotNull(c); p = c.getPassport(); assertNotNull( p ); assertEquals( "123456789", p.getNumber() ); assertNotNull( p.getOwner() ); assertEquals( "Hibernatus", p.getOwner().getName() ); tx.commit(); // commit or rollback is the same, we don't care for read queries s.close(); } public void testOneToOneWithExplicitFk() throws Exception { Client c = new Client(); Address a = new Address(); a.setCity("Paris"); c.setName("Emmanuel"); c.setAddress(a); Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.persist(c); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); c = (Client) s.get( Client.class, c.getId() ); assertNotNull(c); assertNotNull( c.getAddress() ); assertEquals( "Paris", c.getAddress().getCity() ); tx.commit(); s.close(); }// FIXME HHH-78 HBX-44 // public void testCompositePk() throws Exception {// Session s;// Transaction tx;// s = openSession();// tx = s.beginTransaction();// ComputerPk cid = new ComputerPk();// cid.setBrand("IBM");// cid.setModel("ThinkPad");// Computer c = new Computer();// c.setId(cid);// c.setCpu("2 GHz");// SerialNumberPk sid = new SerialNumberPk();// sid.setBrand( cid.getBrand() );// sid.setModel( cid.getModel() );// SerialNumber sn = new SerialNumber();// sn.setId(sid);// sn.setValue("REZREZ23424");// c.setSerial(sn);// s.persist(c);// tx.commit();// s.close();//// s = openSession();// tx = s.beginTransaction();// c = (Computer) s.get(Computer.class, cid);// assertNotNull(c);// assertNotNull( c.getSerial() );// assertEquals( sn.getValue(), c.getSerial().getValue() );// 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, Client.class, Address.class, Computer.class, SerialNumber.class }; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -