代码:
person类:
public class Person {
private long id;
private String name;
private int age;
private Date birthDay;
private int schNo;
public long getId() {
return id;
}
public void setId(long 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 Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public int getSchNo() {
return schNo;
}
public void setSchNo(int schNo) {
this.schNo = schNo;
}
public Person() {
super();
}
public Person(long id, String name, int age, Date birthDay, int schNo) {
super();
this.id = id;
this.name = name;
this.age = age;
this.birthDay = birthDay;
this.schNo = schNo;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", birthDay=" + birthDay + ", schNo=" + schNo + "]";
}
}
school类:
public class School {
private int schNo;
private String schName;
private Set<Person> persons;
public int getSchNo() {
return schNo;
}
public void setSchNo(int schNo) {
this.schNo = schNo;
}
public String getSchName() {
return schName;
}
public void setSchName(String schName) {
this.schName = schName;
}
public School(int schNo, String schName) {
super();
this.schNo = schNo;
this.schName = schName;
}
public Set<Person> getPersons() {
return persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
public School() {
super();
}
}
test.hbm.xml文件:
<?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="hibernate.entity.Person" table="person">
<id name="id" column="pid"></id>
<property name="name" column="pname"></property>
<property name="birthDay" type="date" ></property>
<property name="age"></property>
<many-to-one name="schNo" column="schNo" class="hibernate.entity.School"/>
</class>
<class name="hibernate.entity.School" table="school">
<id name="schNo" column="schNo">
<generator class="native"></generator>
</id>
<property name="schName" column="schName"></property>
<set name="persons">
<key column="schNo"/>
<one-to-many class="hibernate.entity.Person"/>
</set>
</class>
</hibernate-mapping>
测试:
创建表并导出建表语句
@Test
public void test8(){
Configuration config = new Configuration().configure("test.hbmcfg.xml");
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.setOutputFile("D:"+File.separator+"test"+File.separator+"hibrenate_table.sql");
schemaExport.setFormat(true);
schemaExport.create(true, true);
//schemaExport.execute(true, true, false, true);
}
结果:
drop table person cascade constraints
drop table school cascade constraints
drop sequence hibernate_sequence
create table person (
pid number(19,0) not null,
pname varchar2(255 char),
birthDay date,
age number(10,0),
schNo number(10,0),
primary key (pid)
)
create table school (
schNo number(10,0) not null,
schName varchar2(255 char),
primary key (schNo)
)
alter table person
add constraint FKC4E39B55D836733C foreign key (schNo) references school
create sequence hibernate_sequence