HibernateTestCase

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

 <hibernate-configuration>
<session-factory>
	<property name="connection.url">
		jdbc:mysql://localhost:3306/hibernate
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">123456</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="dialect">
		org.hibernate.dialect.MySQL5Dialect
	</property>
	<property name="show_sql">true</property>
	<property name="format_sql">true</property>

	<mapping resource="com/xiuye/mysql/dbclass/emp.hbm.xml" />
	<mapping resource="com/xiuye/mysql/dbclass/Account.hbm.xml" />
	<mapping resource="com/xiuye/mysql/dbclass/Service.hbm.xml" />

</session-factory>

</hibernate-configuration>
 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.xiuye.mysql.dbclass.Account" table="ACCOUNT">
		<id name="id" type="integer" column="id">
			<generator class="native">
				
			</generator>   
		</id>
		<property name="recommenderId" type="integer" column="RECOMMENDER_ID" />
		<property name="loginName" type="string" column="LOGIN_NAME" />
		<property name="loginPassword" type="string" column="LOGIN_PASSWD" />
		<property name="status" type="string" column="STATUS" />
		<property name="createDate" type="date" column="CREATE_DATE" />
		<property name="pauseDate" type="date" column="PAUSE_DATE" />
		<property name="closeDate" type="date" column="CLOSE_DATE" />
		<property name="realName" type="string" column="REAL_NAME" />
		<property name="idcardNo" type="string" column="IDCARD_NO" />
		<property name="birthdate" type="date" column="BIRTHDATE" />
		<property name="gender" type="string" column="GENDER" />
		<property name="occupation" type="string" column="OCCUPATION" />
		<property name="telephone" type="string" column="TELEPHONE" />
		<property name="email" type="string" column="EMAIL" />
		<property name="mailaddress" type="string" column="MAILADDRESS" />
		<property name="zipcode" type="string" column="ZIPCODE" />
		<property name="qq" type="string" column="QQ" />
		<property name="lastLoginTime" type="date" column="LAST_LOGIN_TIME" />
		<property name="lastLoginIp" type="string" column="LAST_LOGIN_IP" />

		<set name="services" lazy="false" fetch="join"
		cascade="all" inverse="true"
		>
		<!-- 外键 -->
			<key column="ACCOUNT_ID"
			
			/>
			<one-to-many class="com.xiuye.mysql.dbclass.Service"/>
		</set>

	</class>
</hibernate-mapping>






<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.xiuye.mysql.dbclass.Service" table="SERVICE">
		<id name="id" type="integer" column="id">
			<generator class="native">
				
			</generator>
		</id>
		<!-- <property name="accountId" type="integer" column="ACCOUNT_ID" /> -->
		<property name="unixHost" type="string" column="UNIX_HOST" />
		<property name="osUserName" type="string" column="OS_USERNAME" />
		<property name="loginPassword" type="string" column="LOGIN_PASSWD" />
		<property name="status" type="string" column="STATUS" />
		<property name="createDate" type="date" column="CREATE_DATE" />
		<property name="pauseDate" type="date" column="PAUSE_DATE" />
		<property name="closeDate" type="date" column="CLOSE_DATE" />
		<property name="costId" type="integer" column="COST_ID" />
		
		<many-to-one lazy="false" fetch="join" name="account" column="ACCOUNT_ID"
			class="com.xiuye.mysql.dbclass.Account" />
	</class>
</hibernate-mapping>
package com.xiuye.mysql.dbclass;

import java.sql.Date;

public class Service {
	@Override
	public String toString() {
		return "Service [id=" + id + ", unixHost=" + unixHost + ", osUserName="
				+ osUserName + ", loginPassword=" + loginPassword + ", status="
				+ status + ", createDate=" + createDate + ", pauseDate="
				+ pauseDate + ", closeDate=" + closeDate + ", costId=" + costId
				+ "]";
	}

	private Integer id;
	// private Integer accountId;
	private String unixHost;
	private String osUserName;
	private String loginPassword;
	private String status;
	private Date createDate;
	private Date pauseDate;
	private Date closeDate;
	private Integer costId;
	private Account account;

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	/*public Integer getAccountId() {
		return accountId;
	}

	public void setAccountId(Integer accountId) {
		this.accountId = accountId;
	}*/

	public String getUnixHost() {
		return unixHost;
	}

	public void setUnixHost(String unixHost) {
		this.unixHost = unixHost;
	}

	public String getOsUserName() {
		return osUserName;
	}

	public void setOsUserName(String osUserName) {
		this.osUserName = osUserName;
	}

