比如一对夫妻,丈夫有id,name;妻子有id,name。增加一对一单向外键关联一般有以下几种方法:
1.在husband中增加一个外键,foreign key
2.在husband中增加字段wife_id,wife_id参照wife的id。以wife为主导,必须wife里有id才能参照
3.在wife中增加一个外键,foreign key
4.在wife中增加字段husband_id,husband_id参照husband的id。以hunsband为主导,必须hunsband里有id才能参照
5.增加中间表——关联表
数据库设计一般两种:主键关联,单向外键关联
示例代码:
husband类
package com.zzk.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Husband { private int id; private String name; private Wife wife;//对象引用,建立关联 @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @OneToOne public Wife getWife() { return wife; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setWife(Wife wife) { this.wife = wife; } }
wife类
package com.zzk.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Wife { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } }
JUNIT测试生成建表语句的方法:
@Test public void testSchemaExport() { new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); }
LOG4J输出:
11:07:55,366 INFO org.hibernate.tool.hbm2ddl.SchemaExport:226 - Running hbm2ddl schema export 11:07:55,369 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:242 - import file not found: /import.sql 11:07:55,369 INFO org.hibernate.tool.hbm2ddl.SchemaExport:251 - exporting generated schema to database 11:07:55,559 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband drop foreign key FKAEEA401B4AFE23AB 11:07:55,626 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:362 - Unsuccessful: alter table Husband drop foreign key FKAEEA401B4AFE23AB 11:07:55,626 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:363 - Table 'hibernate.husband' doesn't exist 11:07:55,627 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - drop table if exists Husband 11:07:55,648 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - drop table if exists Wife 11:07:55,652 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Husband ( id integer not null auto_increment, name varchar(255), wife_id integer, primary key (id) ) 11:07:55,743 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Wife ( id integer not null auto_increment, name varchar(255), primary key (id) ) 11:07:55,818 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband add index FKAEEA401B4AFE23AB (wife_id), add constraint FKAEEA401B4AFE23AB foreign key (wife_id) references Wife (id) 11:07:56,040 INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete
然后为了将wife_id改为wifeId,可以对husband类作如下处理:
package com.zzk.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Husband { private int id; private String name; private Wife wife;//对象引用,建立关联 @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @OneToOne @JoinColumn (name="wifeId") public Wife getWife() { return wife; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setWife(Wife wife) { this.wife = wife; } }
运行JUNIT测试方法,LOG4J打印的建表语句即变为:
11:27:11,113 INFO org.hibernate.tool.hbm2ddl.SchemaExport:226 - Running hbm2ddl schema export 11:27:11,116 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:242 - import file not found: /import.sql 11:27:11,116 INFO org.hibernate.tool.hbm2ddl.SchemaExport:251 - exporting generated schema to database 11:27:11,303 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband drop foreign key FKAEEA401BCC07428E 11:27:11,393 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:362 - Unsuccessful: alter table Husband drop foreign key FKAEEA401BCC07428E 11:27:11,394 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:363 - Error on rename of '.\hibernate\husband' to '.\hibernate\#sql2-8f0-f' (errno: 152) 11:27:11,395 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - drop table if exists Husband 11:27:11,418 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - drop table if exists Wife 11:27:11,445 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Husband ( id integer not null auto_increment, name varchar(255), wifeId integer, primary key (id) ) 11:27:11,505 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Wife ( id integer not null auto_increment, name varchar(255), primary key (id) ) 11:27:11,579 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband add index FKAEEA401BCC07428E (wifeId), add constraint FKAEEA401BCC07428E foreign key (wifeId) references Wife (id) 11:27:11,715 INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete
另外一种方法配置:
student类
package com.zzk.hibernate.model; public class Student { private int id; private String name; private int age; private String sex; private boolean good; public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
StuIdCard类
package com.zzk.hibernate.model; public class StuIdCard { private int id; private String num; private Student student; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
StuIdCard.hbm.xml
<?xml version="1.0"?> <!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.zzk.hibernate.model.StuIdCard"> <id name="id"> <generator class="native"></generator> </id> <property name="num"/> <many-to-one name="student" column="studentId" unique="true"></many-to-one> </class> </hibernate-mapping>
Student.hbm.xml
<?xml version="1.0"?> <!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.zzk.hibernate.model.StuIdCard"> <id name="id"> <generator class="native"></generator> </id> <property name="num"/> <many-to-one name="student" column="studentId" unique="true"></many-to-one> </class> </hibernate-mapping>
hibernate.cfg.xml增加如下:
<mapping resource="com/zzk/hibernate/model/Student.hbm.xml"/> <mapping resource="com/zzk/hibernate/model/StuIdCard.hbm.xml"/>
JUNIT单元测试:
@Test public void testSchemaExport() { new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); }
输出LOG4J的一些建表语句如下:
15:03:29,397 INFO org.hibernate.tool.hbm2ddl.SchemaExport:226 - Running hbm2ddl schema export 15:03:29,401 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:242 - import file not found: /import.sql 15:03:29,401 INFO org.hibernate.tool.hbm2ddl.SchemaExport:251 - exporting generated schema to database 15:03:31,082 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband drop foreign key FKAEEA401BCC07428E 15:03:31,880 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table StuIdCard drop foreign key FKD3A449FF7E0CA1A0 15:03:31,934 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:362 - Unsuccessful: alter table StuIdCard drop foreign key FKD3A449FF7E0CA1A0 15:03:31,934 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:363 - Table 'hibernate.stuidcard' doesn't exist 15:03:31,934 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - drop table if exists Husband 15:03:31,960 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - drop table if exists StuIdCard 15:03:31,970 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - drop table if exists Student 15:03:32,045 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - drop table if exists Wife 15:03:32,132 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Husband ( id integer not null auto_increment, name varchar(255), wifeId integer, primary key (id) ) 15:03:32,214 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table StuIdCard ( id integer not null auto_increment, num varchar(255), studentId integer unique, primary key (id) ) 15:03:32,306 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Student ( id integer not null auto_increment, name varchar(255), age integer, sex varchar(255), good char(1), primary key (id) ) 15:03:32,382 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Wife ( id integer not null auto_increment, name varchar(255), primary key (id) ) 15:03:32,446 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband add index FKAEEA401BCC07428E (wifeId), add constraint FKAEEA401BCC07428E foreign key (wifeId) references Wife (id) 15:03:32,722 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table StuIdCard add index FKD3A449FF7E0CA1A0 (studentId), add constraint FKD3A449FF7E0CA1A0 foreign key (studentId) references Student (id) 15:03:32,916 INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete
时间: 2024-10-22 20:33:41