customsqltest.java

来自「好东西,hibernate-3.2.0,他是一开元的树杖hibernate-3.」· Java 代码 · 共 159 行

JAVA
159
字号
//$Id: CustomSQLTest.java 8894 2005-12-21 13:22:50Z steveebersole $
package org.hibernate.test.legacy;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQLDialect;

/**
 * @author MAX
 *
 */
public class CustomSQLTest extends LegacyTestCase {

	public CustomSQLTest(String name) {
		super(name);
	}

	public String[] getMappings() {
		return new String[] { "legacy/CustomSQL.hbm.xml" };
	}

	public void testInsert() throws HibernateException, SQLException {

		if ( getDialect() instanceof HSQLDialect ) return;
		if ( getDialect() instanceof MySQLDialect ) return;
		
		Role p = new Role();

		p.setName("Patient");

		Session s = openSession();

		s.save(p);
		s.flush();

		s.connection().commit();
		s.close();

		getSessions().evict(Role.class);
		s = openSession();

		Role p2 = (Role) s.get(Role.class, new Long(p.getId()));
		assertNotSame(p, p2);
		assertEquals(p2.getId(),p.getId());
		assertTrue(p2.getName().equalsIgnoreCase(p.getName()));
		s.delete(p2);
		s.flush();


		s.connection().commit();
		s.close();


	}

	public void testJoinedSubclass() throws HibernateException, SQLException {
		Medication m = new Medication();

		m.setPrescribedDrug(new Drug());

		m.getPrescribedDrug().setName("Morphine");


		Session s = openSession();

		s.save(m.getPrescribedDrug());
		s.save(m);

		s.flush();
		s.connection().commit();
		s.close();
		s = openSession();

		Medication m2  = (Medication) s.get(Medication.class, m.getId());
		assertNotSame(m, m2);

		s.flush();
		s.connection().commit();
		s.close();

	}

	public void testCollectionCUD() throws HibernateException, SQLException {
		
		if ( getDialect() instanceof HSQLDialect ) return;
		if ( getDialect() instanceof MySQLDialect ) return;
		
		Role role = new Role();

		role.setName("Jim Flanders");

		Intervention iv = new Medication();
		iv.setDescription("JF medical intervention");

		role.getInterventions().add(iv);

		List sx = new ArrayList();
		sx.add("somewhere");
		sx.add("somehow");
		sx.add("whatever");
		role.setBunchOfStrings(sx);

		Session s = openSession();

		s.save(role);
		s.flush();
		s.connection().commit();
		s.close();

		s = openSession();

		Role r = (Role) s.get(Role.class,new Long(role.getId()));
		assertNotSame(role,r);

		assertEquals(1,r.getInterventions().size());

		assertEquals(3, r.getBunchOfStrings().size());

		r.getBunchOfStrings().set(1, "replacement");
		s.flush();
		s.connection().commit();
		s.close();

		s = openSession();

		r = (Role) s.get(Role.class,new Long(role.getId()));
		assertNotSame(role,r);

		assertEquals(r.getBunchOfStrings().get(1),"replacement");
		assertEquals(3, r.getBunchOfStrings().size());

		r.getBunchOfStrings().set(1, "replacement");

		r.getBunchOfStrings().remove(1);
		s.flush();

		r.getBunchOfStrings().clear();
		s.flush();

		s.connection().commit();
		s.close();

	}

	public void testCRUD() throws HibernateException, SQLException {

		if ( getDialect() instanceof HSQLDialect ) return;
		if ( getDialect() instanceof MySQLDialect ) return;

		Person p = new Person();

		p.setName("Max");
		p.setLastName("Andersen");
		p.setNationalID("110974XYZ

⌨️ 快捷键说明

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