Hibernate Annotation 一对多单向关联

来源:互联网 时间:2017-07-27

1、单向关联


(1)Group




[java]
view plain
copyprint
?

%20
packagecom.yy.hibernate.model;importjava.util.HashSet;%20
%20importjava.util.Set;importjavax.persistence.Entity;%20
%20importjavax.persistence.GeneratedValue;%20
%20importjavax.persistence.Id;%20
%20importjavax.persistence.JoinColumn;%20
%20importjavax.persistence.OneToMany;%20
%20importjavax.persistence.Table;@Entity
%[email protected]%20(name="t_group")%20
%20publicclassGroup{privateintid;%20
%20privateStringname;%20
%20privateSetset=newHashSet();@OneToMany%20
%[email protected](name="groupId")%20
%20publicSetgetSet(){%20
%20returnset;%20
%20}%20
%20publicvoidsetSet(Setset){%20
%20this.set=set;%20
%20}%20
%[email protected]
%[email protected]%20
%20publicintgetId(){%20
%20returnid;%20
%20}%20
%20publicvoidsetId(intid){%20
%20this.id=id;%20
%20}%20
%20publicStringgetName(){%20
%20returnname;%20
%20}%20
%20publicvoidsetName(Stringname){%20
%20this.name=name;%20
%20}}%20
(2)User.java%20

%20

%20

%20

%20
%20
%20[java]%20
%20view%20plain%20
%20copyprint%20
%20?%20
%20
%20
packagecom.yy.hibernate.model;importjavax.persistence.Entity;%20
%20importjavax.persistence.GeneratedValue;%20
%20importjavax.persistence.Id;%20
%20importjavax.persistence.ManyToOne;%20
%20importjavax.persistence.Table;@Entity%20
%[email protected](name="t_user")%20
%20publicclassUser{privateintid;%20
%20privateStringname;%20
%[email protected]%20
%[email protected]%20
%20publicintgetId(){%20
%20returnid;%20
%20}%20
%20publicvoidsetId(intid){%20
%20this.id=id;%20
%20}%20
%20publicStringgetName(){%20
%20returnname;%20
%20}%20
%20publicvoidsetName(Stringname){%20
%20this.name=name;%20
%20}}%20
%20
%20
%20[html]%20
%20view%20plain%20
%20copyprint%20
%20?%20
%20
%20
mysql>desct_group;%20
%20+-------+--------------+------+-----+---------+----------------+%20
%20|Field|Type|Null|Key|Default|Extra|%20
%20+-------+--------------+------+-----+---------+----------------+%20
%20|id|int(11)|NO|PRI|NULL|auto_increment|%20
%20|name|varchar(255)|YES||NULL||%20
%20+-------+--------------+------+-----+---------+----------------+%20
%20
%20
%20[html]%20
%20view%20plain%20
%20copyprint%20
%20?%20
%20

mysql>desct_user;
+---------+--------------+------+-----+---------+----------------+
|Field|Type|Null|Key|Default|Extra|
+---------+--------------+------+-----+---------+----------------+
|id|int(11)|NO|PRI|NULL|auto_increment|
|name|varchar(255)|YES||NULL||
|groupId|int(11)|YES|MUL|NULL||
+---------+--------------+------+-----+---------+----------------+

相关阅读:
Top