	public String getLoginPassword() {
		return loginPassword;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getPauseDate() {
		return pauseDate;
	}

	public void setPauseDate(Date pauseDate) {
		this.pauseDate = pauseDate;
	}

	public Date getCloseDate() {
		return closeDate;
	}

	public void setCloseDate(Date closeDate) {
		this.closeDate = closeDate;
	}

	public Integer getCostId() {
		return costId;
	}

	public void setCostId(Integer costId) {
		this.costId = costId;
	}
}
package com.xiuye.mysql.dbclass;

import java.sql.Date;
import java.util.Set;

public class Account {
	private Integer id;
	private Integer recommenderId;
	private String loginName;
	private String loginPassword;
	private String status;
	private Date createDate;
	private Date pauseDate;
	private Date closeDate;
	private String realName;
	private String idcardNo;
	private Date birthdate;
	private String gender;
	private String occupation;
	private String telephone;
	private String email;
	private String mailaddress;
	private String zipcode;
	private String qq;
	private Date lastLoginTime;
	private String lastLoginIp;

	private Set<Service> services;
	
	
	
	@Override
	public String toString() {
		return "Account [id=" + id + ", recommenderId=" + recommenderId
				+ ", loginName=" + loginName + ", loginPassword="
				+ loginPassword + ", status=" + status + ", createDate="
				+ createDate + ", pauseDate=" + pauseDate + ", closeDate="
				+ closeDate + ", realName=" + realName + ", idcardNo="
				+ idcardNo + ", birthdate=" + birthdate + ", gender=" + gender
				+ ", occupation=" + occupation + ", telephone=" + telephone
				+ ", email=" + email + ", mailaddress=" + mailaddress
				+ ", zipcode=" + zipcode + ", qq=" + qq + ", lastLoginTime="
				+ lastLoginTime + ", lastLoginIp=" + lastLoginIp + "]";
	}

	public Set<Service> getServices() {
		return services;
	}

	public void setServices(Set<Service> services) {
		this.services = services;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getRecommenderId() {
		return recommenderId;
	}

	public void setRecommenderId(Integer recommenderId) {
		this.recommenderId = recommenderId;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getLoginPassword() {
		return loginPassword;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getPauseDate() {
		return pauseDate;
	}

	public void setPauseDate(Date pauseDate) {
		this.pauseDate = pauseDate;
	}

	public Date getCloseDate() {
		return closeDate;
	}

	public void setCloseDate(Date closeDate) {
		this.closeDate = closeDate;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getIdcardNo() {
		return idcardNo;
	}

	public void setIdcardNo(String idcardNo) {
		this.idcardNo = idcardNo;
	}

	public Date getBirthdate() {
		return birthdate;
	}

	public void setBirthdate(Date birthdate) {
		this.birthdate = birthdate;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getOccupation() {
		return occupation;
	}

	public void setOccupation(String occupation) {
		this.occupation = occupation;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMailaddress() {
		return mailaddress;
	}

	public void setMailaddress(String mailaddress) {
		this.mailaddress = mailaddress;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public String getLastLoginIp() {
		return lastLoginIp;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}
}

package test;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.xiuye.mysql.dbclass.Account;
import com.xiuye.mysql.dbclass.Service;
import com.xiuye.mysql.util.HibernateUtil;

public class Test3 {

	// @Test
	public void test1() {
		// onr to many
		Session session = HibernateUtil.getSession();

		Account account = (Account) session.get(Account.class, 2);
		System.out.println(account);
		System.out.println("==========================");

		Set<Service> services = account.getServices();
		System.out.println(services.getClass().getName());

		for (Service service : services) {

			System.out.println(service);

		}

		session.close();

	}

	// @Test
	public void test4() {

		Session session = HibernateUtil.getSession();

		Service service = (Service) session.get(Service.class, 100);

		System.out.println(service);
		System.out.println("=================");

		System.out.println(service.getAccount());

	}

	// @Test
	public void test3() {
		Account a = new Account();
		a.setLastLoginIp("gg");
		a.setLoginName("wwwwwwww");
		a.setLoginPassword("123456");

		Service s1 = new Service();
		s1.setAccount(a);
		s1.setOsUserName("Unix");
		s1.setLoginPassword("123456");

		Service s2 = new Service();
		s2.setAccount(a);
		s2.setOsUserName("Linux");
		s2.setLoginPassword("123456");

		a.setServices(new HashSet<Service>());
		a.getServices().add(s1);
		a.getServices().add(s2);

		Session session = HibernateUtil.getSession();
		Transaction as = session.beginTransaction();

		try {
			session.save(a);
			as.commit();
		} catch (HibernateException e) {

			e.printStackTrace();
			as.rollback();
		} finally {
			session.close();
		}

	}

	//@Test
	public void test5() {
		Session session = HibernateUtil.getSession();
		// 查询出要修改的账务账号
		Account account = (Account) session.get(Account.class, 5);
		// 模拟对账务账号的修改
		account.setLoginName("pp");
		Set<Service> services = account.getServices();
		for (Service service : services) {
			// 模拟对业务账号的修改
			service.setLoginPassword("pp");
		}
		Transaction ts = session.beginTransaction();
		try {
			session.update(account);
			ts.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			session.close();
		}
	}

	//@Test
	public void test6() {
		Session session = HibernateUtil.getSession();
		Account account = (Account) session.get(Account.class, 2);
		Transaction ts = session.beginTransaction();
		try {
			session.delete(account);
			ts.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			session.close();
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值