值类型和动态组件

saveEntity()不能做更新,更新要在updateEntity()里面
使用值类型集合的坏处是想更改某一值有点麻烦,要改就要全部改再全部存起来。
使用值类型不用建立映射文件,只需要在实体POJO和映射文件上说明即可;使用动态组件不用建立映射文件,只需要在实体POJO和映射文件上说明即可.二者都是要新建表。
//下面是动态组件的表
动态组件的不用新建表,只需要在实体表上加入字段即可

//下面是值类型的表
create table alias(aliasId bigint not null primary key, foreign key(aliasId) references customer(id_no), aliasName varchar(20));//然后在数据库把aliasName也改成主键

Customer.hbm.xml代码:

<!--
    值类型,一对一,客户对多个别名
   -->
   <set name="aliases" table="alias" lazy="true">
   <key column="aliasId"/>
   <element column="aliasName" type="string"/>
   </set>
   <!--
    值类型,一对一,客户对地址
   -->
   <dynamic-component name="saddress">
   <!--parent name="customer"/--><!--把它注释去除的话就可以通过saddress找到customer然后进行操作-->
   <property name="city"  type="string" column="city">
   </property>
   <property name="province" column="province" type="string">
   </property>
   <property name="street" column="street" type="string">
   </property>
   <property name="number" column="number" type="int">
   </property>
   </dynamic-component>


Customer.java代码:

package basicCar.bean;

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

 

public class Customer implements java.io.Serializable{
 private long id_no; 

 private String name;
 
 private String address;
 
 
 private Set Accounts=new HashSet();//用于一对多存储多个账户时
 
 private Number number;
 
 private Set aliases = new HashSet();//值类型集合
 
 private Map sAddress;//动态组件,地址(省、市、街、门牌号)
 
 
 
 public Customer() {
 }

 public Customer(long Id, String Name, String Address) {
  this.id_no = Id;
  this.name = Name;
  this.address = Address;
 }

 public long getId_no() {
  return this.id_no;
 }

 public void setId_no(long Id) {
  this.id_no = Id;
 }

 public String getName() {
  return this.name;
 }

 public void setName(String N) {
  this.name = N;
 }
 
 public String getAddress() {
  return this.address;
 }

 public void setAddress(String A) {
  this.address = A;
 }
 
 public void setAccounts(Set A) {
  this.Accounts = A;
 }

 public Set getAccounts() {
  return this.Accounts;
 }

 public void setNumber(Number N){
  this.number = N;
 }
 
 public Number getNumber() {
  return this.number;
 }
 public void setAliases(Set A){
  this.aliases = A;
 }
 
 public Set getAliases() {
  return this.aliases;
 }

 public Map getSaddress() {
  return this.sAddress;
 }

 public void setSaddress(Map a) {
  this.sAddress = a;
 }
 
}

Test.java:

void saveEntity() {
		
		Session session;
		session = sf.openSession();
		// save a BasicCar in database
		Transaction tx1 = session.beginTransaction();
		
		//测试动态组件
		Map ad = new HashMap();
		ad.put("province", "aa4");
		ad.put("city", "shenzhen");
		ad.put("street", "goodstreet");
		ad.put("number", new Integer(1));
		Customer ss = new Customer(255,"小明","广州");
		ss.setSaddress(ad);
		
		session.save(ss);
		
		//测试值类型集合
	    //创建一个存放值类型的集合
		/*Set aliases = new HashSet();
		aliases.add("dog1");
		aliases.add("cat1");
		//新建用户
		Customer co1 = new Customer(244,"man32","广州");
		co1.setAliases(aliases);
		
		session.save(co1);*/



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值