日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

兩種方式配置Hibernate復(fù)合主鍵(修正)

系統(tǒng) 2137 0

數(shù)據(jù)庫結(jié)構(gòu)

create table component(name varchar(50) not null,
?????????????????????? sex varchar(50) not null,
?????????????????????? description varchar(50),
?????????????????????? primary key(name,sex));

?

主鍵類,一定要實(shí)現(xiàn)Serializable接口,并改寫equals和hascode方法

?

package ?component;

import ?java.io.Serializable;

import ?org.apache.commons.lang.builder.EqualsBuilder;
import ?org.apache.commons.lang.builder.HashCodeBuilder;

public ? class ?ComponentPK? implements ?Serializable? ... {
??
private ?String?name;
??
private ?String?sex;

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

public ? void ?setName(String?name)? ... {
????
this .name? = ?name;
}

public ?String?getSex()? ... {
????
return ?sex;
}

public ? void ?setSex(String?sex)? ... {
????
this .sex? = ?sex;
}

public ? boolean ?equals(Object?obj)? ... {
????
if (obj? == ? this )? ... {
????????
return ? true ;
????}

????
????
if ( ! (obj? instanceof ?Component))? ... {
????????
return ? false ;
????}

????
????ComponentPK?componentpk?
= ?(ComponentPK)?obj;
????
return ? new ?EqualsBuilder()
??????????.append(
this .name,?componentpk.getName())
??????????.append(
this .sex,?componentpk.getSex())
??????????.isEquals();
????
??}

??
??
public ? int ?hashCode()? ... {
????
return ? new ?HashCodeBuilder()
??????????.append(
this .name)
??????????.append(
this .sex)
??????????.toHashCode();
??}


}

?

?

package ?component;

public ? class ?Component? ... {
??
private ?String?description;
??
private ?ComponentPK?componentpk;
public ?ComponentPK?getComponentpk()? ... {
????
return ?componentpk;
}

public ? void ?setComponentpk(ComponentPK?componentpk)? ... {
????
this .componentpk? = ?componentpk;
}

public ?String?getDescription()? ... {
????
return ?description;
}

public ? void ?setDescription(String?description)? ... {
????
this .description? = ?description;
}

}

?

HBM文件

?

<? 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"
>
<!-- ?
????Mapping?file?autogenerated?by?MyEclipse?-?Hibernate?Tools
-->
< hibernate-mapping? package ="component" > ?

??
< class? name ="Component" ?table ="component" > ?
????
< composite-id? name ="componentpk"
??????????????class
="ComponentPK"
??????????????unsaved-value
="any" >
????????
< key-property? name ="name" ?
????????????????column
="name" ?
????????????????type
="java.lang.String" />
????????
< key-property? name ="sex" ?
????????????????column
="sex" ?
????????????????type
="java.lang.String" />
????
</ composite-id >
????
????
< property? name ="description" ?column ="description" ?type ="java.lang.String" />
??
??
</ class > ?

</ hibernate-mapping >

?

?

測試代碼:

?

public ? static ? void ?main(String[]?args)? ... {
????????Configuration?cfg
= new ?Configuration();
????????cfg.configure();
????????SessionFactory?sf
= cfg.buildSessionFactory();
????????Session?session
= sf.openSession();
????????Transaction?t
= session.beginTransaction();
????????
????????ComponentPK?pk
= new ?ComponentPK();
????????pk.setName(
" 1 " );
????????pk.setSex(
" 3 " );
????????
????????Component?component
= new ?Component();
????????component.setComponentpk(pk);
????????component.setDescription(
" 12 " );
????????session.save(component);
????????t.commit();
????????System.out.println(
" success " );

????}

?

?

如果組成復(fù)合主鍵的某一個(gè)屬性是其他持久話類的話,則需要使用<key many-to-one>

增加表User:

CREATE TABLE `user` (
? `id` varchar(50) NOT NULL,
? `pass` varchar(50) default NULL,
? PRIMARY KEY? (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;??

增加持久化類User及相應(yīng)的Mapping文件

?

package ?component;

public ? class ?User? ... {
???
private ?String?id;
???
private ?String?pass;
public ?String?getId()? ... {
????
return ?id;
}

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

public ?String?getPass()? ... {
????
return ?pass;
}

public ? void ?setPass(String?pass)? ... {
????
this .pass? = ?pass;
}

}


<?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 " >
<!--?
????Mapping?file?autogenerated?by?MyEclipse?-?Hibernate?Tools
-->
<hibernate-mapping?package
= " component " >?

??<class?name
= " User " ?table = " user " >?
???
????<id?name
= " id " ?column = " id " >
??????<generator?class
= " assigned " ></generator>
????</id>
????<property?name
= " pass " ?column = " pass " ?type = " java.lang.String " />
??
??</class>?

</hibernate-mapping>


修改ComponentPK類

?

package ?component;

import ?java.io.Serializable;

import ?org.apache.commons.lang.builder.EqualsBuilder;
import ?org.apache.commons.lang.builder.HashCodeBuilder;

public ? class ?ComponentPK? implements ?Serializable? ... {
??
private ?String?name;
??
private ?String?sex;
??
private ?User?user;
public ?User?getUser()? ... {
????
return ?user;
}

兩種方式配置Hibernate復(fù)合主鍵(修正)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 东源县| 永川市| 布拖县| 江西省| 溆浦县| 汾西县| 福州市| 巴中市| 勐海县| 民勤县| 宁晋县| 乌苏市| 九龙城区| 阿拉善右旗| 鹤山市| 永吉县| 碌曲县| 太康县| 建平县| 平江县| 锡林浩特市| 喀喇沁旗| 拉孜县| 都兰县| 东乌珠穆沁旗| 民权县| 通河县| 哈尔滨市| 崇文区| 区。| 临桂县| 南木林县| 昆山市| 苍南县| 灵石县| 南通市| 平泉县| 霍林郭勒市| 西峡县| 股票| 通榆县|